Programing lanugaue is C++
Plan and code a menu-driven modular program utilizing an array
An input file has an unknown number of numeric values(could be empty too).Read the numbers from the input fileand store even numbers in one arrayand odd numbers in another array.Create menu options to
Display count of even numbers, count of odd numbersand the sum
values in each array
Determine the average of each array
Determine the median of each array
Sort each array in ascending order(use bubble sort) and output
results to a file
Display X number of the highest values(for example, 3 highest
values) in each array.Ask a user for the number of the highest
values
Display X number of the lowest values(for example, 7 lowest values)
in each array.Ask a user for the number of the lowest values
A user should be able to run many as many times as a user
wants
Notes :
Clearly label all output
Use switch statement to implement the menu
Watch put for array boundaries.C would not do it for you.
You only need to create one function to determine the average.Just
call it twice to determine the average of each array.The same
applies to the rest of the menu options.
A user should be able to run the menu as many times as a user
wants
Thoroughly test your program.Your grade partially depends on the
quality of your test data.
Must comply with the posted guidelinesand standards
Well document your code(comments)
Cannot use Vector function
ANSWERS.
#include<iostream>
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
//function to find the median
float findMedian(int arr[],int n)
{
sort(arr,arr+n);
if(n%2!=0)
{
return (float)arr[n/2];
}
else
{
return float(arr[(n-1)/2]+arr[n/2])/2.0;
}
}
//function to find the average
float average(int arr[],int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum+=arr[i];
}
return sum/n;
}
//driver function
int main()
{
int arrEven[10000];
int arrOdd[10000];
int a;
static int i=0;
static int j=0;
//creating file object to to read the file data
ifstream infile;
infile.open("testArray.txt");
//checking if file is empty or not
if(infile.peek() == std::ifstream::traits_type::eof())
{
cout<<"File is Empty. Cant't perform operation!!!"<<endl;
}
else
{
//if not empty adding values to even and odd array
if (infile.is_open())
{
while(infile>>a)
{
if(a%2==0)
{
if(i>=10000)
{
cout<<"Array even Is full!!!"<<endl;
break;
}
else
{
arrEven[i++]=a;
}
}
else
{
if(j>=10000)
{
cout<<"Array odd is full!!!"<<endl;
break;
}
else
{
arrOdd[j++]=a;
}
}
}
}
}
while(1)
{
//menu
cout<<"1.Count of even numbers:"<<endl;
cout<<"2.Count of odd numbers:"<<endl;
cout<<"3.Sum of values in each array:"<<endl;
cout<<"4.Determine average of each array:"<<endl;
cout<<"5.Determine median of each array:"<<endl;
cout<<"6.Sort each array in asending order and put to a file:"<<endl;
cout<<"7.Display X number of the highest values:"<<endl;
cout<<"8.Display X number of lowest values:"<<endl;
cout<<"9.exit:"<<endl;
int ch;
cout<<"Enter Choice"<<endl;
cin>>ch;
switch(ch)
{
//count of even number
case 1:
{
cout<<"Count of even numbers are:"<<i<<endl;
break;
}
//count of odd numbers
case 2:
{
cout<<"Count of odd numbers are:"<<j<<endl;
break;
}
//find sum of array
case 3:
{
int sumEven=0;
int sumOdd=0;
for(int k=0;k<i;k++)
{
sumEven+=arrEven[k];
}
for(int k=0;k<j;k++)
{
sumOdd+=arrOdd[k];
}
cout<<"Sum of Even array:"<<sumEven<<endl;
cout<<"Sum of Odd array:"<<sumOdd<<endl;
break;
}
//finds average of the arrays
case 4:
{
float evenAvg=average(arrEven,i);
float oddAvg=average(arrOdd,j);
cout<<"Average of even array:"<<evenAvg<<endl;
cout<<"Average of odd array:"<<oddAvg<<endl;
break;
}
//find the median of the array
case 5:
{
float medianeven=findMedian(arrEven,i);
float medianodd=findMedian(arrOdd,j);
cout<<"Median of even array:"<<medianeven<<endl;
cout<<"Median of odd array:"<<medianodd<<endl;
break;
}
//sorting the array using bubble sort and putting the result in output.txt file
case 6:
{
int temp;
for(int k=i-2;k>=0;k--)
{
for(int h=0;h<=k;h++)
{
if(arrEven[h]>arrEven[h+1])
{
temp=arrEven[h];
arrEven[h]=arrEven[h+1];
arrEven[h+1]=temp;
}
}
}
for(int k=j-2;k>=0;k--)
{
for(int h=0;h<=k;h++)
{
if(arrOdd[h]>arrOdd[h+1])
{
temp=arrOdd[h];
arrOdd[h]=arrOdd[h+1];
arrOdd[h+1]=temp;
}
}
}
ofstream fout;
fout.open("output.txt");
for(int k=0;k<i;k++)
{
fout<<arrEven[k]<<endl;
}
for(int k=0;k<j;k++)
{
fout<<arrOdd[k]<<endl;
}
fout.close();
break;
}
//prints the X higest values from both array
case 7:
{
int x;
cout<<"Enter the value of X:"<<endl;
cin>>x;
int a[10000];
for(int k=0;k<i;k++)
{
a[k]=arrEven[k];
}
sort(a,a+i);
cout<<x<<" Highest values in even array:"<<endl;
for(int k=i-1;k>=i-x;k--)
{
cout<<a[k]<<" ";
}
cout<<endl;
int a1[10000];
for(int k=0;k<j;k++)
{
a1[k]=arrOdd[k];
}
sort(a1,a1+j);
cout<<x<<" Highest values in odd array:"<<endl;
for(int k=j-1;k>=j-x;k--)
{
cout<<a1[k]<<" ";
}
cout<<endl;
break;
}
//print x lowest values from both array
case 8:
{
int x;
cout<<"Enter the value of X:"<<endl;
cin>>x;
int a[10000];
for(int k=0;k<i;k++)
{
a[k]=arrEven[k];
}
sort(a,a+i);
cout<<x<<" Lowest values in even array:"<<endl;
for(int k=0;k<x;k++)
{
cout<<a[k]<<" ";
}
cout<<endl;
int a1[10000];
for(int k=0;k<j;k++)
{
a1[k]=arrOdd[k];
}
sort(a1,a1+j);
cout<<x<<" Lowest values in odd array:"<<endl;
for(int k=0;k<x;k++)
{
cout<<a1[k]<<" ";
}
cout<<endl;
break;
}
case 9:
exit(0);
}
}
}
Get Answers For Free
Most questions answered within 1 hours.