| Deutsch English Français Italiano |
|
<vrde09$3sjne$1@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: DFS <nospam@dfs.com>
Newsgroups: comp.lang.c
Subject: Re: Suggested method for returning a string from a C program?
Date: Tue, 18 Mar 2025 23:34:33 -0400
Organization: A noiseless patient Spider
Lines: 98
Message-ID: <vrde09$3sjne$1@dont-email.me>
References: <vrd77d$3nvtf$2@dont-email.me> <868qp1ra5f.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 19 Mar 2025 04:34:41 +0100 (CET)
Injection-Info: dont-email.me; posting-host="92f16538b7b31d02767c6fdbe2b76261";
logging-data="4083438"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+yxMJjj22gpOefMABhNFoX"
User-Agent: Betterbird (Windows)
Cancel-Lock: sha1:ZehOwbzrLfrKuk5P0/W9jfjGW6M=
Content-Language: en-US
In-Reply-To: <868qp1ra5f.fsf@linuxsc.com>
On 3/18/2025 11:07 PM, Tim Rentsch wrote:
> DFS <nospam@dfs.com> writes:
>
>> 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?
>
> Have you thought about how large the value of 'n' can
> become inside the while() loop?
Each algorithm problem has a constraint you're supposed to consider
(which I figure is the largest value they test for).
For this 'weird algorithm' problem, the constraint is 1 <= n <= 10^6.
The output of n = 999999 is:
999999 2999998 1499999 4499998 2249999 6749998 3374999 10124998 5062499
15187498 7593749 22781248 11390624 5695312 2847656 1423828 711914 355957
1067872 533936 266968 133484 66742 33371 100114 50057 150172 75086 37543
112630 56315 168946 84473 253420 126710 63355 190066 95033 285100 142550
71275 213826 106913 320740 160370 80185 240556 120278 60139 180418 90209
270628 135314 67657 202972 101486 50743 152230 76115 228346 114173
342520 171260 85630 42815 128446 64223 192670 96335 289006 144503 433510
216755 650266 325133 975400 487700 243850 121925 365776 182888 91444
45722 22861 68584 34292 17146 8573 25720 12860 6430 3215 9646 4823 14470
7235 21706 10853 32560 16280 8140 4070 2035 6106 3053 9160 4580 2290
1145 3436 1718 859 2578 1289 3868 1934 967 2902 1451 4354 2177 6532 3266
1633 4900 2450 1225 3676 1838 919 2758 1379 4138 2069 6208 3104 1552 776
388 194 97 292 146 73 220 110 55 166 83 250 125 376 188 94 47 142 71 214
107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233
700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502
251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079
3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077
9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184
92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
The largest n in there is 22781248.
So my variables are plenty big enough to handle the constraint and output.
But my main concern right now is getting my submission to work with
their auto-tester. All it says is 'runtime error' and 'empty output'.
Any ideas what I might be doing wrong?