Re: Suggested method for returning a string from a C program?

Liste des GroupesRevenir à cl c  
Sujet : Re: Suggested method for returning a string from a C program?
De : nospam (at) *nospam* dfs.com (DFS)
Groupes : comp.lang.c
Date : 22. Mar 2025, 15:29:13
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vrmhfm$2tds$1@dont-email.me>
References : 1 2 3 4
User-Agent : Betterbird (Windows)
On 3/22/2025 4:07 AM, Peter 'Shaggy' Haywood wrote:
Groovy hepcat DFS was jivin' in comp.lang.c on Wed, 19 Mar 2025 03:42
pm. It's a cool scene! Dig it.
 
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()
    Normally I'd say take care with scanf(). But in this case, since the
program is intended to be executed in an automated environment, it
should be fine.
   The reason scanf() can be a bit iffy is that you can't control what a
user will enter. If you search Google or Duck Duck Go for "comp.lang.c
faq" you can find more information on this and other issues. (The FAQ
is still out there, people..., somewhere...)
https://c-faq.com/
I still see links to that document from time to time, like on university websites.

2 update int to long
3 handle special case of n = 1
    The problem definition doesn't mention any special case. You should, I
think, treat 1 like any other number. So the output for 1 should be
 1 4 2 1
It's a 'special case' because n is already 1.
Your code passed all CSES tests but this one.

4 instead of collecting the results in a char variable, I print
    them as they're calculated
    Yep, that's a more usual approach.
   Another suggestion I have is to use a separate function to do part of
the work. But it's not vital.
   Also, since the specification says that only positive numbers are to
be accepted, it makes sense (to me, at least) to use an unsigned type
for n.
   One more thing: using while(1){...break;} is a bit pointless. You can
use do{...}while(1 != n) instead.
   Here's my solution, for what it's worth:
 #include <stdio.h>
 unsigned long weird(unsigned long n)
{
   printf("%lu", n);
    if(n & 1)
   {
     /* Odd - multiply by 3 & add 1. */
     n = n * 3 + 1;
   }
   else
   {
     /* Even - divide by 2. */
     n /= 2;
   }
   return n;
}
 int main(void)
{
   unsigned long n;
    /* Get n from stdin. */
   scanf("%lu", &n);
    /* Now feed it to the algorithm. */
   do
   {
     n = weird(n);
     putchar(' ');
   } while(1 != n);
    printf("%lu\n", n);
   return 0;
}
Cool.
I tweaked my original and got it down to:
--------------------------------------------------------
#include <stdio.h>
int main(void)
{
long n = 0;
scanf("%ld", &n);
while(n > 1) {
printf("%ld ",n);
n = (n % 2) ? (n * 3 + 1) : (n / 2);
}
printf("1\n");
return 0;
}
--------------------------------------------------------
I also liked the Number Spiral, Coin Piles and Palindrome Reorder problems.
Thanks for the input!

