Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Program HW1-1 (Graded out of 100) This homework wil alow you to practice rea

ID: 3782409 • Letter: C

Question


C++ Program

HW1-1 (Graded out of 100) This homework wil alow you to practice reading from a file, arrays, and loops Your program should do the folowing: he user to input positive integer vakue x, if the value is -1, quit 2. Then your program opens the Random Numbers3 txt" file, reads the content of the file, and does the 3. in the first iteration of the loop, display the content of the file on the screen 4 stores the values less than or equal toxin the "lower" array, in the order in which they are read from the file. For example, lowerfol wil contain the first value found less than or equal to x. f no such value is found, nothing is stored in the array, which wal be effectively empty 5.Stores the values greater than x in the "upper array, in the order in which they are read from the file. For esa mple, upperlol will contain the first value found less greater than x, no such value is found, nothing is stored in the array, which will be effectively empty. 6. Calculate the averages of the values for each of the "lower" and "upper arrays 7. Print the value xentered, the number of values and the average of the values for each of the "lower and "upper" arrays 8 Print the content of the "lower" and "upper" arrays, or "Empty" is the corresponding array is empty 9. Close the file 10 Loop back to step 1. 1. Requirements Although the numbers are integers, the average of the 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.txt" was opened successfully. If it is not opened successfully, your program must print an error message and exit. You are allowed to hard code the file name as "RandomNumbers3.txt" in your program. You are required to implement your program with at least 3 functions that are called in the Your program must print 10 values per row. Make sure you foliow the style requirements, especially regarding the comment header for functions, to avoid losing points. Refer to the Homework Notes posted on eLearning for details. 2. Notes for lmplementation The file RandomNumbers3txt is posted on elearning, Dopy it to the same folder you are running your program.

Explanation / Answer

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>

void printArray(int * arrayContent, int count)
{
   if (count == 0)
   {
       std::cout<<"Empty";
       return;
   }
   for (int i = 0; i < count; i++)
   {
       std::cout<<*(arrayContent + i)<<" ";
   }
  
}

float calcAverage(int * arrayContent, int count)
{
   if (count == 0)
   {
       return 0.0;
   }
   float average = 0.0;
   for (int i = 0; i < count; i++)
   {
       average += *(arrayContent + i);
   }
   return average/count;
}

int _tmain(int argc, _TCHAR* argv[])
{
   int startValue;
   int * lowerArray = new int;
   int * upperArray = new int;
   int i = 0, j = 0;
   std::cout << std::setprecision(4) << std::fixed;
  
   do
   {
       std::cout<<"Enter a positive integer value, enter -1 to quit :";
       std::cin>>startValue;
       if (startValue == -1)
       {
           return 0;
       }

       //
       std::fstream myFile("HW1-2-Input.txt", std::ios_base::in);
       int value;
       std::cout<<"Content of file :"<<std::endl;
       while (myFile >> value)
       {
           std::cout<<value<<" ";
      
           if (value <= startValue)
           {
               *(lowerArray + i++) = value;
           }
           else
           {
               *(upperArray + j++) = value;
           }
       }
       std::cout<<std::endl<<"Number of values in file = "<<i + j<<std::endl;
  
       std::cout<<"Upper array (Values greater than input value)"<<std::endl<<"***********************"<<std::endl;
       std::cout<<"Number of values = "<<j<<" Average ="<<calcAverage(upperArray, j)<<std::endl;
       printArray(upperArray, j);
       std::cout<<std::endl;

       std::cout<<"Lower array (Values less or equal to input value)"<<std::endl<<"***********************"<<std::endl;;
       std::cout<<"Number of values = "<<i<<" Average ="<<calcAverage(lowerArray, i)<<std::endl;
       printArray(lowerArray, i);
       std::cout<<std::endl;
   }while(startValue != -1);

   return 0;
}

// Note : You can create your input file and run above program it is working based on yr requirements.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote