C++ Programming
C++ Programming
1) // This program will read in a group of test scores (positive integers from 1 to 100)
// from the keyboard and then calculate and output the average score
// as well as the highest and lowest score. There will be a maximum of 100 scores.
// MANDEEP SINGH
#include <iostream>
using namespace std;
typedef int GradeType[100]; // declares a new data type:
float findAverage (const GradeType, int); // finds average of all grades
int findHighest (const GradeType, int); // finds highest of all grades
int findLowest (const GradeType, int); // finds lowest of all grades
int main()
{
GradeType grades;
int numberOfGrades;
int pos;
float avgOfGrades;
int highestGrade;
int lowestGrade;
pos = 0;
cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
cin>>grades[pos];
pos++;
while (grades[pos-1]!=-99)
{
cin>>grades[pos];
pos++;
}
numberOfGrades=pos-1;
cout<<"Entered grades are:\n\n";
for (int k = 0; k <numberOfGrades ; k++)
{
cout<<grades[k]<<" ";
}
cout<<endl<<endl;
avgOfGrades = findAverage(grades, numberOfGrades);
cout << endl << "The average of all the grades is " << avgOfGrades << endl;
highestGrade = findHighest(grades, numberOfGrades);
cout << endl << "The highest grade is " << highestGrade << endl;
lowestGrade = findLowest(grades, numberOfGrades);
cout << endl << "The Lowest grade is " << lowestGrade << endl<< endl;
return 0;
}
float findAverage (const GradeType array, int size)
{
float sum = 0;
for (int pos = 0; pos < size; pos++)
sum = sum + array[pos];
return (sum / size);
}
int findHighest (const GradeType array, int size)
{
int temp=0;
for (int pos = 0; pos < size; pos++)
{
if(array[pos]>=temp)
temp=array[pos];
}
return (temp);
}
int findLowest (const GradeType array, int size)
{ int temp=array[0];
for (int pos = 0; pos < size; pos++)
{
if(array[pos]<=temp)
temp=array[pos];
}
return (temp);
}
2) // This program will read in prices and store them into a two-dimensional array.
// It will print those prices in a table form.
// MANDEEP SINGH
#include <iostream>
#include <iomanip>
using namespace std;
const int MAXROWS = 10;
const int MAXCOLS = 10;
typedef float PriceType[MAXROWS][MAXCOLS];
void getPrices(PriceType, int&, int&);
void printPrices(PriceType, int, int);
int main()
{
int rowsUsed;
int colsUsed;
PriceType priceTable;
getPrices(priceTable, rowsUsed, colsUsed);
printPrices(priceTable, rowsUsed, colsUsed);
system("pause");
return 0;
}
void getPrices(PriceType table, int& numOfRows, int& numOfCols)
{
cout << "\nPlease input the number of rows from 1 to "<< MAXROWS << endl;
cin >> numOfRows;
cout << "\nPlease input the number of columns from 1 to "<< MAXCOLS << endl;
cin >> numOfCols;
for (int row = 0; row < numOfRows; row++)
{
for (int col = 0; col < numOfCols; col++)
{
cout<<"\nPlease input the price of an item with 2 decimal places: \n";
cin>>table[row][col];
}
}
}
void printPrices(PriceType table, int numOfRows, int numOfCols)
{
cout << fixed << showpoint << setprecision(2);
cout <<endl<<" ";
for (int row = 0; row < numOfRows; row++)
{
for (int col = 0; col < numOfCols; col++)
{
cout<<table[row][col]<<" ";
}
cout <<endl<<" ";
}
cout<<endl;
}
3)
#include <iostream>
#include <iomanip>
using namespace std;
typedef int Temp[50];
float findAverage (const Temp, int);
float findHighest (const Temp, int);
float findLowest (const Temp, int);
int main()
{
Temp temp;
int counttemp;
int pos;
float avgtemp;
float hightemp;
float lowtemp;
pos = 0;
cout << "Please input the number of temperatures to be read" << endl;
cin>>counttemp;
cout<<endl;
while (pos<counttemp)
{
cout<<" Input temperature "<<pos+1<<":"<<endl;
cin>>temp[pos];
pos++;
}
cout << fixed << showpoint << setprecision(2);
avgtemp = findAverage(temp, counttemp);
cout << endl << "The average of all the temperatures is " << avgtemp << endl;
hightemp = findHighest(temp, counttemp);
cout << endl << "The highest temperature is " << hightemp << endl;
lowtemp = findLowest(temp, counttemp);
cout << endl << "The Lowest temperature is " << lowtemp << endl<< endl;
system("pause");
return 0;
}
float findAverage (const Temp array, int size)
{
float sum = 0;
for (int pos = 0; pos < size; pos++)
sum = sum + array[pos];
return (sum / size);
}
float findHighest (const Temp array, int size)
{
int temp=0;
for (int pos = 0; pos < size; pos++)
{
if(array[pos]>=temp)
temp=array[pos];
}
return (temp);
}
float findLowest (const Temp array, int size)
{
int temp=array[0];
for (int pos = 0; pos < size; pos++)
{
if(array[pos]<=temp)
temp=array[pos];
}
return (temp);
}