#Arrays
61 messages · Page 1 of 1 (latest)
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.
oh wait i think i might know why
That's not how you get an array's length.
Is it because numbers isn't a defined integer?
You are having an integer array, not a single integer.
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
numbers, when used in a comparison like this, decays to a pointer, so the comparison could look like so:
i < 0xFA78350
```if the `numbers` array starts at `0xFA78350`, or it could be
```c
i < 0xA8730
```if the `numbers` array starts at `0xA8730`
Or it could be anything else
Ohh
But the main problem here is that you're comparing an integer and a pointer
#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?
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
But basically i want the first Int to represent how long the loop will be
meanwhile the last integer the position in the array to print
In this tutorial, you will learn to work with arrays. You will learn to declare, initialize and access array elements of an array with the help of examples. An array is a variable that can store multiple values.
Read this
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?
you're comparing the array with a number
that checking if i is less than the first element in numbers, which isn't what you want
But will the loop start even if the condition isn't met?
#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?
It will be empty
it's going to be a random value
as numbers[0] doesn't exist until the scanf happens
Yeah sorry, an arbitrary number
do for(int i = 0; i < 100; i++)
Yeah but that wont work either
secondly, scanf("%d", &numbers); is not correct
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
scanf will tell you when a number is not inputted
you can exit the loop in that case
scanf will return the number of items in the format string it successfully read, or a negative value on i/o error
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
Hmm i don't think doing it that way will work
what seems to be wrong?
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)
You really didn't read the article I linked you, did you?
Then how could you initially do it so wrong, when it's written there so clearly?
!solved