Deutsch   English   Français   Italiano  
<slrnvtkrqc.ii1.ike@faeroes.freeshell.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Ike Naar <ike@sdf.org>
Newsgroups: comp.lang.c
Subject: Re: Suggested method for returning a string from a C program?
Date: Wed, 19 Mar 2025 07:16:29 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <slrnvtkrqc.ii1.ike@faeroes.freeshell.org>
References: <vrd77d$3nvtf$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=646
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 19 Mar 2025 08:16:30 +0100 (CET)
Injection-Info: dont-email.me; posting-host="1abc227d17852392ce3f5e15ddd32794";
	logging-data="386389"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19Nvkz+TYsAJ0Cvch3HZiEV"
User-Agent: slrn/1.0.3 (Patched for libcanlock3) (NetBSD)
Cancel-Lock: sha1:plTpu1AnvZZTGzzy+c4T/uLYRGU=

On 2025-03-19, DFS <nospam@dfs.com> wrote:
>
> I'm doing these algorithm problems at
> https://cses.fi/problemset/list/
>
> For instance: Weird Algorithm
> https://cses.fi/problemset/task/1068
>
> My code works fine locally (prints the correct solution to the console), 
> but when I submit the .c file the auto-tester flags it with 'runtime 
> error' and says the output is empty.
>
> ------------------------------------------------------------
> // If n is even, divide it by two.
> // If n is odd, multiply it by three and add one.
> // Repeat until n is one.
> // n = 3: output is 3 10 5 16 8 4 2 1
>
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> int main(int argc, char *argv[])
> {
>      int n = atoi(argv[1]);
>      int len = 0;
>      char result[10000] = "";
>      sprintf(result, "%d ", n);
>
>      while(1) {
>          if((n % 2) == 0)
>              {n /= 2;}
>          else
>              {n = (n * 3) + 1;}
>
>          if(n != 1)
>              {
>                  len = strlen(result);
>                  sprintf(result + len, "%d ", n);
>              }
>          else
>              break;
>      }
>
>      len = strlen(result);
>      sprintf(result + len, "1 ");
>      printf("%s\n",result);
>
>      return 0;
> }
> ------------------------------------------------------------
>
> Any ideas?

What happens if the input is a negative number?