I have to modify a Program that I previously wrote. I have to modify my sort function so that when a character is passed through it which is coded in main not given by a user it arranges it by that. Where I am at right now neither gets organized correctly for whatever reason. Here is my code and help be appreciated.
/********************************************************************
CSCI 240 - Assignment 7 - Spring 2009
Progammer: Justin R. Smith
Section: 9
TA: Pooja Uppalapati
Date Due: March 26, 2009
Purpose: The program reads in data from a averages file and prints out
the unsorted data information and uses functions to fill arrays
and prints out the sorted data information.
*********************************************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int buildAr(int [], double [], double []); //function prototype
void displayAr(int [], double [], double [], int); //fuction prototype
void sortAr (int [], double [], double [], int, char); //function prototype
#define ARSIZE 20 //symbolic constant
int main()
{
int size; //declare size as an int
int sub = 0; //declare sub as an int sets equal to zero
int zid[ARSIZE]; //declares the array zid using symbolic constant
double pgmavg[ARSIZE]; //declares the array prgavg using symbolic constant
double testavg[ARSIZE]; //declares the array testavg using symbolic constant
char sortchar;
buildAr (zid, pgmavg, testavg); //calls the buildAr function
size = buildAr (zid, pgmavg, testavg); //sets size to the buildAr function
//displays title for unsorted student information
cout << "The UNSORTED student information" << endl;
cout << "----------------------------------------------------------------"
"-------" << endl;
displayAr (zid, pgmavg, testavg, sub); // calls the displayAr function
system ("pause"); //pauses the program until user hits enter
//displays title for sorted student information
cout << endl << "The SORTED student information" << endl;
cout << "----------------------------------------------------------------"
"-------" << endl;
sortchar = 'i' || 'I';
sortAr (zid, pgmavg, testavg, sub, sortchar); //calls the sortAr function
displayAr (zid, pgmavg, testavg, sub); //calls the displayAr fucntion
system ("pause");
sortchar = 'p' || 'P';
sortAr (zid, pgmavg, testavg, sub, sortchar); //calls the sortAr function
displayAr (zid, pgmavg, testavg, sub); //calls the displayAr fucntion
system ("pause");
return 0;
}
/**********************************************************
Function: int buildAr
Use: Builds each of the arrays and returns the subscript
of them to int main().
Arguments: 1. int zid[] array that holds student zid's
2. double prgavg[] array that holds studnets program
averages
3. int testavg[] array that holds students test
averages
Returns: sub which is used for the subscript for the array's
**********************************************************/
int buildAr (int zid [], double pgmavg [], double testavg [])
{
int num;
double num2, num3;
int sub = 0;
ifstream inFile;
inFile.open( "averages.txt" );
if ( inFile.fail() )
{
cout << "input file did not open";
exit(0);
}
while ( inFile )
{
inFile >> num;
zid[sub] = num;
inFile >> num2;
pgmavg[sub] = num2;
inFile >> num3;
testavg[sub] = num3;
sub++;
}
inFile.close();
return (sub);
}
/**********************************************************
Function: void displayAr
Use: Displays the unsorted and sorted grades when called
by int main().
Arguments: 1. int zid[] array that holds student zid's
2. double prgavg[] array that holds studnets program
averages
3. int testavg[] array that holds students test
averages
4. int size which holds the buildAr fucntion from
int main()
Returns: nothing
**********************************************************/
void displayAr(int zid [], double pgmavg [], double testavg [], int size)
{
int sub = 0;
double overall;
char sortchar;
cout << "Student ID Program Average Test Average "<<
"Overall Average" << endl;
for(sub = 0; sub < 18; sub++)
{
overall = (pgmavg[sub] * .4) + (testavg[sub] * .6);
cout << " " << zid[sub] << " " << setw(6) << fixed <<
setprecision(2) << pgmavg[sub] << " " << setw(6) << fixed <<
setprecision(2) << testavg[sub] << " " << setw(4) << fixed
<< setprecision(2) << overall << endl;
}
sortAr (zid, pgmavg, testavg, sub, sortchar);
}
/**********************************************************
Function: void sortAr
Use: Organizes the output given by the function display
in ascending order.
Arguments: 1. int zid[] array that holds student zid's
2. double prgavg[] array that holds studnets program
averages
3. int testavg[] array that holds students test
averages
4. int size which holds the buildAr fucntion from
int main()
Returns: nothing
**********************************************************/
void sortAr(int zid [], double pgmavg [], double testavg [], int size, char sortchar)
{
{
int i, j, min, minat;
for(i = 0; i<(size-1); i++)
{
minat = i;
min = zid[i];
for(j = i+1;j < size; j++)
{
if (sortchar == 'i' || 'I')
if(min > zid[j])
{
minat = j;
min = zid[j];
}
else if (sortchar == 'p' || 'P')
if(min > pgmavg[j])
{
minat = j;
min = pgmavg[j];
}
}
double temp = zid[i];
zid[i] = zid[minat];
zid[minat]=temp;
double temp = pgmavg[i];
pgmavg[i] = pgmavg[minat];
pgmavg[minat] = temp;
temp = testavg[i];
testavg[i] = testavg[minat];
testavg[minat] = temp;
}
}
}