(C++) Please restructure this program so that the driver function/ int main() is
ID: 3843173 • Letter: #
Question
(C++) Please restructure this program so that the driver function/int main() is at the beginning of the program.
#include
#include
using namespace std;
// To create the odd and even array of maximum numbers
#define MAX 100
// function to get the highest element
int getHighest(int *arr, int size) {
int max = arr[0];
for(int i=0; i if(arr[i] > max) {
max = arr[i];
}
}
return max;
}
// function to get the lowest element
int getLowest(int *arr, int size) {
int min = arr[0];
for(int i=0; i if(arr[i] < min) {
min = arr[i];
}
}
return min;
}
// function to get the average element
double getAverage(int *arr, int size) {
double sum = 0;
for(int i=0; i sum += arr[i];
}
return sum/size;
}
// function to print the numbers greater than average
void printHigherNumber(int *arr, int size, double avg) {
for(int i=0; i if(arr[i] > avg) {
cout << arr[i] << " ";
}
}
cout << endl;
}
// Driver function
int main()
{
int even[MAX]={0}, odd[MAX]={0};
int countEven=0, countOdd=0;
std::fstream myfile("data.txt", std::ios_base::in);
// If unable to open file
if (!myfile.is_open())
{
cout << "Invalid input file." << endl;
return 1;
}
int a;
while (myfile >> a)
{
if(a%2==0) {
even[countEven++] = a;
} else {
odd[countOdd++] = a;
}
}
cout << "Highest of Even Array: " << getHighest(even, countEven) << endl;
cout << "Lowest of Even Array: " << getLowest(even, countEven) << endl;
cout << "Average of Even Array: " << getAverage(even, countEven) << endl;
cout << "Numbers higher than average in Even Array: ";
printHigherNumber(even, countEven, getAverage(even, countEven));
cout << endl;
cout << endl;
cout << "Highest of Odd Array: " << getHighest(odd, countOdd) << endl;
cout << "Lowest of Odd Array: " << getLowest(odd, countOdd) << endl;
cout << "Average of Odd Array: " << getAverage(odd, countOdd) << endl;
cout << "Numbers higher than average in Odd Array: ";
printHigherNumber(odd, countOdd, getAverage(odd, countOdd));
cout << endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// To create the odd and even array of maximum numbers
#define MAX 100
// function to get the highest element
int getHighest(int *arr, int size);
int getLowest(int *arr, int size);
double getAverage(int *arr, int size);
void printHigherNumber(int *arr, int size, double avg);
// Driver function
int main()
{
int even[MAX]={0}, odd[MAX]={0};
int countEven=0, countOdd=0;
std::fstream myfile("data.txt", std::ios_base::in);
// If unable to open file
if (!myfile.is_open())
{
cout << "Invalid input file." << endl;
return 1;
}
int a;
while (myfile >> a)
{
if(a%2==0) {
even[countEven++] = a;
} else {
odd[countOdd++] = a;
}
}
cout << "Highest of Even Array: " << getHighest(even, countEven) << endl;
cout << "Lowest of Even Array: " << getLowest(even, countEven) << endl;
cout << "Average of Even Array: " << getAverage(even, countEven) << endl;
cout << "Numbers higher than average in Even Array: ";
printHigherNumber(even, countEven, getAverage(even, countEven));
cout << endl;
cout << endl;
cout << "Highest of Odd Array: " << getHighest(odd, countOdd) << endl;
cout << "Lowest of Odd Array: " << getLowest(odd, countOdd) << endl;
cout << "Average of Odd Array: " << getAverage(odd, countOdd) << endl;
cout << "Numbers higher than average in Odd Array: ";
printHigherNumber(odd, countOdd, getAverage(odd, countOdd));
cout << endl;
return 0;
}
int getHighest(int *arr, int size) {
int max = arr[0];
for(int i=0; i<size;i++) {
if(arr[i] > max) {
max = arr[i];
}
}
return max;
}
// function to get the lowest element
int getLowest(int *arr, int size) {
int min = arr[0];
for(int i=0; i<size;i++) {
if(arr[i] < min) {
min = arr[i];
}
}
return min;
}
// function to get the average element
double getAverage(int *arr, int size) {
double sum = 0;
for(int i=0; i<size;i++) {
sum += arr[i];
}
return sum/size;
}
// function to print the numbers greater than average
void printHigherNumber(int *arr, int size, double avg) {
for(int i=0; i<size;i++){
if(arr[i] > avg) {
cout << arr[i] << " ";
}
}
cout << endl;
}
=======================================
Done, have a look
Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.