<< 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.

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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