In: Computer Science
4. Write a program trace for the pseudocode in Exercise • E4.6, assuming the input values are 4 7 –2 –5 0.
Ans.
first |
value |
minimum |
output |
• E4.6 --> This is the pseudocode
Set a Boolean variable "first" to true.
While another value has been read successfully
If first is true
Set the minimum to the value.
Set first to false.
Else if the value is less than the minimum
Set the minimum to the value.
Print the minimum.
// PLEASE LIKE THE SOLUTION
// FEEL FREE TO DISCUSS IN COMMENT SECTION
// C++ PROGRAM
#include<bits/stdc++.h>
using namespace std;
// main method
int main(){
// now create a bool variable
bool first = true;
// variable to store minimum
double min;
// while loop
while(true){
// read value
double read;
cin>>read;
// unsuccessful read or 0
if(!read)
break;
// now check if true
if(first == true){
min =
read;
first =
false;
}
else{
// check if
less
if(read<min)
min = read;
}
}
// print minimum
cout<<"Minimum = "<<min<<endl;
return 0;
}
// SAMPLE OUTPUT