Thread: reverse array logic error

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    reverse array logic error

    I'm having problems getting the last part of this program to work. Help!!
    Code:
    //reverse array prblm 5 pg.290 c++ primer plus
    
    #include <iostream>
    using namespace std;
    
    int fill_array(double ar[], int limit);
    void show_array(const double ar[],int limit);
    void reverse(double ar[],int limit);
    
    int main()
    {
    	int limit=20;
    	double ar[20];
    	limit=fill_array(ar,limit);
    	cout << "You entered:\n";
    	show_array(ar,limit);
    	reverse(ar,limit);
    	cout << "Reversed:\n";
    	show_array(ar,limit);
    	cout << "Reversed again except for first and last elements:\n";
    	reverse(ar+1,limit-1);
    	show_array(ar,limit);
    	return 0;
    }
    
    int fill_array(double ar[], int limit)
    {
    	double temp;
    	int i;
    	for (i=0;i<limit; i++)
    	{
    		cout << "Enter value #"<<(i+1)<<":";
    		if (!(cin>>temp))
    			break;
    		ar[i]=temp;
    	}
    	return i;
    }
    
    void show_array(const double ar[],int limit)
    {
    	for (int i=0;i<limit;i++)
    	{
    		cout << "Array element #" << (i+1) << ":" << ar[i] << endl;
    	}
    }
    
    void reverse(double ar[],int limit)
    {
    	double temp;
    	for (int i=0;i<limit;i++)
    	{
    	if (i==limit-i-1)
    		break;
    	 temp=ar[limit-i-1];
    	ar[limit-i-1]=ar[i];
    	ar[i]=temp;
    	}
    }
    Last edited by rippascal; 03-20-2002 at 02:42 PM.

  2. #2
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Here is a reverse function that I just threw together:

    Code:
    void reverse(double ar[], int limit)
    {
       double *tempArray = new double[limit];
    
       for( int i = 0; i < limit; i++ )
          tempArray[limit - i] = ar[i];
    
       for( int k = 0; k < limit; k++)
          ar[k] = tempArray[k];
    
       delete [] tempArray;
    }
    It should work. Try it out and let me know.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    biosx's system writes all elements backward to a temp array and then rewrites the temp array back to the original array. Should work fine.

    To do it in one read through using switches like rippascal attempted I would just make a slight change to the following line:

    for (int i=0;i<limit;i++)

    I would have the terminating condition be i < limit/2. the dividend will be an integer since the compiler will use integer math. You only need half as many times through loop since you switch 2 elements with every loop. Think about it like this;

    array[3] = {1, 2, 3};
    limit = 3;
    limit/2 = 1;
    for(int i = 0; i < limit/2; i++)
    {
    temp = array[i];
    arrray[i] = array[limit - 1 - i];
    array[limit - 1 - i] = temp;
    }

    First time through loop array[0] is switched with array[2] with the results being 3 2 1 if array elements were read out after first loop through. If you did the loop limit times, then you would be switching back to original positions once i was > limit/2.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    xcept for first, last

    I thought about both methods you all posted for reversing the array. The problem i am having is calling the function to reverse all but the first and last elements.

    reverse(ar+1,limit-1);

    shouldnt this work if double ar[] is equivalent to double * ar. passing then the address ar+1 in affect giving an array starting on the second element where the function just sees an array
    void reverse(double ar[],int limit)

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    solved

    Code:
    int main()
    {
    	int limit=20;
    	double ar[20];
    	limit=fill_array(ar,limit);
    	cout << "You entered:\n";
    	show_array(ar,limit);
    	reverse(ar,limit);
    	cout << "Reversed:\n";
    	show_array(ar,limit);
    	cout << "Reversed again except for first and last elements:\n";
    	reverse(&ar[1],limit-2); //******** this is the solution
    	show_array(ar,limit);
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I would avoid fooling around with pointer math and expand the parameter list passed to the function to include where you want to start the reversal and where you want to end the reversal. There are several ways to do this. Here's one possible way:

    Code:
    void reverse(int  * array, int limit, int startIndex, int endIndex)
    {
      int i;
      int loops = 0;
      int temp;
      if(startIndex >= 0 && endIndex < limit && startIndex < endIndex)
      {
        for(i = startIndex; i < ((endIndex - startIndex)/2); i++)
        {
          temp = array[i];
          array[i] = array[endIndex - loops];
          array[endIndex - loops]= temp;
          loops++;
        }
      }
    }
    HTH

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM