By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,083 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,083 IT Pros & Developers. It's quick & easy.

Need help with array invertion

P: 9
Hey guys
i have been working on a C++ program as a revision for an upcoming final exam.
One of the main point is to know how to create a function that would invert the elements in your array. That one has given me alot of trouble. A tip or a hint would be greatly appriciate it :D

ie: A={1,2,3} -------> A'={3,2,1}

Now i have tried using the fonction on the top of my program but it messed it up for me. and all my output was invalid.

That's the code i have so far and i am sorry in advance if it's difficult to read i have tried making it as esthetic as possible.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3. #define MAX 100
  4.  
  5.  
  6. int sum( int number[], int n)    // Sum fonction of the n elements of the array.
  7. {
  8.     int z=0;
  9.     for( int i=0;i<n;i++)
  10.         z=z+number[i];
  11.     return (z) ;
  12. }
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.     int main()
  20. {
  21.     int number[MAX];
  22.     int index;
  23.     int count=0;
  24.     char more;
  25.  
  26.  
  27.  
  28.     do
  29.     {
  30.         cout<<"Enter a value:    ";
  31.         cin>>number[count];
  32.  
  33.             if (number[count]<0 || number[count]>100)   
  34.             {
  35.                 cout<<"You have entered a wrong value\n";
  36.                 system("pause");
  37.                 continue; 
  38.             }
  39.  
  40.  
  41.         cout<<"Any more items to enter y/n:  ";            
  42.         cin>>more;
  43.         count++;
  44.  
  45.     }
  46.  
  47.     while (more!='n');
  48.     int n;
  49.         cout<<"Enter the number of elements you wish to sum up\n";
  50.         cin>>n;
  51.  
  52.  
  53.     cout<<"The  sum is   "<<sum(number,n)<<endl;
  54.  
  55.     for (index=0; index<count;index++)    // calling the elements of the array
  56.     {
  57.         cout<<endl;
  58.         cout<<"The CONTENT OF ARRAY ELEMENT   "<<  index  <<"    "<<"IS  ";
  59.         cout<<number[index];
  60.     }
  61.  
  62.  
  63.     cout<<endl<<"This Array is composed of "<<count<<"  Elements";        
  64.     cout<<endl<<endl;
  65.     return (0);
  66. }
  67.  

Thanks for any help :)
Oct 30 '06 #1
Share this Question
Share on Google+
3 Replies


P: 9
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3. #define MAX 100
  4. #include <iomanip>
  5.  
  6.  
  7.  
  8.  
  9. int sum( int number[], int n)    // Sum fonction of the n elements of the array.
  10. {
  11.     int z=0;
  12.     for( int i=0;i<n;i++)
  13.         z=z+number[i];
  14.     return (z) ;
  15. }
  16.  
  17.  
  18.  
  19. int find_minimum(int number[], int n)
  20. {
  21.      int minimum=number[0];
  22.      for (int i=0;i<n;i++)
  23.      {
  24.          if(number[i]<minimum)
  25.              minimum=number[i];
  26.      }
  27.      return (minimum);
  28.  }
  29.  
  30.  
  31.  
  32.  int find_maximum(int number[], int n)
  33. {
  34.      int maximum=number[0];
  35.      for (int i=0;i<n;i++)
  36.      {
  37.          if(number[i]>maximum)
  38.              maximum=number[i];
  39.      }
  40.      return (maximum);
  41.  }
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  /*int find_value (const int number[], int v, int target)
  48.  {
  49.      int low=0;
  50.      int mid;
  51.      int high= v-1;
  52.      while(low<=high)
  53.      {
  54.          mid=(low+high)/2;
  55.          if (target>number[mid])
  56.              low=mid+1;
  57.          else if(target<number[mid])
  58.              high=mid-1;
  59.          else
  60.              return (mid);
  61.      }
  62.      return(-1);
  63.  }*/
  64.  
  65.  
  66.  
  67.     int main()
  68. {
  69.     int number[MAX];
  70.     int index;
  71.     int count=0;
  72.     char more;
  73.  
  74.  
  75.  
  76.     do
  77.     {
  78.         cout<<"Enter a value:    ";
  79.         cin>>number[count];
  80.  
  81.             if (number[count]<0 || number[count]>100)    
  82.             {
  83.                 cout<<"You have entered a wrong value\n";
  84.                 system("pause");
  85.                 continue; 
  86.             }
  87.  
  88.  
  89.         cout<<"Any more items to enter y/n:  ";            
  90.         cin>>more;
  91.         count++;
  92.  
  93.     }
  94.  
  95.     while (more!='n');
  96.     int n;
  97.         cout<<"Enter the number of elements you wish Study"<<endl;
  98.         cin>>n;
  99.  
  100.  
  101.     cout<<"\nThe  sum is   "<<sum(number,n)<<endl;
  102.     cout<<"The minimum value is this array is  "<< find_minimum(number,n)<<endl;
  103.     cout<<"The maximum value for this array is  "<<find_maximum(number,n)<<endl;
  104.     cout<<endl <<"This Array is composed of "<<count<<"  Elements"<<endl;
  105. ;// number of elements in the array
  106.  
  107.  
  108.  
  109.  
  110.     for (index=0; index<count;index++)            // calling the elements of the array
  111.     {
  112.         cout<<endl;
  113.         cout<<"The CONTENT OF ARRAY ELEMENT   "<<  index  <<"    "<<"IS  ";
  114.         cout<<number[index];
  115.     }
  116.  
  117.  
  118.     /*cout<<endl<<"This Array is composed of "<<count<<"  Elements";*/        // number of elements in the array
  119.     cout<<endl<<endl;
  120.     return (0);
  121. }
  122.  

Updated code

/free bumb ;) still need help
Oct 30 '06 #2

Ganon11
Expert 2.5K+
P: 3,652
How about this:

Expand|Select|Wrap|Line Numbers
  1. void invertArray(int[] anArray, int size);
  2. // invertArray creates a new array, called temp, with the same size
  3. // as anArray.  Then, using a for loop going from size - 1 to 0, the
  4. // function sets temp[size - the index - 1] to anArray[the index].
  5. // Finally, the function sets anArray equal to temp with a simple
  6. // for loop.
  7. // Precondition: anArray contains x elements, and size is x.
  8. // Postcondition: anArray contains the same values, but in reverse order.
Oct 30 '06 #3

P: 9
How about this:

Expand|Select|Wrap|Line Numbers
  1. void invertArray(int[] anArray, int size);
  2. // invertArray creates a new array, called temp, with the same size
  3. // as anArray.  Then, using a for loop going from size - 1 to 0, the
  4. // function sets temp[size - the index - 1] to anArray[the index].
  5. // Finally, the function sets anArray equal to temp with a simple
  6. // for loop.
  7. // Precondition: anArray contains x elements, and size is x.
  8. // Postcondition: anArray contains the same values, but in reverse order.

Am not near a compiler at the moment but i think that should do it.
Just one question tho, i am creating a new array with the same size as my previous one right ?

And let's say i dont enter any value for my initial array how could i put a safety mechanism that won't call this function ?

Thanks for your help :)
Oct 30 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.