c++ using the file name \"integers.dat\" CHA PTER 6/ VO Streams as an Introducti
ID: 3601567 • Letter: C
Question
c++
using the file name "integers.dat"
Explanation / Answer
1)
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
ifstream fin;
int ch;
fin.open("integers.dat");//enter name of file
fin>>ch;
int min=ch;
int max=ch;
while(fin>>ch)
{
if (ch>max)
max=ch;
if(ch<min)
min=ch;
}
cout<<"Largest:"<<max<<endl;
cout<<"Smallest:"<<min;
fin.close();
return 0;
}
2)
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
ifstream fin;
double ch;
fin.open("integers.dat");//enter name of file
double sum=0,n=0;
while(fin>>ch)
{
n=n+1;
sum=sum+ch;
}
cout<<"Average:"<<sum/n<<endl;
fin.close();
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.