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

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

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