Jump to content
xisto Community
Sign in to follow this  
de4thpr00f

Basic C++ : Tutorial 2

Recommended Posts

Outline for Tutorial 2:

a)Pseudo code for Program 2

b)Writing Program 2: the if statement

c)Enrichment:creating readable output

d)Relational operators,compound assignment operators

e)Data types float, double, char

f)Enrichment: debugging

-Summary and Exercises

 

Problem #2: A program is required that determines if or if not a person can register for courses based a specific formula. The person's GPA is plugged into the formula result = formula(GPA) and tested if or if it is not greater than zero. If it is greater than zero, the program will print "Admit". Construct a chart showing various values for grade point average and formula.The formula is as follows:

result = GPA3+7GPA-1

GPA2-GPA + 5

3

 

a) Since we have to construct a chart showing various values, we should use a loop structure. So...

for values of gpa, from 0.00 to 4.00, increasing by increments of 0.50

compute result = formula(GPA)

print GPA, result.

if result is greater than or equal to 0

print "Admit"

* The 0.50 increment is only an example of the increment. Any increment can be used in this program example but for teaching purposes 0.50 was chosen.

 

b)Writing Program 2: The if Statement:

The first few lines in our program will be exactly the same except for the comment. Which will look like this:

//Problem #2://creates a table to evaluate a formula based on values of//GPA in the range from GPA = 0.00 to 4.00 in units of 0.50#include <iostream>using namespace std;int main(){

Now what we need is a declaration of the variables and their data types. The problem already tells us to use the variables GPA and result so all that is left is the data types. So we examine what values GPA and result hold in this program. The GPA values start at 0.00 and go up in increments of .5 but the only data type we have seen so far is int. Declaring GPA as int will not work because data such as 1.5 will have the .5 dropped and be stored as 1.

So now the data type double will be introduced. The data type double values to hold a real number, one that may contain decimal places. Clearly since result comes from computation using GPA, the data type is bound to contain decimals and therefore is also of data type double. So, here is our declaration:

double GPA, result;
Next is our for loop which should look like this:

for (GPA = 0.00; GPA <= 4.00; GPA = GPA + 0.50)
After our for loop we compute the result. For now we'll simply just write the assignment statement as result = formula Since translating the formula to code can be tricky we'll cover it a little later.

After this we print the GPA and the result with this:

cout<< GPA << "	 " << result << endl;
Last is checking if the result is greater than or equal to zero. In C++, a conditional or if statement is used to ask a question or make a decision. If the result is greater than or equal to zero then we print "Admit". If the result is less than zero then we don't print. In either case we want to go to a new line to continue processing the next value of GPA. So our code for it should look like this:

if (result >= 0)		  cout << " Admit";	 cout << endl;
The general form of an if Statement is this:

if(condition)			  // no semicolon here		  stmt-1;			   //end of if statement	 stmt-2;					//next statement
So for our example if the result were to be less than zero; it skips statement 1 and moves onto the next statement.

 

Now onto writing the formula in C++

result = GPA3+7GPA-1

GPA2-GPA + 5

3

By straightforward translation our formula comes out to this:

result = GPA * GPA * GPA + 7 * GPA - 1 / GPA * GPA - GPA + 5 / 3;	 //This is incorrect
The straightforward translation ignores precedence of operations therefore is incorrect. The Arithmetic Precedence

Along with precedence there is the concept of associativity which has priority if two operators with the same precedence are next to each other. So an expression such as a-b+c where both operations have the same precedence, the one on the left is done first because of associativity.

The C++ Arithmetic Precedence Rules with Associativity Rules:

Associativityhighest precedence(done first)	 unary minus	 unary plus	 ++	 --	 right to left												   *	 /	 %				left to rightLowest precedence(done last)						  +	 -				   left to right
So, our translation for the formula is to be changed based on precedence and parenthesis just like in math has higher precedence than multiplication and division. The changed formula is as follows:

result = (GPA * GPA * GPA + 7 * GPA - 1) / (GPA * GPA - (GPA + 5) / 3);	 //correct one
Our entire program for Problem 2 so far is:

//Problem #2://creates a table to evaluate a formula based on values of//GPA in the range from GPA = 0.00 to 4.00 in units of 0.50#include <iostream>using namespace std;int main(){	 double GPA,result;	 for(GPA = 0.00; GPA <= 4.00; GPA = GPA + 0.50) {		  result = (GPA * GPA * GPA + 7 * GPA - 1) /					   (GPA * GPA - (GPA + 5) / 3);		  cout << GPA << "	 " << result << endl;		  if(result >= 0)			   cout<< " Admit" << endl;		  cout <<endl;	 }	 cout << "The Chart is Finished" <<endl	 return 0;}

***section c-f & exercises coming later***

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.