C++ Introduction

Table of Contents Credits

Welcome to the C++ tutorial page. This page is intended for people who have no or little experience with C++. We will begin with a background about programming in C++. C++ is a procedural programming language. Procedural programming languages have program structure  such as: i/o (input/output), assignment statement, conditional statements, iterative statements, and subprograms. C++ contains many data structures. The simple types are called integer, real, char. Other advanced data structures are arrays, structs, and pointers. To get these ideas across let's first make a program. This program will be called average.cc and this program will determine final average and pass/fail status for each student.

To do this program on your own you must first have a wam, glue, or csc account.

If this program feels unfamiliar please click here to start the tutorial at the beginning.

If you have used this tutorial before and would like a different format please click here.

 

SAMPLE PROGRAM IN C++
 
#include <iostream.h>
main () {
  /* Determine final average and pass/fail status for each student.*/
  int NumGr, StudNo, Sum, Gr;
  float Avg;
  cin >> NumGr;
  while (cin >> StudNo) {
    Sum = 0;
    for (int Ct=1; Ct<=NumGr; Ct++) {
      cin >> Gr;
      Sum = Sum + Gr;
    }
    Avg = Sum / NumGr;
    cout << StudNo << " " << Avg;
    if (Avg > 60)
      cout << " Pass" << '\n';
    else
      cout << " Fail" << '\n';
  }
}

To get this program to run, we first must compile the program by typing in the following unix command

% g++ average.cc

Let's input data into this program and trace what this program does step by step.

(<- = Represents a comment about the data)

5 <- This is assigned to the NumGr (Number of Grades) variable

123 90 90 90 90 90 <- Student #1 | These numbers are assigned to the

3678 0 81 45 67 75 <- Student #2 | Gr (Grade) variable and then they are

23 100 50 100 50 75 <- Student #3 | added together to the sum variable

After inserting that data into the program. The output displayed by the cout statements are as follows:

123 90 Pass

3678 53 Fail

23 75 Pass

If this program seems easy reading please click here find the topics you need to review or new lessons to learn.