1 Replies - 510 Views - Last Post: 28 April 2009 - 08:25 PM Rate Topic: -----

#1 Darbkeshan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-February 09

Program not working

Post icon  Posted 28 April 2009 - 08:00 PM

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

int buildAr(int [], double [], double []); // fuction prototype
void displayAr(int [], double [], double [], int); // fuction prototype
void sortAr (int [], double [], double [], int, char); // functioin prototype
void calcStatByAddr (double [], int, double*, double*, double*, double*);
double sqrt(double);

#define ARSIZE 20 // symbolic constant

int main()
  {
  int size;
  int sub = 0;
  int zid[ARSIZE];
  double pgmavg[ARSIZE];
  double testavg[ARSIZE];
  double minval, maxval, avg, dev;
  
  buildAr (zid, pgmavg, testavg);
  
  size = buildAr (zid, pgmavg, testavg);
  
  
  cout << endl << "the SORTED student information" << endl;
	cout << "--------------------------------------------------------------"
  
  
															"-----------"<< endl;
  char sortchar = 'i';
  sortAr (zid, pgmavg, testavg, size, sortchar);
  displayAr (zid, pgmavg, testavg, size);
  
  sortchar = 'p';
  sortAr (zid, pgmavg, testavg, size, sortchar);
  displayAr (zid, pgmavg, testavg, size);
  
	calcStatByAddr(pgmavg, size, &minval, &maxval, &avg, &dev);
	cout << endl << "The minimum value of the program averages is: " << minval << endl;
	cout << "The maximum value of the program averages is: " << maxval << endl;
	cout << "The average value of the program averages is: " << avg << endl << endl;
	cout << dev;
	
	sortchar = 't';
  sortAr (zid, pgmavg, testavg, size, sortchar);
  displayAr (zid, pgmavg, testavg, size);
  
	calcStatByAddr(testavg, size, &minval, &maxval, &avg, &dev);
	cout << endl << "The minimum value of the test averages is: " << minval << endl;
	cout << "The maximum value of the test averages is: " << maxval << endl;
	cout << "The average value of the test averages is: " << avg << endl << endl;
  
  
  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 pgmavg[] array that holds students 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 pgmavg[] array that holds students program 
			averages
			3. int testavg[] array that holds students test 
			averages
			4. int size which holds the buildAr function 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 < size; sub++)
  {
  overall = (pgmavg[sub] * .4) + (testavg[sub] * .6);
  cout << "  " << zid[sub] << "	   " << setw(6) << fixed <<
  setprecision(2) << testavg[sub] << "		" << setw(4) << fixed
  << setprecision(2) << overall << endl;
  }
}
/***********************************************************
Function: 	void sortAr
  
Use:		Organizes the otput given by the function display 
			in ascending order..
			
Arguments:	1. int zid[] array that holds student zid's
				2.  double pgmavg[] array that holds students program 
			averages
			3. int testavg[] array that holds students test 
			averages
			4. int size which holds the buildAr function from 
			int main()
			
Returns:	nothing
************************************************************/  

void sortAr(int zid [], double pgmavg [], double testavg [], int size, char sortchar)
{
  int i, j, minat;
  
	for(i = 0; i<(size); i++)
	{
	  minat = i;
	  
	for(j = i+1; j < size; j++)
	  {
		if (sortchar == 'i' || sortchar == 'I')
		{
		  if(zid[minat] > zid[j])
		  {
			minat = j;
		  }
		}
		else if (sortchar == 'p' || sortchar == 'P')
		{
		  if(pgmavg[minat] > pgmavg[j])
		  {
		  minat = j;
		  }
		}
		else if (sortchar == 't' || sortchar == 'T')
		{
		  if(testavg[minat] > testavg[j])
		  {
		  minat = 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;
	
}
/***********************************************************/

void calcStatByAddr( double average[], int size, double *m, double *v, double *a, double *d )
{
  double sum;
  sum = 0;
  *m = average[0];
  *v = average[size-1];
  
  for (int i = 0; i < size; i++)
	  sum += average[i];
	  *a = sum/size;
  
  for (int i = 0; i < size; i++)
	  {
	*d += pow(average[i]-*a,2);
	}
	*d = sqrt(*d/(size-1));
	
}



I'm having trouble getting this program to work, and I'm not sure what I'm doing wrong

Any help in the right direction would be greatly appreciated.

here's what it's supposed to do.




Fill the three arrays by calling the buildAr() function.

Sort the three arrays by calling the sortAr() function. The arrays should be sorted based on the program averages.

Display the title "The SORTED student information".

Display the sorted arrays by calling the displayAr() function.

Call the calcStatByAddr() function for the program averages.

Display the results that were passed back by the calcStatByAddr() function with appropriate labels.

Sort the three arrays by calling the sortAr() function. The arrays should be sorted based on the test averages.

Display the title "The SORTED student information".

Display the sorted arrays by calling the displayAr() function.

Call the calcStatByAddr() function for the test averages.

Display the results that were passed back by the calcStatByAddr() function with appropriate labels.






void calcStatByAddr( double [], int, double *, double *, double *, double * );

This function will use the passed in array to calculate and pass back:

the minimum value in the array

the maximum value in the array

the mean (average) of the values in the array

the standard deviation of the values in the array (see "Calculation Note")

It takes as its arguments a floating point array, an integer that holds the number of valid elements in the array, and four pointers to doubles that will be used to pass back the four calculated values.It returns nothing.

Is This A Good Question/Topic? 0
  • +

Replies To: Program not working

#2 Darbkeshan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-February 09

Re: Program not working

Posted 28 April 2009 - 08:25 PM

nevermind, I figured it out
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1