Date Sujet#  Auteur
19 Mar 25 * Suggested method for returning a string from a C program?391DFS
19 Mar 25 +* Re: Suggested method for returning a string from a C program?4Keith Thompson
19 Mar 25 i+- Re: Suggested method for returning a string from a C program?1Keith Thompson
19 Mar 25 i`* Re: Suggested method for returning a string from a C program?2DFS
19 Mar 25 i `- Re: Suggested method for returning a string from a C program?1Keith Thompson
19 Mar 25 +* Re: Suggested method for returning a string from a C program?323Tim Rentsch
19 Mar 25 i+* Re: Suggested method for returning a string from a C program?2DFS
19 Mar 25 ii`- Re: Suggested method for returning a string from a C program?1Richard Heathfield
19 Mar 25 i`* Re: Suggested method for returning a string from a C program?320DFS
19 Mar 25 i +* Re: Suggested method for returning a string from a C program?6Tim Rentsch
19 Mar 25 i i`* Re: Suggested method for returning a string from a C program?5DFS
19 Mar 25 i i +* Re: Suggested method for returning a string from a C program?3James Kuyper
19 Mar 25 i i i+- Re: Suggested method for returning a string from a C program?1Keith Thompson
19 Mar 25 i i i`- Re: Suggested method for returning a string from a C program?1DFS
19 Mar 25 i i `- Re: Suggested method for returning a string from a C program?1Tim Rentsch
19 Mar 25 i `* Re: Suggested method for returning a string from a C program?313Michael S
19 Mar 25 i  +* Re: Suggested method for returning a string from a C program?309DFS
19 Mar 25 i  i`* Re: Suggested method for returning a string from a C program?308Richard Heathfield
19 Mar 25 i  i `* Re: Suggested method for returning a string from a C program?307Michael S
19 Mar 25 i  i  +- Re: Suggested method for returning a string from a C program?1Richard Heathfield
20 Mar 25 i  i  `* Re: Suggested method for returning a string from a C program?305Tim Rentsch
20 Mar 25 i  i   `* Re: Suggested method for returning a string from a C program?304bart
20 Mar 25 i  i    +* Re: Suggested method for returning a string from a C program?298bart
20 Mar 25 i  i    i+* Re: Suggested method for returning a string from a C program?92Muttley
20 Mar 25 i  i    ii+* Re: Suggested method for returning a string from a C program?8Michael S
20 Mar 25 i  i    iii`* Re: Suggested method for returning a string from a C program?7Muttley
20 Mar 25 i  i    iii `* Re: Suggested method for returning a string from a C program?6Michael S
20 Mar 25 i  i    iii  +* Re: Suggested method for returning a string from a C program?3Muttley
23 Mar 25 i  i    iii  i`* Re: Suggested method for returning a string from a C program?2Michael S
23 Mar 25 i  i    iii  i `- Re: Suggested method for returning a string from a C program?1Tim Rentsch
20 Mar 25 i  i    iii  +- Re: Suggested method for returning a string from a C program?1Tim Rentsch
20 Mar 25 i  i    iii  `- Re: Suggested method for returning a string from a C program?1Keith Thompson
20 Mar 25 i  i    ii`* Re: Suggested method for returning a string from a C program?83bart
20 Mar 25 i  i    ii +- Re: Suggested method for returning a string from a C program?1Muttley
20 Mar 25 i  i    ii +* Re: Suggested method for returning a string from a C program?80Michael S
20 Mar 25 i  i    ii i`* Re: Suggested method for returning a string from a C program?79bart
20 Mar 25 i  i    ii i +* Re: Suggested method for returning a string from a C program?3Kaz Kylheku
20 Mar 25 i  i    ii i i`* Re: Suggested method for returning a string from a C program?2Michael S
20 Mar 25 i  i    ii i i `- Re: Suggested method for returning a string from a C program?1Kaz Kylheku
21 Mar 25 i  i    ii i `* Re: Suggested method for returning a string from a C program?75Keith Thompson
24 Mar 25 i  i    ii i  `* The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)74Janis Papanagnou
24 Mar 25 i  i    ii i   `* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)73Keith Thompson
25 Mar 25 i  i    ii i    `* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)72David Brown
25 Mar 25 i  i    ii i     +* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)5Kaz Kylheku
25 Mar 25 i  i    ii i     i`* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)4David Brown
25 Mar 25 i  i    ii i     i `* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)3Keith Thompson
26 Mar 25 i  i    ii i     i  +- Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)1Chris M. Thomasson
26 Mar 25 i  i    ii i     i  `- Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)1David Brown
25 Mar 25 i  i    ii i     +* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)64Janis Papanagnou
25 Mar 25 i  i    ii i     i+* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)54bart
25 Mar 25 i  i    ii i     ii`* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)53Janis Papanagnou
26 Mar 25 i  i    ii i     ii +* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)2bart
26 Mar 25 i  i    ii i     ii i`- Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)1Janis Papanagnou
26 Mar 25 i  i    ii i     ii `* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)50David Brown
26 Mar 25 i  i    ii i     ii  `* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)49Janis Papanagnou
26 Mar 25 i  i    ii i     ii   `* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)48David Brown
26 Mar 25 i  i    ii i     ii    `* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)47Janis Papanagnou
26 Mar 25 i  i    ii i     ii     +- Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)1David Brown
27 Mar 25 i  i    ii i     ii     `* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)45bart
27 Mar 25 i  i    ii i     ii      +- Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)1bart
27 Mar 25 i  i    ii i     ii      +* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)4Waldek Hebisch
27 Mar 25 i  i    ii i     ii      i`* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)3bart
27 Mar 25 i  i    ii i     ii      i +- Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)1David Brown
28 Mar 25 i  i    ii i     ii      i `- Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)1Waldek Hebisch
27 Mar 25 i  i    ii i     ii      `* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)39Janis Papanagnou
27 Mar 25 i  i    ii i     ii       +* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)13bart
28 Mar 25 i  i    ii i     ii       i`* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)12Janis Papanagnou
28 Mar 25 i  i    ii i     ii       i +* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)9David Brown
28 Mar 25 i  i    ii i     ii       i i+* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)7Janis Papanagnou
28 Mar 25 i  i    ii i     ii       i ii+* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)2Michael S
28 Mar 25 i  i    ii i     ii       i iii`- Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)1Janis Papanagnou
28 Mar 25 i  i    ii i     ii       i ii+* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)2bart
28 Mar 25 i  i    ii i     ii       i iii`- Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)1Janis Papanagnou
28 Mar 25 i  i    ii i     ii       i ii`* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)2David Brown
28 Mar 25 i  i    ii i     ii       i ii `- Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)1Janis Papanagnou
28 Mar 25 i  i    ii i     ii       i i`- Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)1Chris M. Thomasson
28 Mar 25 i  i    ii i     ii       i +- Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)1bart
31 Mar 25 i  i    ii i     ii       i `- [OT] PC hardware prices [correction] (was Re: The integral type 'byte')1Janis Papanagnou
27 Mar 25 i  i    ii i     ii       `* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)25David Brown
28 Mar 25 i  i    ii i     ii        `* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)24Janis Papanagnou
28 Mar 25 i  i    ii i     ii         +* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)18Chris M. Thomasson
28 Mar 25 i  i    ii i     ii         i`* [OT] SPARC (was Re: The integral type 'byte')17Janis Papanagnou
28 Mar 25 i  i    ii i     ii         i +- Re: [OT] SPARC (was Re: The integral type 'byte')1Keith Thompson
28 Mar 25 i  i    ii i     ii         i +* Re: [OT] SPARC (was Re: The integral type 'byte')14Michael S
28 Mar 25 i  i    ii i     ii         i i+* Re: [OT] SPARC (was Re: The integral type 'byte')8David Brown
28 Mar 25 i  i    ii i     ii         i ii+* Re: [OT] SPARC (was Re: The integral type 'byte')6Michael S
28 Mar 25 i  i    ii i     ii         i iii`* Re: [OT] SPARC (was Re: The integral type 'byte')5David Brown
28 Mar 25 i  i    ii i     ii         i iii +* Re: [OT] SPARC (was Re: The integral type 'byte')3Chris M. Thomasson
28 Mar 25 i  i    ii i     ii         i iii i+- Re: [OT] SPARC (was Re: The integral type 'byte')1Chris M. Thomasson
28 Mar 25 i  i    ii i     ii         i iii i`- Re: [OT] SPARC (was Re: The integral type 'byte')1Kaz Kylheku
28 Mar 25 i  i    ii i     ii         i iii `- Re: [OT] SPARC (was Re: The integral type 'byte')1Kaz Kylheku
28 Mar 25 i  i    ii i     ii         i ii`- Re: [OT] SPARC (was Re: The integral type 'byte')1Kaz Kylheku
28 Mar 25 i  i    ii i     ii         i i`* Re: [OT] SPARC (was Re: The integral type 'byte')5Janis Papanagnou
28 Mar 25 i  i    ii i     ii         i i `* Re: [OT] SPARC (was Re: The integral type 'byte')4Michael S
28 Mar 25 i  i    ii i     ii         i i  +* Re: [OT] SPARC (was Re: The integral type 'byte')2Janis Papanagnou
28 Mar 25 i  i    ii i     ii         i i  i`- Re: [OT] SPARC (was Re: The integral type 'byte')1David Brown
28 Mar 25 i  i    ii i     ii         i i  `- Re: [OT] SPARC (was Re: The integral type 'byte')1Kaz Kylheku
28 Mar 25 i  i    ii i     ii         i `- Re: [OT] SPARC (was Re: The integral type 'byte')1Chris M. Thomasson
28 Mar 25 i  i    ii i     ii         `* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)5David Brown
28 Mar 25 i  i    ii i     ii          +- Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)1Michael S
28 Mar 25 i  i    ii i     ii          +* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)2bart
28 Mar 25 i  i    ii i     ii          `- Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)1Janis Papanagnou
26 Mar 25 i  i    ii i     i`* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)9David Brown
25 Mar 25 i  i    ii i     `* Re: The integral type 'byte' (was Re: Suggested method for returning a string from a C program?)2Keith Thompson
20 Mar 25 i  i    ii `- Re: Suggested method for returning a string from a C program?1Keith Thompson
20 Mar 25 i  i    i+* Re: Suggested method for returning a string from a C program?93bart
20 Mar 25 i  i    i+* Re: Suggested method for returning a string from a C program?90Keith Thompson
21 Mar 25 i  i    i`* Re: Suggested method for returning a string from a C program?22Waldek Hebisch
20 Mar 25 i  i    +- Re: Suggested method for returning a string from a C program?1Michael S
20 Mar 25 i  i    +- Re: Suggested method for returning a string from a C program?1Muttley
20 Mar 25 i  i    +- Re: Suggested method for returning a string from a C program?1Kaz Kylheku
20 Mar 25 i  i    +- Re: Suggested method for returning a string from a C program?1Tim Rentsch
20 Mar 25 i  i    `- Re: Suggested method for returning a string from a C program?1Keith Thompson
19 Mar 25 i  `* Re: Suggested method for returning a string from a C program?3Tim Rentsch
19 Mar 25 +* Re: Suggested method for returning a string from a C program?27Keith Thompson
19 Mar 25 +* Re: Suggested method for returning a string from a C program?9Ike Naar
19 Mar 25 +* Re: Suggested method for returning a string from a C program?19bart
19 Mar 25 +* Re: Suggested method for returning a string from a C program?6Michael S
22 Mar 25 `* Re: Suggested method for returning a string from a C program?2Lynn McGuire

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal