473,508 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with array invertion

9 New Member
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
3 1679
Ouwet5775
9 New Member
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
3,652 Recognized Expert Specialist
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
Ouwet5775
9 New Member
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

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

Similar topics

9
2107
by: Nathan Rose | last post by:
Here's my problem. I am reading from a text file using this: if (!file_exists($file)) { echo "Don't exist\n"; return false; } $fd = @fopen($file, 'r'); if (!is_resource($fd))
4
5961
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have...
3
1927
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
48
3182
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and...
2
3350
by: Thomas Connolly | last post by:
Anyone know if there is a C# equivallent to: enum { LIFFE_SIZE_AUTOMARKETREF = 15 }; typedef char LiffeAutoMarketReference ; Thanks,
23
2501
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
12
3856
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
5
1182
by: SpreadTooThin | last post by:
If you are deriving a new class from another class, that you must (I assume) know the initializer of the other class. So in myClass import array class myClass(arrary.array): def...
1
2072
by: javabeginner123 | last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.