473,327 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 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 3075
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

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

Similar topics

11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
12
by: Elijah Bailey | last post by:
I have two char arrays of size k. I want to know which one is bigger (exactly like for instance I compare two ints/longs/etc.). What is the fastest way to do this? k <= 10 usually for my...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
18
by: Mike Bartels | last post by:
Hi Everyone! I have two Arrays A and B. Both arrays are byte arrays with 7 bytes each. The contents of array A and B are the same A = {1, 2, 3, 4, 5, 6, 7}; B = {1, 2, 3, 4, 5, 6, 7}; When...
8
by: Frost | last post by:
Hi All, I am a newbie i have written a c program on unix for line by line comparison for two files now could some one help on how i could do word by word comparison in case both lines have the...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
1
by: psmahesh | last post by:
Hi folks, I am comparing two arrays and removing matches from the second array from the first array. Can someone take a look at this code below and mention if this is okay and perhaps if there...
9
by: Anil Gupte | last post by:
I am having a problem using Multidim arrays. I want to create an array which as I understand it is dimensioned as: dim xyz (rows,columns) as String I want to populate it with rows from a...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.