# Need help with arrays..

This is a discussion on Need help with arrays.. within the C Programming forums, part of the General Programming Boards category; Lets say I have the array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } and ...

1. ## Need help with arrays..

Lets say I have the array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } and I want to pick 4 of those arrays, somewhere in the middle... like for an example, I want to only use 4 of those elements starting at 3... in that way, it prints out 3, 4, 5, 6... How do i do this using arrays?

2. Just search for the element whose value is 3, then take it and the next three elements.

3. How do i do that, like I'm just learning about arrays...

4. Do you know how to access a particular element of an array?

Do you know how to repeat a statement (or group of statements) repeatedly until some condition is met?

5. also, the lets say the array will change here and there, so I won't be able to search for 3 if there isn't a 3 in the array. I want to pick the "third " element in the array and that is what I want to know, sorry that i was confusing.

6. How would you search for an element with the value of 3? Provide some example code.

I want to pick the "third " element in the array and that is what I want to know, sorry that i was confusing.
The third element in the array is array[2].

7. Code:
```/*
* I want to print out how many elements in the array starting with the index given
* function for pick is pick(int many, int starter, arr[])
* My output with int array[] = {1, 5, 6, 8, 1, 10, 11, 13};
* pick(4, 3, array);
* 8 1 10 11
*```

8. Okay, so now we'll add "do you know how to repeat a statement (or group of statements) a fixed number of times?" to the list.

9. I only know how to do things like "while this is true, do this, until its proven false" or "for(i=0;i<0;i++).. I dunno if this helps.

10. I've been working on it, I guess the for loop is used here and i thought i had it, but... nothing happens.

Code:
```void pick(int many, int starter, int arr[]) {
int i, s, h;
many= h;
starter= s;

for(i=s;s<h;i++)
{
printf("&#37;d ", arr[i]);
fflush(stdout);
}
}```

11. You should give your variables descriptive names. Okay, we can make an exception for i since it is a conventional name for a loop/array index, but s and h are meaningless. Worse still, you assign h to many, but h was not initialised, and you clearly want to use the value of many.

Your loop itself either does not run or does not terminate (in theory) since if s < h, then s is always less than h since neither s nor h change in the loop body.

12. So for loops do things for a fixed number of times, yes. So:
1. How many times do you want your for loop to run?
2. How do you write a for loop that runs that many times?
3. If this is the i'th iteration of the loop (where i = 0, 1, 2, ... ?) what array index do you want to access?
4. How do you access and print that array element?

13. Originally Posted by tabstop
So for loops do things for a fixed number of times, yes. So:
1. How many times do you want your for loop to run?
2. How do you write a for loop that runs that many times?
3. If this is the i'th iteration of the loop (where i = 0, 1, 2, ... ?) what array index do you want to access?
4. How do you access and print that array element?
Well I want to loop it on the number of "many" that is put into the function pick.
I want to access the "starter" part of the array, so if number 3 is put into the function, then i want it to start at that element.
Well, the only thing I can think of is... "%d" or "%s".

14. Its all good you guys... I can't solve it, been thinking about it for like 1.5 hours now.. but I won't give up... I know there is a way to do it and i know its possible. I just need to find out how but thanks though.

15. Do you just mean this?
Code:
```void pick( int many, int starter, int arr[])
{
int i;

for (i = starter; i < starter + many; i++)
printf( "%d ", arr[i]);
printf( "\n");
}```
BTW, programmers often work in groups, so you should work on your communication skills!

Page 1 of 2 12 Last
Popular pages Recent additions