<< Chapter < Page Chapter >> Page >

set the average to the total divided by the counter

print the average

else

Print “No grades were entered”.

Final step: After coding, we come to the following C++ program.

#include<iostream.h>

#include<iomanip.h>

int main()

{

int total, // sum of grades

gradeCounter, // number of grades entered

grade; // one grade

double average; // number with decimal point for average

// initialization phase

total = 0;

gradeCounter = 0;

// processing phase

cout<<"Enter grade, -1 to end: ";

cin>>grade;

while ( grade != -1 ) {

total = total + grade;

gradeCounter = gradeCounter + 1;

cout<<"Enter grade, -1 to end: ";

cin>>grade;

}

// termination phase

if ( gradeCounter != 0 ) {

average = double ( total ) / gradeCounter;

cout<<"Class average is "<<setprecision( 2 )

<<setiosflags( ios::fixed | ios::showpoint )

<<average<<endl;

}

else

cout<<"No grades were entered"<<endl;

return 0;

}

Arrays

An array is an advanced data type that contains a set of data represented by a single variable name.

An element is an individual piece of data contained in an array.

Array declaration

The syntax for declaring an array is

type name[elements];

Array names follow the same naming conventions as variable names and other identifiers.

Example:

int MyArray[4];

char StudentGrade[5];

The declaration int MyArray[3]; tells the compiler to reserve 4 elements for integer array MyArray.

The numbering of elements within an array starts with an index number of 0. An index number is an element’s numeric position within an array. It is also called a subsript.

Each individual element is referred to as an indexed variable or a subscripted variable because both a variable name and an index or subscrip value must be used to reference the element.

Example:

StudentGrade[0] refers to the first element in the StudentGrade array.

StudentGrade[1] refers to the second element in the StudentGrade array.

StudentGrade[2] refers to the third element in the StudentGrade array.

StudentGrade[3] refers to the fourth element in the StudentGrade array.

StudentGrade[4] refers to the fifth element in the StudentGrade array.

Subscripted variables can be used anywhere scalar variables are valid. Examples using the elements of the MyArray array are:

MyArray[0] = 17;

MyArray[1] = MyArray[0]– 11;

MyArray[2] = 5*MyArray[0];

MyArray[3] = (MyArray[1]+ MyArray[2] –3)/2;

Sum = MyArray[0] +MyArray[1]+MyArray[2] + MyArray[3];

Example

#include<iostream.h>

int main(){

char StudentGrade[5]= {‘A’, ‘B’, ‘C’, ‘D’, ‘F’};

for ( int i = 0; i<5; i++)

cout<<StudentGrade[i]<<endl;

return 0;

}

The output is:

A

B

C

D

F

Example

// Compute the sum of the elements of the array

#include<iostream.h>

int main()

{

const int arraySize = 12;

int a[ arraySize ] = { 1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89, 45 };

int total = 0;

for ( int i = 0; i<arraySize; i++ )

total += a[ i ];

cout<<"Total of array element values is "<<total<<endl;

return 0 ;

}

The output of the above program is as follows :

Total of array element values is 383

Multi-dimensional arrays

The C++ language allows arrays of any type, including arrays of arrays. With two bracket pairs we obtain a two-dimensional array. The idea can be iterated to obtain arrays of higher dimension. With each bracket pair we add another array dimension.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask