Hye all.. Can anybody here give some ideas or repair to make this program better..
I mean a good programming style.. This program is about using POINTER only..:)
//This program find the total rain for each year, the average
//yearly rainfall and the month with the highest and lowest rainfall yearly.
//The program reports the rank of the rainfall order for each year.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//FUNCTION PROTOTYPES
void inputData (double *aYearData);
void calcTotalAve (double *aYearData, double *total, double *avg);
string getHighest (double *aYearData);
string getLowest (double *aYearData);
double *monthRanking(double *aYearData);
string month[]={"January ","February ","March ",
"April ","May ","Jun ",
"July ","August ","September",
"October ","November ","December "};
string tinggi, //to hold the highest rainfall month
rendah; //to hold the lowest rainfall month
int main()
{
double *aYearData,
**rankMain,
*total,
*avg;
//DYNAMIC MEMORY ALLOCATION
avg = new double[12];
total = new double[12];
aYearData = new double[12];
cout << "***************************************************************\n"
<< "This program find the total rain for each year, the average\n"
<< "yearly rainfall and the month with the highest and lowest\n"
<< "rainfall yearly. The program reports the rank of the rainfall\n"
<< "order for each year.\n"
<< "***************************************************************\n\n\n";
for (int i = 1; i < 6; i++)
{
cout << "\a\aYEAR " << i << endl << endl;
inputData(aYearData); //get data from user
cout << "\n\n\nMONTH \t\tRANKING \tRAINFALL" << endl;
cout << "----------------------------------------" << endl;
rankMain[i] = new double; // Rank for each year
for(int j = 0; j < 12; j++)
rankMain[i][j] = monthRanking(aYearData)[j]; //assign the rank to rankMain
for(int g = 0; g < 12; g++)
cout << month[g] << setw(10) << rankMain[i][g]
<< setw(18) << aYearData[g] << endl; //display month, rank, and rainfall data
calcTotalAve(aYearData, total, avg); //calculate the total and average and display them
tinggi = getHighest(aYearData);
cout << endl << "HIGHEST : " << tinggi << endl;
rendah = getLowest(aYearData);
cout << "LOWEST : " << rendah << "\n\n\n\n\n";
}
return 0;
}
//********************************************************
// Definition of function calcTotalAve. *
// This function performs a calculation that calculates *
// the total and the average of rainfall per year *
//********************************************************
void calcTotalAve (double * aYearData, double *total = 0, double *avg = 0)
{
for (int index = 0; index < 12; index++)
*total = *total + *(aYearData+index);
*avg = *total/12;
cout << "\n\nThe total rainfall : " << *total << endl;
cout << "The average : " << *avg << endl;
}
//********************************************************
// Definition of function inputData. *
// This function asks the user to enter the value *
// of rainfall for each month. The parameter, aYearData, *
// is a pointer. *
//********************************************************
void inputData(double * aYearData)
{
cout << "\nENTER DATA \n";
cout << "---------- \n";
for(int index = 0; index < 12; index++)
{
cout << month[index] << " : ";
cin >> *(aYearData+index);
}
}
//********************************************************
// Definition of function getHighest *
// This function accepts a double pointer as arguments, *
// The highest value in the pointer aYearData is returned*
// as a string. *
//********************************************************
string getHighest (double * aYearData)
{
double highest = 0.0;
for (int index = 0; index < 12; index++)
{
if (*(aYearData+index) > highest)
highest = *(aYearData+index);
if (highest == *(aYearData+index))
tinggi = month[index];
}
return tinggi;
}
//********************************************************
// Definition of function getLowest *
// This function accepts a double pointer as arguments, *
// The highest value in the pointer aYearData is returned*
// as a string. *
//********************************************************
string getLowest (double * aYearData)
{
double lowest = *(aYearData+0);
for (int index = 0; index < 12; index++)
{
if (*(aYearData+index) < lowest)
lowest = *(aYearData+index);
if (lowest == *(aYearData+index))
rendah = month[index];
}
return rendah;
}
//********************************************************
// The monthRanking function returns a pointer to an *
// array. It will performs an ascending order selection *
// sort on array, which is an array of pointers. *
//********************************************************
double *monthRanking(double * aYearData)
{
double *level; // Pointer variable declaration
level = new double[12];
for(int index = 0; index < 12; index++)
{
*(level+index)=1;
for(int count = 0; count < 12; count++)
{
if(*(aYearData+index) < *(aYearData+count))
level[index]++ ;
}
}
return level;
}