426,083 Members | 1,608 Online 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 #include using namespace std; #define MAX 100     int sum( int number[], int n)    // Sum fonction of the n elements of the array. {     int z=0;     for( int i=0;i>number[count];               if (number[count]<0 || number[count]>100)                {                 cout<<"You have entered a wrong value\n";                 system("pause");                 continue;              }             cout<<"Any more items to enter y/n:  ";                     cin>>more;         count++;       }       while (more!='n');     int n;         cout<<"Enter the number of elements you wish to sum up\n";         cin>>n;         cout<<"The  sum is   "<
3 Replies

 P: 9 Expand|Select|Wrap|Line Numbers #include using namespace std; #define MAX 100 #include          int sum( int number[], int n)    // Sum fonction of the n elements of the array. {     int z=0;     for( int i=0;imaximum)              maximum=number[i];      }      return (maximum);  }            /*int find_value (const int number[], int v, int target)  {      int low=0;      int mid;      int high= v-1;      while(low<=high)      {          mid=(low+high)/2;          if (target>number[mid])              low=mid+1;          else if(target>number[count];               if (number[count]<0 || number[count]>100)                 {                 cout<<"You have entered a wrong value\n";                 system("pause");                 continue;              }             cout<<"Any more items to enter y/n:  ";                     cin>>more;         count++;       }       while (more!='n');     int n;         cout<<"Enter the number of elements you wish Study"<>n;         cout<<"\nThe  sum is   "<

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

 P: 9 How about this: Expand|Select|Wrap|Line Numbers void invertArray(int[] anArray, int size); // invertArray creates a new array, called temp, with the same size // as anArray.  Then, using a for loop going from size - 1 to 0, the // function sets temp[size - the index - 1] to anArray[the index]. // Finally, the function sets anArray equal to temp with a simple // for loop. // Precondition: anArray contains x elements, and size is x. // 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 