Question

Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input...

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

Homework Answers

Answer #1

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);
                                
                                
                                
                                
                }
                
                
        }
        
        
        
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
user will input a set of integers until 0 is input. When each number is input,...
user will input a set of integers until 0 is input. When each number is input, output whether the number input is it even or odd. Additionally, count the even numbers input. When 0 is input, output the count of the even numbers. Write the code here including the class definition and main function:
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
I am looking for PYTHON code that will display the following: Code a menu-driven application that...
I am looking for PYTHON code that will display the following: Code a menu-driven application that does the following: The user can enter data for a product name, product code, and unit price. An example might be 'Breaburn Apples', 'BAP'. 1.99. After entering the data, it is appended (note I said 'appended') as a single line of text to a text file. Each line of text in the file should represent a single product. There is an option to display...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
Java Create a new Drive class. * Create a Menu class. * The menu class will...
Java Create a new Drive class. * Create a Menu class. * The menu class will use the previous 4 classes you created in GCD, LCM, FACTORIAL, DIGITS The Menu will display a menu that give you the option of selecting: 1) Greatest Common Denominator 2) Lowest Common Multiple 3) Factorial 4) Number of Digits in an Integer Enter 1,2,3 or 4: When the User enter the choice, then the correct function/method is called for the class and asks the...
PLease code a C++ program that prompts a user to enter 10 numbers. this program should...
PLease code a C++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the 10 numbers and the average of the 10 numbers please use file i/o and input measures for Handling Errors in C++ When Opening a File
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat (posted on Canvas)and store it into an array. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be determined. Your program should then prompt the user to enter another whole number between 2 and 20 (you may assume the user enters a valid value) and...
Please code using C# Complete the two methods, highest() and lowest(). Both methods are static with...
Please code using C# Complete the two methods, highest() and lowest(). Both methods are static with a double return type and one integer parameter. Both methods will accept user input of as many numbers as the integer parameter’s value. The highest() method will return the largest number input. The lowest() method will return the smallest number input. Examples: The call highest(3) with user inputs of [5, 60, 2] will return the value of 60 The call lowest(3) with user inputs...
C++ Visual Studio 2019 Write a C++ console application that accepts up to 5 numbers from...
C++ Visual Studio 2019 Write a C++ console application that accepts up to 5 numbers from the user. Display all numbers, the highest, the lowest, and the average of the numbers. Ask the user if they want to continue entering another set of numbers. 1) Use proper naming conventions for your variables and functions. 2) Tell the user what the program is all about. Do NOT start the program with “Enter a number”!! 3) Create an array to store the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT