C

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.

Thanks senpai

Attached: pepe.png (657x527, 13K)

Other urls found in this thread:

ghostbin.com/paste/wpk8e
twitter.com/NSFWRedditImage

std.readInto(IntArray(std.lengthOf()))

ez. don't bother testing it; just give it to your teacher.

>a brainlet trying to learn C
just give up and learn python like every other brainlet

I already did, but C sounds very interesting too and I want to become a better programer

That's not even C, user.

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.

Whoosh?

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.

I'm too lazy to write the program for you, but basically
int arr[argc];
int i;
for(i=0; i

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.

I forgot to mention that first you introduce the number of elements, and then you introduce the array separated by whitespaces

It should be argc - 1

reddit-->?

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.

And you introduce it in different lines

Is this a hackerrank question?

had trouble posting, accidental double post.

First you introduce the number of elements, then on another line you introduce the array elements

Yeah

>Jow Forums can't write a simple C program

Do please teach us
>inb4 not doing your homework
ofc user ofc

Just go through the string and when you encounter a whitespace you do atoi on a pointer to the whitespace

String str = “”;
while(i < s.length()) {
str += s.charAt(i);
if(s.charAt(i) == “ “) {
v[j] = Interger.parseInt(str);
j++;
str = “”;
}
}

like this?

If you can't do that, you're officially retarded
Also count the number of words before and do array = malloc(nb_words * sizeof(int))

That's what I am doing. THe point of my question was to be if someone knew a more elegant method like scanf directly into the array or wtv.

i++
j = 0

This is not python

And now I know.

solutions probably already posted but here you go

Attached: 2019-03-17-215939_685x700_scrot.png (685x700, 59K)

yeah I already finished that part now I am implementing bublesort. Thanks!

NYPA

Is command line argument part of stdin, my negro friend?

Here you go:
ghostbin.com/paste/wpk8e

I've added comments to explain how

Thanks man. I already managed it.

Man this is awesome thanks for taking the time!

No, it's different.
Example : cat reads from stdin
If you do
echo "op is a faggot" | cat
Cat read from stdin.
(Stdin has a filedescriptor of 0)

>misusing const
>using variable length arrays
>atoi
>bubble sort

Attached: fine.gif (259x251, 1.91M)

Attached: 1534908997169.png (1920x1080, 74K)

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

>pepe.png
that's apu, fren

Attached: 1525585183721.png (850x682, 159K)

>1525585183721.png
that's mysoginist cat.png, fren

Attached: mysoginist cat.png (149x125, 21K)

Rule number one: Forget about scanf