472,145 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

C++ comparing 3 arrays against odd or even and then merging them into one array

I'm working on a school project and I am having a few issues... The program calls for three arrays a,b,c that have to be sorted, then compared to even or odd and stored in arrays d & e, then merge a,b,c into another array f.. I can do two arrays, but I have issues when trying to do all three and when I do the even/odd compare I only get 2 numbers processed.

here is some of the code...
Expand|Select|Wrap|Line Numbers
  1. //*******************Function prototypes*****************************
  2. void asortm(int[], int); //This is a classic bubble sort in ascending order
  3. void dsortm(int[], int); //This is a classic bubble sort in descending order
  4. void parray(int[], int, ofstream &); //prints the array handed it to last int
  5. int rarray(int[], int, int, ifstream &); //Reads an array, second int is the max size
  6. int tarray(int[], int, int, int[], int, int); //this function transfer from one array to elements of another
  7.  
  8. main()
  9. //***************************************************************************************************************
  10. // This program reads three integer arrays a, b, and c. The arrays are then sorted into ascending or descending order
  11. // and printed. Then the arrays are sorted into an even array d and an odd array e and printed. Finally, the arrays a
  12. // are mergered into array f and printed.
  13. // Key varibles in this program are:
  14. // SIZEA, SIZEB, SIZEC, SIZED, SIZEE, SIZEF  integer constants with sizes a, b, c, d, e, f
  15. // alast, blast, clast int variables with last subscript of a, b, c
  16. // CA, CB, CC, CD, CE, CF integer subscript with current element in a, b, c, d, e, f
  17. // 
  18. //*****************************************************************************************************************
  19.  
  20. {
  21.     int a[SIZEA], b[SIZEB], c[SIZEC], d[SIZED], e[SIZED], f[SIZEF];
  22.     int alast = -1, blast = -1, clast = -1, dlast= -1, elast =-1, flast= 12;
  23.     int CA=0, CB=0, CC=0, CD=0, CE=0, CF=0;
  24.     int input=0;
  25. // ***************************Setup In/Output files ***************************************************************
  26.     ofstream outdat1("c:\\OUTDAT1.txt", ios::out);
  27.     ifstream indat1("c:\\INDAT1.txt", ios::in);
  28.     outdat1 << "this is a test of ifstream"  << endl;
  29. //**************************** Read in A ***************************************************************************
  30.     cout << "Enter values for A, -9 will stop" << endl;
  31.     alast = rarray(a, SIZEA,-9,indat1);
  32.     outdat1 << "Array A unsorted " << endl;
  33.     cout << "Array A unsorted " << endl;
  34.     parray(a,alast,outdat1);
  35. //**************************** Read in B **************************************************************************
  36.     cout << "Enter values for B, -9 will stop" << endl;
  37.     blast = rarray(b, SIZEB,-9,indat1);
  38.     outdat1 << "Array B unsorted " << endl;
  39.     cout << "Array B unsorted " << endl;
  40.     parray(b,blast,outdat1);
  41. //**************************** Read in C **************************************************************************
  42.     cout << "Enter values for C, -9 will stop" << endl;
  43.     clast = rarray(c, SIZEC,-9,indat1);
  44.     outdat1 << "Array C unsorted " << endl;
  45.     cout << "Array C unsorted " << endl;
  46.     parray(c,clast,outdat1);
  47. //*************************** Sort and print A *******************************************************************
  48.     asortm(a, alast);
  49.     outdat1 << "Array A sorted " << endl;
  50.     cout << "Array A sorted " << endl;
  51.     parray(a,alast,outdat1);
  52. //*************************** Sort and print B *******************************************************************
  53.     asortm(b, blast);
  54.     outdat1 << "Array B sorted " << endl;
  55.     cout << "Array B sorted " << endl;
  56.     parray(b,blast,outdat1);
  57. //*************************** Sort and print C *******************************************************************
  58.     asortm(c, clast);
  59.     outdat1 << "Array C sorted " << endl;
  60.     cout << "Array C sorted " << endl;
  61.     parray(c,clast,outdat1);
  62. //***************************Sort a,b,c into odd or even arrays d or e********************************************
  63.  
  64.     for(int h=0; h <= clast; h++)
  65.  
  66.     if(CC%2==0)//Check for even
  67.     {    
  68.         d[CD]=c[CC];//Put c into even array
  69.         CC++;
  70.     }
  71.     else 
  72.     {    
  73.         e[CE]=c[CC];//Put c into odd array
  74.  
  75.     }
  76.          cout << "Even array D " << endl;//Print out arrays D & E
  77.          cout <<d[CD]<<endl;
  78.          outdat1 << "Even array D " << endl;//Print out arrays D & E
  79.          outdat1 <<d[CD]<<endl;
  80.          cout << "Odd array E " << endl;
  81.          cout <<e[CD]<<endl;
  82.          outdat1 << "Odd array E " << endl;
  83.          outdat1 <<e[CD]<<endl;
  84.  
  85. //*************************** Merge A, C into F *******************************************************************
  86.     while (CA <= alast && CB <= blast) // && CC <= clast)
  87.     {
  88.         if(a[CA] <= b[CB])
  89.         { f[CF] = a[CA]; //A has the larger element move into f
  90.             CF++;
  91.             CA++;
  92.         }
  93.         else
  94.         {
  95.             f[CF] = b[CB]; //B has the larger element move into f
  96.             CF++;
  97.             CB++;
  98.         }
  99.  
  100.     }    
  101. //************************** Now shift any leftovers from A and C ******    
  102.     if(CA <= alast)    flast = tarray(a,alast,CA,f,SIZEF,CF);
  103.         else
  104.             flast = tarray(b,blast,CB,f,SIZEF,CF);
  105.             outdat1 << "Merged array F is " << endl;
  106.             cout << "Merged array F is " << endl;
  107.             parray(f,flast,outdat1);
  108.         return 0;
  109. }
  110.  
  111.  
