ooo Sprint F 227 AM 68 HW 11 HW11 Graded out of 100 This hom

ooo Sprint F 2:27 AM 68% HW 1-1 HW1-1 (Graded out of 100) This homework will allow you to practice reading from a file, arrays, and loops. Your program should do the following 1. Prompts the user to input a positive integer value x If the value is-1, quit. 2. Then your program opens the \"RandomNumbers3.txt\" file, reads the content of the fie, and does the following 3. In the first iteration of the loop, display the content of the file on the screen t. Stores the values less than or equal toxin the \"lower array, inthe order in which they are readfrom the file. For example, lowerloj will contain the first value found less than or equal to x if no such value is found, nothing is stored in the array, which will be effectively empty. 5. Stores the values greater than xin the \"upper array, in the order in which they are read from the file For example, upperlol will contain the first value found less greater than If no such value is found, nothing is stored in the array, which will be effectively empty. 6. Calculate the averages of the values foreach of the lower and\"upper arrays 7. Print the value x entered, the number of values and the average of the values for each of the Tower and \"upper arrays 8. Print the content of the Tower and upper arrays, or \"Empty is the corresponding array isempty 9 Close the file 10. Loop back to step 1. 1. Requirements Although the numbers are integers, the average ofthe numbers must be calculated as a real average (for example double, and displayed in decimal notation, with four significant digits after the decimal point. Your program must check that the file RandomNumbers3 was opened successfully. If it is not opened successfully, your programmust print an error message and exit. o hard code the file name as\"RandomNumbers3txt\" in your program. You are allowed You are required to implement your program with at least3functions that are called in the main function. Your program must print 10 values per row. Make sure you follow the style requirements, especially regarding thecomment header for functions, to avoid losing points. Refer to the Homework Notes posted on eleaming for details. 2. Notes for Implementation The file RandomNumbers3.txt is posted on elearning, copy it to the same folder vou are running your program

Solution

#include <iostream>
#include <fstream>
using namespace std;

//declare req variables
int upper[100];
int lower[100];

//function to read data frm file
int readFromFile(int x)
{
    int count=0,uCount=0,lCount=0;
    ifstream myfile(\"data.txt\");
    float no;
    while (myfile >> no)
    {
       cout<<no<<\"\\t\";
       if(no>x)
       {
           upper[uCount]=no;
           uCount++;
           count++;
       }
       else
       {
           lower[lCount]=no;
           lCount++;
           count++;
       }
    }

    return count;
}

//function to display upper array elements
void displayUpper()
{
     cout<<\"\ Upper array (values greater than input value) \";
    cout<<\"\ --------------------\ \";
    int count=0;
    float avg=0;
    while(upper[count]!=0)
    {
        avg+=upper[count];
        count++;
    }
    if(count!=0)
    avg/=count;
   cout<<\"Number of values: \"<<count;
   cout<<\"\\tAverage: \"<<avg<<endl;
   for(int i=0;i<count;i++)
   cout<<upper[i]<<\" \";
  
}

//fucntion to display lower array elements
void displayLower()
{
   cout<<\"\ Lower array (values greater than input value) \";
    cout<<\"\ --------------------\ \";
    int count=0;
    float avg=0;
    while(lower[count]!=0)
    {
        avg+=lower[count];
        count++;
    }
    if(count!=0)
    avg/=count;
   cout<<\"\ Number of values: \"<<count;
   cout<<\"\\tAverage: \"<<avg<<endl;
   for(int i=0;i<count;i++)
   cout<<lower[i]<<\" \";
}

//main fucntion which triggers
int main()
{
    int x,count=0;
   cout << \"Enter value of x: \";
   cin>>x;
   if(x!=-1)
   {
       count=readFromFile(x);
   }
   cout<<\"\ Number of values in the file: \"<<count;
   cout<<\"\ Input value entered : \"<<x;
   displayUpper();
   displayLower();

   return 0;
}

Sample Output:

Enter value of x: 3                                                                                                                                                                                                                                     

3       6       3       8       5       8       5                                                                                                                                                                                                       

Number of values in the file: 7                                                                                                                                                                                                                         

Input value entered : 3                                                                                                                                                                                                                                 

Upper array  (values greater than input value)                                                                                                                                                                                                          

--------------------                                                                                                                                                                                                                                    

Number of values: 5     Average: 6.4                                                                                                                                                                                                                    

6 8 5 8 5                                                                                                                                                                                                                                               

Lower array  (values greater than input value)                                                                                                                                                                                                          

--------------------                                                                                                                                                                                                                                    

                                                                                                                                                                                                                                                        

Number of values: 2     Average: 3                                                                                                                                                                                                                      

3 3

 ooo Sprint F 2:27 AM 68% HW 1-1 HW1-1 (Graded out of 100) This homework will allow you to practice reading from a file, arrays, and loops. Your program should
 ooo Sprint F 2:27 AM 68% HW 1-1 HW1-1 (Graded out of 100) This homework will allow you to practice reading from a file, arrays, and loops. Your program should
 ooo Sprint F 2:27 AM 68% HW 1-1 HW1-1 (Graded out of 100) This homework will allow you to practice reading from a file, arrays, and loops. Your program should

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site