473,545 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 3091
Savage
1,764 Recognized Expert Top Contributor
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
461
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
12
885
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 application. I tried bcmp / a loop for comparison, but it seems they are very slow compared to comparing longs...Any ideas? I tried splitting the array...
41
3912
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 Y contains all the elements that are B but not in A. Z will then have the elements that are in both A and B. One way of doing this is of course...
18
39970
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 I do
8
7506
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 same words but in jumbled order they should match and print only the dissimilar lines.The program also checks for multiple entries of the same line....
10
4970
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 get an error: ---------------- An unhandled exception of type 'System.NullReferenceException' occured in
1
7683
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 is a better way to achieve it for(i=0;i<arrayA.length;i++){ for(j=0;j<arrayB.length;j++){ if(arrayA==arrayB)
9
5348
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 table in a database. I don't know how many rows I am getting so of course I have to Redim inside the loop that does this. Unfortunately, the error...
25
12996
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 *p, void *q) that will take any two pointers and decide whether they can be compared or not. I really can't think how to do this - any...
0
7496
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7428
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
7685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7784
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
6014
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...
0
3485
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1916
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
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
738
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.