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
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...
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
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 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...
Collapse Write a program that prompts the user to input a positive integer. It should then...
Collapse Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd integer is prime if it is not divisible by any odd integer less than or equal to the square root of the number.) Turn in: Your source code for with The name of your program as a comment at the top...
C++ create a program that: in main: -opens the file provided for input (this file is...
C++ create a program that: in main: -opens the file provided for input (this file is titled 'Lab_HW10_Input.txt' and simply has 1-10, with each number on a new line for 10 lines total) -calls a function to determine how many lines are in the file -creates an array of the proper size -calls a function to read the file and populate the array -calls a function to write out the contents of the array in reverse order *output file should...
C++ Programming   You are to develop a program to read Baseball player statistics from an input...
C++ Programming   You are to develop a program to read Baseball player statistics from an input file. Each player should bestored in a Player object. Therefore, you need to define the Player class. Each player will have a firstname, last name, a position (strings) and a batting average (floating point number). Your class needs to provide all the methods needed to read, write, initialize the object. Your data needs to be stored in an array of player objects. The maximum...
The following code to run as the described program on python. The extra test file isn't...
The following code to run as the described program on python. The extra test file isn't included here, assume it is a text file named "TXT" with a series of numbers for this purpose. In this lab you will need to take a test file Your program must include: Write a python program to open the test file provided. You will read in the numbers contained in the file and provide a total for the numbers as well as the...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT