| Deutsch English Français Italiano |
|
<vrdi0g$47cb$3@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!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: Wed, 19 Mar 2025 00:42:57 -0400
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <vrdi0g$47cb$3@dont-email.me>
References: <vrd77d$3nvtf$2@dont-email.me>
<87a59hvgyk.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 19 Mar 2025 05:42:57 +0100 (CET)
Injection-Info: dont-email.me; posting-host="92f16538b7b31d02767c6fdbe2b76261";
logging-data="138635"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Z7PV2zZNBOWxV66v7u4w1"
User-Agent: Betterbird (Windows)
Cancel-Lock: sha1:qmeR15On45AlJ/Fi3Hy8VRTM+1o=
In-Reply-To: <87a59hvgyk.fsf@nosuchdomain.example.com>
Content-Language: en-US
Bytes: 2851
On 3/18/2025 11:26 PM, Keith Thompson wrote:
> DFS <nospam@dfs.com> writes:
> There's your problem.
>
> https://cses.fi/problemset/text/2433
>
> "In all problems you should read input from standard input and write
> output to standard output."
ha! It usually helps to read the instructions first.
> The autotester expects your program to read arguments from stdin, not
> from command line arguments.
>
> It probably passes no arguments to your program, so argv[1] is a null
> pointer. It's likely your program compiles (assuming the NBSP
> characters were added during posting) and crashes at runtime, producing
> no output.
I KNEW clc would come through!
Pretty easy fixes:
1 use scanf()
2 update int to long
3 handle special case of n = 1
4 instead of collecting the results in a char variable, I print
them as they're calculated
The algorithm part was very simple and correct. Later ones won't be so
easy. I coded 4 so far (but just submitted this one here), and plan on
doing all 300.
https://imgur.com/bq0pKIw
Did you hear a boom?
Thanks again!
updated code that passes:
===============================================================
// If n is even, divide it by two.
// If n is odd, multiply it by three and add one.
// Repeat until n is one.
// example: the sequence for n=3 is 3 10 5 16 8 4 2 1
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
long n = 0;
scanf("%ld", &n);
printf("%ld ",n);
while(1) {
if(n==1) {exit(0);}
if((n % 2) == 0)
{n /= 2;}
else
{n = (n * 3) + 1;}
if(n != 1)
{printf("%ld ",n);}
else
break;
}
printf("1\n");
return 0;
}
===============================================================