#Arrays

61 messages · Page 1 of 1 (latest)

mystic turtle
#
#include <stdio.h>

int main()
{
    int numbers[100];
    for(int i = 0; i < numbers; i++)
    {
        scanf("%d", &numbers);
    }
    printf("%d", numbers);
}

Why do i recieve

river stumpBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

mystic turtle
#

oh wait i think i might know why

silent pecan
#

That's not how you get an array's length.

mystic turtle
#

Is it because numbers isn't a defined integer?

silent pecan
#

You are having an integer array, not a single integer.

mystic turtle
#

I'm not entirely sure how to acomplish this

#

basically the last value in the input indicates the position in the array that is made

wide cargo
#

Or it could be anything else

mystic turtle
#

Ohh

wide cargo
#

But the main problem here is that you're comparing an integer and a pointer

mystic turtle
#
#include <stdio.h>

int main()
{
    int numbers[100];
    for (int i = 0; i < numbers; i++)
    {
        scanf("%d", numbers);
    }
    printf(" %d", numbers);
}

But how do i store scans into an array?

#

or sorry

#

how do i decide when the loop stops?

wide cargo
#

If you want to get the length of an array you can use this:

#define arr_len(arr) (sizeof (arr) / sizeof (*arr))

If you want to get the length of a pointer there is no way to do so, you need to remember that size yourself

mystic turtle
#

meanwhile the last integer the position in the array to print

wide cargo
#

Read this

mystic turtle
#

WIll do

#

If i for example do somethig like this c for (int i = 0; i < numbers[0]; i++) { scanf("%d", numbers[i]); }

#

Does the loop even start?

#

or does a for loop require the condition to be true to start the loop?

onyx zephyr
onyx zephyr
mystic turtle
#

But will the loop start even if the condition isn't met?

onyx zephyr
#
#include <stdio.h>

int main()
{
    int numbers[100];
    for(int i = 0; i < numbers[0]; i++)
    {
        scanf("%d", &numbers);
    }
    printf("%d", numbers);
}

what value do you think numbers[0] has on the first iteration?

mystic turtle
#

It will be empty

onyx zephyr
#

it's going to be a random value

mystic turtle
#

as numbers[0] doesn't exist until the scanf happens

#

Yeah sorry, an arbitrary number

onyx zephyr
#

do for(int i = 0; i < 100; i++)

mystic turtle
#

Yeah but that wont work either

onyx zephyr
#

secondly, scanf("%d", &numbers); is not correct

mystic turtle
#

i dont want the user to input 100 integers

#

it should be at most 100 integers

#

that's why i set the Array to 100

#

The first integer in the scanf will dictate how many integers there will be

onyx zephyr
#

you can exit the loop in that case

mystic turtle
#

ooh

#

with an if condition?

onyx zephyr
#

scanf will return the number of items in the format string it successfully read, or a negative value on i/o error

onyx zephyr
# mystic turtle with an if condition?

yeah, for example

if(scanf("%d", numbers+i) != 1)
{
    //error
}

also I presume you want to write to the element with offset i from the start of the array

mystic turtle
#

Hmm i don't think doing it that way will work

onyx zephyr
#

what seems to be wrong?

mystic turtle
#

I just want to scan an array of Integers.
And then i will print the n:th integer based on the last integer in the array.

#

My main problem is having the loop stop

#

I want the loop to stop based on the first integer input

#
#include <stdio.h>

int main()
{
    int numbers[100];
    int x = 0;
    scanf("%d", &x);
    for (int i = 0; i < x + 1; i++)
    {
        scanf(" %d", &numbers[i]);
    }

    printf("%d\n", numbers[numbers[x]]); // the last integer in the array will decide which array position to print.
}
#

Okay i think I'm close

#

i am sure that i am scanning correclty now

#

Now i just need a tiny hint on how to print correctly :c

#

Okay i got it to work, i just needed to think out loud (type)

wide cargo
mystic turtle
#

Yes i did?

#

I literally got it to work

wide cargo
#

Then how could you initially do it so wrong, when it's written there so clearly?

mystic turtle
#

!solved