Thread: Trouble using a loop to compare array contents

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Trouble using a loop to compare array contents

    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.
    Last edited by Swerve; 01-20-2008 at 01:53 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Here's your code, properly indented. Hopefully, you can see your error:

    Code:
    	while (secondloopcount < 9)
    	{
    		if (arrayname[indexleft]> arrayname[indexright] )
    		{
    			Gwin.writeText (10,100,"Left is bigger");
    
    			else 
    			Gwin.writeText (10,100,"Right is bigger");
    			indexleft ++;
    			indexright ++;
    			secoundloopcount++;
    		}
    	}
    
    	Keyboard.getch();
    	
    
    	return 0;
    Surely you are getting a compile error.

    Todd

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Code:
           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++;
    		}
    	} //end of main bracket
    
    	Keyboard.getch();
    	
    
    	return 0;

    Thank you Todd

    I have placed the { } brackets correctly. (Hope that was the error you were referring to).

    My main problem is the looping through the array contents so I can compare them.

    Thanks!
    Last edited by Swerve; 01-20-2008 at 01:44 PM.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Since there are 4 pairs, and you are looking at a pair at a time, how many times does the second loop need to iterate?

    Todd

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    4 times.

    [0] compared to [1]
    [2] to [3]
    [4] to [5]
    [6] to [7]

    I have just removed the variable 'secondloopcount' and instead continued to use the first 'loopcount' one.

    During the second loop I've set it to stop once (loopcount < 10) is false

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Replies: 7
    Last Post: 05-11-2008, 10:57 AM
  3. for loop again and array values, need help
    By InvariantLoop in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 10:29 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM