2) Analyze the following problem: #include <iostream> #include <iomanip> #includ
ID: 3630971 • Letter: 2
Question
2) Analyze the following problem:#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//Program name: Extrema
int main(void)
{
int Size, I;
double Extremum1, Extremum2, Number;
fstream InLab83;
InLab83.open("g:\inlab83.txt", ios::in);
InLab83>>Size;
InLab83>>Number;
Extremum1 = Number;
Extremum2 = Number;
I = 1;
while (I <= Size-1)
{
InLab83>>Number;
if (Number < Extremum1)
Extremum1 = Number;
if (Number > Extremum2)
Extremum2 = Number;
I++;
} // while I
cout<<"The extrema are "
<<fixed<<showpoint<<setprecision(2)
<<Extremum1<<" and "<<Extremum2;
InLab83.close();
cin.get(); cin.get();
return 0;
} // main
Problem:Modify the program Extrema by replacing the WHILE-LOOP with a FOR-LOOP. The modified program should give the same output as with WHILE-LOOP. Test your program for correctness.
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//Program name: Extrema
int main(void)
{
int Size, I;
double Extremum1, Extremum2, Number;
fstream InLab83;
InLab83.open("g:\inlab83.txt", ios::in);
InLab83>>Size;
InLab83>>Number;
Extremum1 = Number;
Extremum2 = Number;
for(I = 1; I <= Size-1; I++)
{
InLab83>>Number;
if (Number < Extremum1)
Extremum1 = Number;
if (Number > Extremum2)
Extremum2 = Number;
} // for end
cout<<"The extrema are "
<<fixed<<showpoint<<setprecision(2)
<<Extremum1<<" and "<<Extremum2;
InLab83.close();
cin.get(); cin.get();
return 0;
} // main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.