Hey faggots I am a brainlet tying to learn C and I am having a hard time doing this:
Read a sequence of numbers separated by white spaces from stdin and store them in an integer array. Like, read something like this: 123, 435, 123, 432, 123 and store each of those numbers in an integer array.
ez. don't bother testing it; just give it to your teacher.
Henry James
>a brainlet trying to learn C just give up and learn python like every other brainlet
Wyatt Perry
I already did, but C sounds very interesting too and I want to become a better programer
Luke Thompson
That's not even C, user.
Alexander Morgan
Multiple ways to do this. There are also multiple ways to read input into a variable or array. Once your input is read, you can loop through it a letter/digit at a time or even simply until you reach one specific letter, digit, symbol or white space; Usually called a delimiter. Once you reach a delimiter, you could store everything behind or in front of it in it's own variable or inside an array at a specific index until you run out of delimiters or reach the end of the input you've read.
Samuel Morales
Whoosh?
Oliver Sanders
Yeah that is what I was thinking about but I was wondering if there was a way to use like scanf. But problem is that the number of integers is not the same for diferent inputs. I will probably just do a loop using fget.
Dylan Garcia
I'm too lazy to write the program for you, but basically int arr[argc]; int i; for(i=0; i
Colton Walker
Usually examples like this will tell you the length of input in the first number, then send the sequence: "4 1 2 3 4". I doubt your teacher expects you to handle an infinite stream of input before you know how to confidently read and parse input.
Hudson Baker
I forgot to mention that first you introduce the number of elements, and then you introduce the array separated by whitespaces
Jonathan Baker
It should be argc - 1
Sebastian Robinson
reddit-->?
Julian Stewart
Usually in examples like this the first number of the sequence will be the size of it: "4 2 5 5 2". I doubt your teacher expects you to handle an unknown amount of input before you know how to comfortably parse an array.
Isaiah James
And you introduce it in different lines
Jaxon Jones
Is this a hackerrank question?
Elijah Lewis
had trouble posting, accidental double post.
Carter Gray
First you introduce the number of elements, then on another line you introduce the array elements
Yeah
Dominic Garcia
>Jow Forums can't write a simple C program
Tyler Garcia
Do please teach us >inb4 not doing your homework ofc user ofc
Charles Ward
Just go through the string and when you encounter a whitespace you do atoi on a pointer to the whitespace
Just to note; my solution is constrained to only 10 integers from stdin (this can be easily changed), and is also limited to 32-bit integers (which is what I'm guessing is the standard across platforms).
To store larger numbers, change the type of array to uint64_t.
Also this solution doesn't properly support negative numbers, but that could easily be implemented