Page 1 of 1

## 1 Replies - 7159 Views - Last Post: 22 April 2009 - 02:37 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=101054&amp;s=4ada8eb19671f085694c5684a40685c0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Darbkeshan

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

Posted 22 April 2009 - 01:26 PM

I need to fix my program and calcStatByAddr function to call by address intead of calling by reference.

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

Logic For main():
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.

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

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
void calcStatByRef (double [], int, double&, double&, double&, double&);
double  sqrt( double );

#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
double minval, valmax, avg, dev;

buildAr (zid, pgmavg, testavg); //calls the buildAr function

size = buildAr (zid, pgmavg, testavg); //sets size to the buildAr function

//displays title for sorted student information
cout << endl << "The SORTED student information" << endl;
cout << "----------------------------------------------------------------"

"-------" << endl;
char sortchar = 'i';
sortAr (zid, pgmavg, testavg, size, sortchar); //calls the sortAr function
displayAr (zid, pgmavg, testavg, size); //calls the displayAr fucntion

sortchar = 'p';
sortAr (zid, pgmavg, testavg, size, sortchar); //calls the sortAr function
displayAr (zid, pgmavg, testavg, size); //calls the displayAr fucntion

calcStatByAddr(pgmavg, size, minval, valmax, avg, dev);
cout << endl << "The minimum value of the program averages is: " << minval <<endl;
cout << "The maximum value of the program averages is: " << valmax << endl;
cout << "The average value of the program averages is: " << avg << endl << endl;
cout << dev;

sortchar = 't';
sortAr (zid, pgmavg, testavg, size, sortchar); //calls the sortAr function
displayAr (zid, pgmavg, testavg, size); //calls the displayAr fucntion

calcStatByAddr(testavg, size, minval, valmax, avg, dev);
cout << endl << "The minimum value of the test averages is: " << minval <<endl;
cout << "The maximum value of the test averages is: " << valmax << 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 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 < size; 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;
}
}
/**********************************************************
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, 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;
}
}
/**********************************************************
Function: void calcStatByRef

Use:	   Takes in array and calculates minimum value, maximum
value, mean, and standard deviation.

Arguments: 1. double average[] array that holds averages
2. int size which holds the buildAr function from int main()
3. double &m
4. double &v
5. double &a
6. double &d

Returns:   nothing
**********************************************************/
void calcStatByAddr( double [], int, double *, double *, double *, double * );
{
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));
}

```

Is This A Good Question/Topic? 0

### #2 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06