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
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.