here is some of the code...
Expand|Select|Wrap|Line Numbers
- //*******************Function prototypes*****************************
- void asortm(int[], int); //This is a classic bubble sort in ascending order
- void dsortm(int[], int); //This is a classic bubble sort in descending order
- void parray(int[], int, ofstream &); //prints the array handed it to last int
- int rarray(int[], int, int, ifstream &); //Reads an array, second int is the max size
- int tarray(int[], int, int, int[], int, int); //this function transfer from one array to elements of another
- main()
- //***************************************************************************************************************
- // This program reads three integer arrays a, b, and c. The arrays are then sorted into ascending or descending order
- // and printed. Then the arrays are sorted into an even array d and an odd array e and printed. Finally, the arrays a
- // are mergered into array f and printed.
- // Key varibles in this program are:
- // SIZEA, SIZEB, SIZEC, SIZED, SIZEE, SIZEF integer constants with sizes a, b, c, d, e, f
- // alast, blast, clast int variables with last subscript of a, b, c
- // CA, CB, CC, CD, CE, CF integer subscript with current element in a, b, c, d, e, f
- //
- //*****************************************************************************************************************
- {
- int a[SIZEA], b[SIZEB], c[SIZEC], d[SIZED], e[SIZED], f[SIZEF];
- int alast = -1, blast = -1, clast = -1, dlast= -1, elast =-1, flast= 12;
- int CA=0, CB=0, CC=0, CD=0, CE=0, CF=0;
- int input=0;
- // ***************************Setup In/Output files ***************************************************************
- ofstream outdat1("c:\\OUTDAT1.txt", ios::out);
- ifstream indat1("c:\\INDAT1.txt", ios::in);
- outdat1 << "this is a test of ifstream" << endl;
- //**************************** Read in A ***************************************************************************
- cout << "Enter values for A, -9 will stop" << endl;
- alast = rarray(a, SIZEA,-9,indat1);
- outdat1 << "Array A unsorted " << endl;
- cout << "Array A unsorted " << endl;
- parray(a,alast,outdat1);
- //**************************** Read in B **************************************************************************
- cout << "Enter values for B, -9 will stop" << endl;
- blast = rarray(b, SIZEB,-9,indat1);
- outdat1 << "Array B unsorted " << endl;
- cout << "Array B unsorted " << endl;
- parray(b,blast,outdat1);
- //**************************** Read in C **************************************************************************
- cout << "Enter values for C, -9 will stop" << endl;
- clast = rarray(c, SIZEC,-9,indat1);
- outdat1 << "Array C unsorted " << endl;
- cout << "Array C unsorted " << endl;
- parray(c,clast,outdat1);
- //*************************** Sort and print A *******************************************************************
- asortm(a, alast);
- outdat1 << "Array A sorted " << endl;
- cout << "Array A sorted " << endl;
- parray(a,alast,outdat1);
- //*************************** Sort and print B *******************************************************************
- asortm(b, blast);
- outdat1 << "Array B sorted " << endl;
- cout << "Array B sorted " << endl;
- parray(b,blast,outdat1);
- //*************************** Sort and print C *******************************************************************
- asortm(c, clast);
- outdat1 << "Array C sorted " << endl;
- cout << "Array C sorted " << endl;
- parray(c,clast,outdat1);
- //***************************Sort a,b,c into odd or even arrays d or e********************************************
- for(int h=0; h <= clast; h++)
- if(CC%2==0)//Check for even
- {
- d[CD]=c[CC];//Put c into even array
- CC++;
- }
- else
- {
- e[CE]=c[CC];//Put c into odd array
- }
- cout << "Even array D " << endl;//Print out arrays D & E
- cout <<d[CD]<<endl;
- outdat1 << "Even array D " << endl;//Print out arrays D & E
- outdat1 <<d[CD]<<endl;
- cout << "Odd array E " << endl;
- cout <<e[CD]<<endl;
- outdat1 << "Odd array E " << endl;
- outdat1 <<e[CD]<<endl;
- //*************************** Merge A, C into F *******************************************************************
- while (CA <= alast && CB <= blast) // && CC <= clast)
- {
- if(a[CA] <= b[CB])
- { f[CF] = a[CA]; //A has the larger element move into f
- CF++;
- CA++;
- }
- else
- {
- f[CF] = b[CB]; //B has the larger element move into f
- CF++;
- CB++;
- }
- }
- //************************** Now shift any leftovers from A and C ******
- if(CA <= alast) flast = tarray(a,alast,CA,f,SIZEF,CF);
- else
- flast = tarray(b,blast,CB,f,SIZEF,CF);
- outdat1 << "Merged array F is " << endl;
- cout << "Merged array F is " << endl;
- parray(f,flast,outdat1);
- return 0;
- }
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.. :)