the output I get is..

this is a test of ifstream
Array A unsorted
Element[] value
0 12
1 -25
2 13
3 2
4 1
5 0
6 -3
7 8
Array B unsorted
Element[] value
0 14
1 22
2 -5
3 4
4 7
5 9
Array C unsorted
Element[] value
0 18
1 79
2 25
3 15
4 -72
5 63
Array A sorted
Element[] value
0 -25
1 -3
2 0
3 1
4 2
5 8
6 12
7 13
Array B sorted
Element[] value
0 -5
1 4
2 7
3 9
4 14
5 22
Array C sorted
Element[] value
0 -72
1 15
2 18
3 25
4 63
5 79
Even array D
-72
Odd array E
15
Merged array F is
Element[] value
0 -25
1 -5
2 -3
3 0
4 1
5 2
6 4
7 7
8 8
9 9
10 12
11 13
12 14
13 22



any help or suggestions would be helpful.. :)
Sep 13 '07 #1
1 2941
Savage
1,764 Expert 1GB
You are missing for loop brackets:

Expand|Select|Wrap|Line Numbers
  1. for(int h=0; h <= clast; h++)
  2.     {// open bracket added
  3.  
  4.     if(CC%2==0)//Check for even
  5.     {   
  6.         d[CD]=c[CC];//Put c into even array
  7.         CC++;
  8.     }
  9.     else
  10.     {   
  11.         e[CE]=c[CC];//Put c into odd array
  12.     }
  13.          cout << "Even array D " << endl;//Print out arrays D & E
  14.          cout <<d[CD]<<endl;
  15.          outdat1 << "Even array D " << endl;//Print out arrays D & E
  16.          outdat1 <<d[CD]<<endl;
  17.          cout << "Odd array E " << endl;
  18.          cout <<e[CD]<<endl;
  19.          outdat1 << "Odd array E " << endl;
  20.          outdat1 <<e[CD]<<endl;
  21.  
  22. }//close bracket added
Savage
Sep 13 '07 #2

Post your reply

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

Similar topics

11 posts views Thread by Peter | last post: by
12 posts views Thread by Elijah Bailey | last post: by
41 posts views Thread by Odd-R. | last post: by
9 posts views Thread by Anil Gupte | last post: by
25 posts views Thread by J Caesar | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

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.