Array issues

I am writing a project in C++ (college entry level), where i need to define an array with 5 items - I then need to populate the array with a list of static variables, and I need to incorporate values from previous projects in these variables and then construct a logic structure to iterate through the array and print out contents of each position in the array. When the index where the calculated field is stored is encountered, i'm supposed to use a logic structure to print a unique print statement indicating the index of where the calculated field is stored. I know how to make the array itself, as well as assign the variables from previous projects to the variables in my array, but i'm lost on the logic structure and how to print the unique statement, as well as how to even make a logic structure that iterates through my array. What I have so far:

int test[5];
test[0] = c; // c is the variable from a previous project
test[1] = num3; // num3 is the variable from a previous project as well
test[2] = 5; // variables 3, 4, and 5 the instructor apparently doesn't really care about so i'm setting these to just random numbers
test[3] = 6;
test[4] = 8;


any help would be hugely appreciated, thank you so much guys.
Last edited on
int test[5];

Array's start at index 0. Meaning, you access the elements by test[0]-test[4]. test[5] doesnt exist.
ooh thanks, forgot about that.
First, arrays start at 0, not 1. So the array as you have it would be out of bounds, you need to go from 0-4 not 1-5.

Use a for loop to iterate through the array.
for (int i = 0; i < 5; i++) {}

Then in the for loop, use the variable i to print the index and test[i] for the value at the index.
how do i do that last part? still pretty new with all this.
how do i do that last part? still pretty new with all this.

Use cout.
Something like this:
cout << "The value at " << index << " is " << array[index] << endl;
Where array is your array (obviously) and index is the loop iterator.
finally finished it 20 minutes before it was due, thanks a ton guys! y'all rock.
Topic archived. No new replies allowed.