CSC126

Handout #1

Variables and Operations

 

1)       Trace:  Show output window:

 

//First C++ program

//This is the source code saved as a .cpp file

 

#include<iostream>

using namespace std;

 

int main()

 

{

cout << "Good Morning";

return 0;

}

 

 

2)       Order of Operations

 

a)       16 + 3 - 2

b)       20 + 4 / 2

c)       (20 + 4) / 2

d)       5 + 6 * 3 / 2 -1

 

 

3)       Show what is printed by the following program:

 

//Second C++ program

//This is the source code saved as a .cpp file

 

#include<iostream>

using namespace std;

 

int main()

 

{

      cout << 7 + 3;

      cout << 8 / 2 * 4;

      cout << 6 + 18 - 14 / 2 + 87;

cout << 4.2 + 6.6 / 3 - 1 << endl;

cout << -45 % 2 + 3 << endl;

      cout << (67 % 6) + (9 % 10); 

      return 0;

}

 

4)       Evaluate Mixed Expressions

 

a)       4.5 - 3.2 / 2 + 1

b)       7.8 + 15 / 2 - 1.2

 

 

5)       int main()

 

{

      int x,y;

      int sum;

      int product;

      char char1,char2;

     

      x = 15;

      y = 23;

      sum = x + y;

      cout << " x is =  " << x << endl;

      cout << " y is =  " << y  << endl;

      cout << " sum is = " << sum << endl;

      cout << "product is = " << x * y << endl;

      char1 = 'a';

      char2 = 'b';

      sum = char1 + char2;

      cout << "sum is = " << sum << endl;

      return 0;

}

 

 

6)       Write a program that will convert Celsius degrees to Fahrenheit.  Complete the missing statements:

 

//This program inputs a Celsius temperature

//and outputs both the Celsius temperature and its equivalent

//Fahrenheit temperature.

 

#include<iostream>

using namespace std;

 

int main()

 

{

      float celsius;

      float faren;

 

      cin >> celsius;

       //compute the equivalent fahrenheit

 

      cout << "Celsius: " << celsius << " degrees" << endl;

      // print out Fahrenheit now

 

 

      return 0;

}

 

 

7)       Evaluate

 

If x = 7,  y = 5 and z = 15 before each of  the following statement is executed, what are the values after execution?

 

a)       x += 60 - z;

b)       y++;

c)       z += y - 2;