Hi,
I am trying to write a program which prompts the user to enter numbers which are then stored into an array. The first number is stored in the 'left' ie: [0], the second number is stored on the 'right' ie: [1], then 'left' again ie: [2], followed by 'right' ie: [3] and so on.
This continues until the array[8] is full.
I have managed this, but then after, using a separate loop I have to loop through the data items in the array and compare the 'Left' and 'Right' values. If 'left'[0] is bigger than 'right'[1], I have to cout "Left is bigger" else "Right is bigger".
My problem is incrementing a variable so that it changes from comparing [0] and [1] to moving onto comparing [2] and [3], and so on until the end of the array is reached.
I believe I can copy each individual stored value from the array into other separate variables, but I'm of the belief that this way is 'wrong'.
Here is my code:-
Code:
int main()
{
// Clear the Gwin window
Gwin.clear();
double arrayname[8];
int loopcount = 0;
int index = 0;
while (loopcount < 5 )
{
Gwin.writeText (10,50,"Enter Left \n"); //Prompt user for number
arrayname[index] = Gwin.getDouble(); //put number into array
index++;
Gwin.writeText (10,70,"Enter Right \n"); //Prompt user for number
arrayname[index] = Gwin.getDouble(); //put number into array
index++;
loopcount++;
}
int indexleft = 0;
int indexright = 1;
//SECOND (PROBLEM) PART BELOW:-
while (loopcount < 10)
{
if (arrayname[indexleft]> arrayname[indexright] )
{
Gwin.writeText (10,100,"Left is bigger");
}
else
{
Gwin.writeText (10,100,"Right is bigger");
}
indexleft ++;
indexright ++;
loopcount++;
}
}
Keyboard.getch();
return 0;
The Gwin lines may be unknown to anyone, as this is a libary we use in class, in my program it is just cout <<"Tell user to enter a value" and cin >> "Enter the value into the array".
So basically, I need to create a loop with compares values within the array, until all the 'lefts' and have been compared to the 'rights'.
Hope this makes sense.
Many thanks.
Swerve.