Folder101   Home Notes Quiz Assigns Info  
Forum Forum
101 Home 101
 
  C++ Level 2

Operators & Expressions - Activity Code

The Function main()

Activity A Function

Activity B Function

Activity C Function

Activity D Function

Activity E Function


  The main() Function

Here is the code for main. Inside the main function - just change the letter A in ActivityA to say B if you want to run ActivityB and C if you want to run AcivityC, etc.

 

#include <iostream.h>

void ActivityA ();  //function prototype
void ActivityB ();
 //function prototype
void ActivityC ();
//function prototype
void ActivityD ();
//function prototype
void ActivityE ();
//function prototype

void main()
{
ActivityA ();
// change this to whichever activity you want to run
}


  Activity A Function

Here is the code for Activity A

 
void ActivityA () {

   int p = 10, q = 5;
   int x = 10, y = 5;

   x = y + 2;
   y = x + 4;

   p = 4 - q;
   q = 4 / p;

   cout << "x is " << x << endl;
   cout << "y is " << y << endl;
   cout << "p is " << p << endl;
   cout << "q is " << q << endl;
}

Note: You must call this function from main() and declare the function protoptye. See my main() function at the top of this page.


  Activity B Function

Here is the code for Activity B

 

void ActivityB () {
   int x = 10, y = 5;
   int p = 10, q = 40;
   int rem1 = 6 % 4;
   int rem2 = 13 % 7;

   x += 2;
   y *= x;
   p -= 2;
   q /= p;

   cout << "x is " << x << endl;
   cout << "y is " << y << endl;
   cout << "p is " << p << endl;
   cout << "q is " << q << endl;
   cout << "rem1 is " << rem1 << endl;
   cout << "rem2 is " << rem2 << endl;

}

Note: You must call this function from main() and declare the function protoptye. See my main() function at the top of this page.


  Activity C Function

Here is the code for Activity C

 

void ActivityC () {

   int ans = 1, count = 2;
   int p = 10, q = 5;
   int x = 2, y = 5;

   ans += count++;

   p = ++q + 3;

   y += 3;

   x *= y--;


   cout << "count is " << count << endl;
   cout << "ans is " << ans << endl;
   cout << "q is " << q << endl;
   cout << "p is " << p << endl;
   cout << "y is " << y << endl;
   cout << "x is " << x << endl;

}

Note: You must call this function from main() and declare the function protoptye. See my main() function at the top of this page.


  Activity D Function

Here is the code for Activity D

 

void ActivityD () {

   cout << "Answer to Q1 = " << 6 + 3 * 4 / 2 << endl;
   cout << "Answer to Q1 = " << (6 + 3) * 4 / 2 << endl;
   cout << "Answer to Q1 = " << -5 * 4 - 6 / 2 << endl;
   cout << "Answer to Q1 = " << -5 * (4 - 6) / 2 << endl;

}

Note: You must call this function from main() and declare the function protoptye. See my main() function at the top of this page.


  Activity E Function

Here is the code for Activity E

 

void ActivityE () {

   short int s = 65;
   int i;
   double d1, d2 = 5.2;
   char ch;

   d1 = (int) d2 + 10;
   cout << "(int)d2 = " << (int)d2 << endl;
   cout << "Answer to Q1 = " << d1 << endl;

   i = (int) d2 * (char)10.34;
   cout << "Answer to Q2 = " << i << endl;

   ch = (char) (s + 3);
   cout << "Answer to Q1 = " << ch << endl;

}

Note: You will get a few warnings from the compiler when you run this code. Don't forget to call this function from main() and declare the function protoptye. See my main() function at the top of this page.