Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Example Code

 


Example 1
Sum of Integers

Example 2
x to Power y

Example 3
Mystery Printing

Example 4
Mystery Printing

Example 5
Mystery Printing

Example 6
Conditional Operator

Example 7
Grades

 

Notes:

 

  x++ is shorthand for x = x + 1

  y  *=  x is shorthand for y = y*x

  y += x is shorthand for y = y + x

  y -= x is shorthand for y = y - x

 

 

Example 1

Calculate the sum of the integers from 1 to 10.


#include <iostream>
using namespace std;

int main()
{
    int sum;
    int x;
    x = 1;
    sum = 0;
    while ( x <= 10 )
    {
        sum += x;
        x++;                                        
    }

    cout << "The sum is: " << sum << endl;
    return 0;
}
 

 

 

Example 2

Raise x to the y power.


#include <iostream>
using namespace std;

int main()
{
    int x; 
    int y;
    int i;
    int power;

    i = 1;
    power = 1;

    cout << "Enter base as an integer: ";
    cin >> x;

    cout << "Enter exponent as an integer: ";
    cin >> y;

    while ( i <= y )
    {
        power *= x;
        i++;
    }

    cout << power << endl;
    return 0;
}

 

 

Example 3

Mystery Printing

 

#include <iostream>
using namespace std;

int main()
{
    int y;
    int x = 1;
    int total = 0;

    while ( x <= 10 )
    {
        y = x * x;
        cout << y << endl;
        total += y;
        x++;
    }

    cout << "Total is " << total << endl;
    return 0;
}

 

 

Example 4

Mystery Printing

 

#include <iostream>
using namespace std;

int main()
{
    int count = 1;

    while ( count <= 10 )
    {
        cout << ( count % 2 ? "****" : "++++++++" ) << endl;
        count++;
    }

    return 0;
}
 

 

 

 

Example 5

Mystery Printing


#include <iostream>
using namespace std;

int main()
{
    int row = 10;
    int column;

    while ( row >= 1 )
    {
        column = 1;

        while ( column <= 10 )
        {
            cout << ( row % 2 ? "<" : ">" );
            column++;
        }

        row--;
        cout << endl; // begin new output line
    }

    return 0;
}

 

 

 

Example 6

Conditional Operator - you will not be asked any questions about this approach or asked to use it.

It does, however, appear in a lot of code - my rationale for presenting it.

 

The conditional operator (?:)

 

This is the only ternary operator - it takes 3 operands.

The first operand is a condition

The second operand is the value for the entire condition if the condition is true

The third operand is the value for the entire condition if the condition is false

 

Said another way

 

Write the condition (Boolean) that is to be evaluated - either true or false. a > b, for example

This condition is followed by ? followed by expression if true followed by : followed by expression if false.

 

boolean to evaluate ? result if true : result if false

 

Example

 

#include <iostream>

using namespace std;


int main ()
{
    int a = 5;
    int b = 15;
    cout << (a > b ? "a > b" : "a !> b")<<endl;
    return 0;
}

 

 

 

 

 

Example 7

Grades

 

Version 1: Number of iterations are "hard wired" and uses integer division

 

#include <iostream>

using namespace std;

int main()
{
    int total, gradeCounter, grade, average; //more than one declaration on same line

    total = 0;
    gradeCounter = 1;
   
    while ( gradeCounter <4 )
    {
        cout << "Enter grade: ";
        cin >> grade;
        total = total + grade;
        gradeCounter = gradeCounter + 1;
    }

    average = total / 3; // note the integer division
    cout << "Class average is " << average << endl;

    return 0;
}

 

 

 

Version 2: Number of iterations are "hard wired" and uses casting.

                     Note that the variable total is not used.

 

#include <iostream>

using namespace std;

int main()
{
    int total, gradeCounter, grade;

    total = 0;
    gradeCounter = 1;

    while ( gradeCounter <4 )
    {
        cout << "Enter grade: ";
        cin >> grade;
        total = total + grade;
        gradeCounter = gradeCounter + 1;
    }

    cout << "Class average is " << (double)total / 3<<endl;

    return 0;
}
 

 

Version 3: User is prompted for when to stop entering grades.   Uses casting - variable total is not used.

 

#include <iostream>

using namespace std;

int main()
{
    int total, grade, gradeCounter, response;
    total = 0;
    gradeCounter = 0;
    response = 1;

    while ( response == 1 )
    {
        cout << "Enter grade: ";
        cin >> grade;
        total = total + grade;
        cout <<"More grades to enter? Enter 1 if yes, another number if no"<<endl;
        cin >> response;
        gradeCounter = gradeCounter + 1;
        if (response !=1)
        {
            cout <<"End of Program"<<endl;
        }
    }

    cout << "Class average is " << (double)total / gradeCounter<<endl;
    return 0;
}