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;
   }
}
}

http://www.cs.niu.edu/~abyrnes/csci240/pgms/240pgm8.htm

Not sure what this is supposed to mean:

>>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.

If you are supposed to let user decide what gets sorted or what type of sort to do by indicating i/I or p/P then I don't see where you ask for user input.

The first thing I would do in the sorting array is clean up the indentation. I suspect thinks are out of scope by the time you want to do some swapping so things don't get sorted the way you want them to, but there's no way I can tell that for sure without rearranging the blocks of statements, nested loops and control statements in a more readable form, and that's something you should be doing instead of me.

Let me try to re-explain. The function is suppose to take in a character, but is not given by a user. Its just passed to the function in int main(), something along those lines.

#
sortchar = 'i' || 'I';
#
sortAr (zid, pgmavg, testavg, sub, sortchar); //calls the sortAr function

I went ahead and tried to clean up the function so you can hopefully make more sense of it.

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;
	  
      temp = pgmavg[i];
	  pgmavg[i] = pgmavg[minat];
	  pgmavg[minat] = temp;
	  
	  temp = testavg[i];
	  testavg[i] = testavg[minat];
	  testavg[minat] = temp;
    }      
}
}

You are mixing tabs and spaces on the indentation, which leads to problems when posting on Daniweb since a tab is 8 spaces here. What exactly is being sorted here?

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;
	  
             temp = pgmavg[i];
	     pgmavg[i] = pgmavg[minat];
	     pgmavg[minat] = temp;
	  
	     temp = testavg[i];
             testavg[i] = testavg[minat];
	     testavg[minat] = temp;
        }      
    }
}

The array zid is getting sorted in ascending order, then the array pgmavg is suppose to be sorted in ascending order, then the testavg array in ascending order. The zid sorts fine but when I have the code in to sort the pgmavg nothing sorts correctly.

This function doesn't appear to change the arrays at all when I pass it these arrays, no matter what I enter for the sort character. What should it display for the various sort characters and is this legal input for the function? I would definitely look at your brackets and make sure they are where you want them to be. It looks like you are doing a bunch of swaps without any if statements attached to them. I can't think of a sort that does that. What sort are you implementing (insertion, bubble, etc.)?

#include <iostream>
using namespace std;

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;
	  
             temp = pgmavg[i];
	     pgmavg[i] = pgmavg[minat];
	     pgmavg[minat] = temp;
	  
	     temp = testavg[i];
             testavg[i] = testavg[minat];
	     testavg[minat] = temp;
        }      
    }    
}


int main ()
{
    int zid[] = {1,3,2,4};
    double pgmavg[] = {1,4,3,2};
    double testavg[] = {4,2,1,3};
    int size = 4;
    char sortchar;
    cout << "Enter sort char : ";
    cin >> sortchar;
    sortAr (zid, pgmavg, testavg, size, sortchar);
    for (int i = 0; i < size; i++)
    {
        cout << i << '\t' << zid[i] << '\t' << pgmavg[i] << '\t' << testavg[i] << endl;
    }
    
    cin.get ();
    cin.get ();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.