Sunteți pe pagina 1din 7

ATGE2053 C++ Programming

Lab 7: Calculating data in a for loop and displaying data using a table with proper
alignments
Student Name: Tiang Ming Kit
ID number: 18WGD00232
Tutorial Group: 1

1) Program

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

int main()
{
int cont,error;
double initial,increment,final,height,velocity,maxh,maxt;
maxh=0;
maxt=0;
system("color f0");//change command page colour

do
{
error = 1,cont=0;//set values

while(error == 1)
{
system("cls");//clear the command page before
cout<<"Enter initial value for table (in hours) : ";
cin>>initial;
cout<<"Enter increment between lines (in hours) : ";
cin>>increment;
cout<<"Enter final value for table (in hours) : ";
cin>>final;
if(initial >48 || initial <0)//check condition
{
cout<<"Invalid initial value!!\n";
}
else if (increment > final)
{
cout<<"Invalid increment value!\n";
}
else if (initial > final)
{
cout<<"Invalid final value!!\n";
}
else
{
error = 0;
}
system("pause");
//Stop the system andpress any button to continue

}
cout<<"\n";
cout<<setw(42)<<"Weather Balloon Information"<<endl;

cout<<setw(10)<<"Time"<<setw(20)<<"Height"<<setw(23)<<"Velocity"
<<endl;

cout<<setw(10)<<"(hr)"<<setw(18)<<"(m)"<<setw(24)<<"(m/s)"<<endl;

for(initial=initial;initial<=final;initial+=increment)
{
height=-0.12*pow(initial,4)+12*pow(initial,3)
-380*pow(initial,2)+4100*initial+220;
velocity=(-0.48*pow(initial,3)+36*pow(initial,2)
-760*initial+4100)/3600;

if(height>maxh)
//Get the biggest value and the time occured
{
maxh=height;
maxt=initial;
}
cout<<setw(10)<<fixed<<setprecision(2)<<initial
<<setw(20)<<fixed<<setprecision(2)<<height

<<setw(23)<<fixed<<setprecision(6)<<velocity<<endl;

}
cout<<"Maximum balloon height was "<<fixed<<setprecision(2)
<<maxh<<" m "<<endl;
cout<<"and it occured at "<<fixed<<setprecision(2)<< maxt
<<" hr. "<<endl;

while(cont==0) // cont value will be 0


{
cout<<"\n\n";
cout<<"Do you want to continue create another table?"
<<endl;//Ask user
cout<<"1.Yes 2.No\nYour choice : ";
cin>>cont;//Put in result
}
}while(cont == 1);//repeat if cont==1,next if cont!=1

system("cls");//Clear the things before


cout<<"Thank you!"<<endl;

}
2) Explanation of the program

The int used for a whole number and the double is used so that the result can having
decimal places.The ‘system("color f0")’ is ued to change the colour of the command
page. The ‘system("cls")’ used to clear the command page before to reset the page.
The ‘system("pause")’ used to pause the program so that the program will not process
automatically if the user did not press any button.

First, the cout is used to telling the user to enter value and the cin is used to save the
user’s value into a place. The next cout is to show the user about the error value that
enter to the program after check the minimum condition to run the next program. After
that the cout is used to show the result tilt it reach a while loop which ask user that
whether he want to continue. Save the value to ‘cont’ using cin. If no , using cout to
give user a message.

Using the ‘setw’ function , the position of the result can be align to a tidy places. The
number of decimal place was set using the function called ‘setprecision’.

With ‘iostream’ library , the cout and cin can be used. The setprecision and setw are
taken from ‘iomanip’. The ‘include cmath’ can support large number of useful
mathematical function such as ‘sqrt’ and ‘pow’.

The first if / else is use for showing the message to user that which value user’s enter
is invalid. The second if / else is used to compare the initial value with the maximum
value.

The for loop is used to run the program repeatedly until the condition is fulfilled. The
first while loop is used to repeat the program until the user enter all the value
correctly. The second while loop is used to determine whether the user need to end
the program or continue to do again.

In the for loop, the condition set to, the first value enter by user , the condition to
finished the loop is the final value is more or equal to initial value , the initial value
add with increment value and save the value to ‘initial’.

3) Output of the program


a) Wrong initial value
b) Wrong increment value

c) Wrong final value

d) Normal value with table


e ) Press 1 to continue next table
f) Press 2 to end the program , and
thanks
4) Flow Chart of the program

S-ar putea să vă placă și