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

Using Visual Studio Input txt file information 3 35.9 147.74 -24.15 -5.62 -25.67

ID: 659768 • Letter: U

Question

Using Visual Studio

Input txt file information

3
35.9 147.74 -24.15 -5.62 -25.67 42.22
-36.32 70.53 49.71 121.29 96.63 94.97
-9.95 -29.67 -49.14 55.2 10.31 82.2

Your program will read in lists of numbers from an input file and, for each list, print, in an output file, all the values, the lowest value, the highest value, and the average value. Details: The input file is called proj2data.txt, and is in the folder S:mackayCS111 The first line of the input file will contain a single positive integer which indicates how many lists of data there are (after the first line) Each list will contain 6 numbers The output file will be called lastname_firstnameproj2results.txt, and will have this format: (using the Example input from above) The numbers on line 1 are: 3, 2.6, -18,12, 5.8, -37 The largest number on line 1 is: 12 The smallest number on line 1 is: -37 The average value for line 1 is: -5.27 The numbers on line 2 are: -5.7, 9, 38, 0, 5, -13.6 The largest number on line 2 is: 38 The smallest number on line 2 is: -13.6 The average value for line 2 is: 5.45 The numbers on line 3 are: 86, -6, -3.84,1.08, 7,1 The largest number on line 3 is: 86 The smallest number on line 3 is: -6 The average value for line 3 is: 14.2 The numbers on line 4 are: 42, -7,13, 4.35,14, 6 The largest number on line 4 is: 42 The smallest number on line 4 is: -7 The average value for line 4 is: 12.1

Explanation / Answer

/*Cpp program that prompts reads an input text file
"proj2data.txt" and writes the values to the output
text file "lastname_firstnameproj2results.txt" with
largest,smallest and average of each line.
*/
#include<iostream>
#include<fstream>
using namespace std;
int main()
{

   //declare float type variables for largest,smallest and average of values in text line
   float largest=0;
   float smallest=0;
   float average=0;

   //declare variables list
   float value;
   int size=6; //number of values in each line
   int lines; //to count lines
   int count=0; //set count to zero
   float total=0; //set total to zero


   //create an input file stream to read text file "proj2data.txt"
   ifstream fin;

   //Note : There is no harddisk drive letter with S in computer.
   //If there is a disk in your computer, replace F drive letter with S drive letter.
   fin.open("F:\mackey\CS111\proj2data.txt");

   //create an output file stream to write to output text file "lastname_firstnameproj2results.txt"
   ofstream fout;
   fout.open("lastname_firstnameproj2results.txt");
  

   if(!fin)
   {
       cout<<"File doesnot exist.Exiting program"<<endl;
       system("pause");
       exit(0);
   }


  

   //read number of lines in the text file
   fin>>lines;

   //loop to run for the lines count
   while(count<lines)
   {
       //read startting value
       fin>>value;
       total+=value;
       //Assum first element is largest and smallest value
       largest=value;
       smallest=value;      
       fout<<"The numbers on line "<<(count+1)<<" are ";
       fout<<value<<",";

       //read 6 values in each line
       for(int index=1;index<=size-1;index++)
       {
           fin>>value;      
           //display only value for last value
           if(index==size-1)
               fout<<value;                  
           //otherwise print comma for output values
           else
               fout<<value<<",";          
              
           //check if next value is greater than assumed largest value
           if(value>largest)
               //then set value to largest
               largest=value;

           //check if value is less than assumed smallest value
           if(value<smallest)
               //then set value to smallest
               smallest=value;


           //add to total
           total+=value;
       }

       fout<<endl;
       fout<<"The largest number on line "<<(count+1)<<" is "<<largest<<endl;
       fout<<"The smallest number on line "<<(count+1)<<" is "<<smallest<<endl;

       average=total/6;
       fout<<"The average number on line "<<(count+1)<<" is "<<average<<endl;
       fout<<"---------------------------------------------------------------"<<endl;

       //reset to zero
       total=0;
       largest=0;
       smallest=0;
       average=0;

       //increment count by one
       count++;
   }

   //close input and output file stream objects
   fin.close();
   fout.close();

   system("pause");
   return 0;
}

Input text file : proj2data.txt
3
35.9   147.74 -24.15 -5.62 -25.67 42.22
-36.32 70.53 49.71 121.29 96.63 94.97
-9.95 -29.67 -49.14 55.2   10.31 82.2


sample output:

The numbers on line 1 are 35.9,147.74,-24.15,-5.62,-25.67,42.22
The largest number on line 1 is 147.74
The smallest number on line 1 is -25.67
The average number on line 1 is 28.4033
---------------------------------------------------------------
The numbers on line 2 are -36.32,70.53,49.71,121.29,96.63,94.97
The largest number on line 2 is 121.29
The smallest number on line 2 is -36.32
The average number on line 2 is 66.135
---------------------------------------------------------------
The numbers on line 3 are -9.95,-29.67,-49.14,55.2,10.31,82.2
The largest number on line 3 is 82.2
The smallest number on line 3 is -49.14
The average number on line 3 is 9.825
---------------------------------------------------------------
Note : output text file will be in the current working directory of your project
Hope this helps you

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