Deutsch English Français Italiano |
<vrmvoa$giup$2@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Lynn McGuire <lynnmcguire5@gmail.com> Newsgroups: comp.lang.c Subject: Re: Suggested method for returning a string from a C program? Date: Sat, 22 Mar 2025 13:32:41 -0500 Organization: A noiseless patient Spider Lines: 61 Message-ID: <vrmvoa$giup$2@dont-email.me> References: <vrd77d$3nvtf$2@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 22 Mar 2025 19:32:42 +0100 (CET) Injection-Info: dont-email.me; posting-host="728f68033f29e1d00af60249e02af5fb"; logging-data="543705"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/t1ZMgCsAGxmQZP3EKEYzFUoaauiH4UO8=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:OUw3PUecSE6xiwdK7Mndas0zPFY= Content-Language: en-US In-Reply-To: <vrd77d$3nvtf$2@dont-email.me> On 3/18/2025 8:38 PM, DFS 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? > Thanks strdup. https://www.geeksforgeeks.org/strdup-strdndup-functions-c/ Lynn