473,586 Members | 2,491 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 1683
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
2114
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
5966
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 been reading this ng all semester, and found it very helpful. I just have a stupid problem going on right now. Here's the project: We have to...
3
1935
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 objects of my classes Person(superclass), Student(inherit Person), Teacher(inherit Person) in that array. The name will be the unique key. These...
48
3216
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 "huge" Indy is both as a project, in support, development, and use. But Indy is new to the .net world. Indy is a HUGE library implementing over 120...
2
3354
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
2521
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
3867
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 looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm...
5
1189
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 __init__(self, now here I need to put array's constructor parameters..., then mine): array.array.__init__(self, typecode)
1
2081
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 the description: The monsters are of a very strange kind, called "Bigmon". They have some basic characteristics, like attack and defense power, life...
0
7841
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6617
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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 we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1184
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.