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

how environment affect demand and supply of commodity ?
Amos Reply
Wht at the criteria for market ?
Amos
what is difference between monitory policy and fiscal policy?
Malik Reply
monetary policy is a policy thrust by National Govt(CBN) to influence government spending, purchase &taxes
Frank
necessity of economics
Pamela Reply
I will say want,choice,opportunity cost,scarcity,scale of preference
Alao
what is monopoly market.How price output are determined under monopoly market
bisham
b) Monopoly market is an impecfect market where s single firm having the innovation to produce a particular commodity.Prices are determined through output since there are no other competitive.
Frank
Monopoly market:firm has market power & does not respond to market price
Frank
Explain the process of price determination under perfect competition market with suitable diagram
bisham Reply
Price determination under perfect competition via this process :firms have no market power to influence price rather firms respond to market price.
Frank
price is different from demand- demand is amount of commodity
Effah Reply
demand is amount /quantity of commodity a potential buyer is willing to buy at a given price at market
Frank
demand is a desire of customer on commodity with the ability to pay it and willing to buy it at given price of commodity
Harika
demand is price of what
Faith Reply
show that shortrun average cost
Baby Reply
what is economics
Mbah Reply
what is money
Mbah
what is money
Mbah
Difine macro economics
agaba
money is a medium of exchange between goods and services,maybe inform of currency.
Wesonga
Economics is study of how human beings strive to satisfy numerous wants using limited available resources.
Wesonga
how do you find the maximum number of workers the firms should employ order to produce where there are increasing returns
Jane
what are implications of computing national income?.
agaba
pl
MUDASIRU
what is the formulae for calculating national income
MUDASIRU
it calculated by value added method
Praveen
classify the production units like agriculture, banking, transport etc
Praveen
money is anything that is generally acceptetable for human
Ogbaji
Estimate the net value added(NVA) at fixed cost by each industrial structure
Praveen
definition of unemployment
Adam Reply
what are the causes of unemployment?
Mbubi Reply
The main causes of unemployment are listed below. 1. Frictional unemployment 2. Cyclical unemployment 3. Structural unemployment
assani
We can also categorize the causes on a broader sense as: 1. Political and 2. Social cause As unemployeement root causes are embaded in this two.
Yonathan
would opportunity cost exist if there was no scarcity?
assani
yes just because the opportunity cost arose when there is Alternative to choose among the alternatives.
BADAMASIU
I am thinking that, if our resources were unlimited, then there wouldn't be any need to forgo some wants. Hence the inexistence if opportunity cost
assani
Politics
Job
politics has done what?
assani
consider time assani
Mary
I'm Emmanuel,...I taught the main cause is the change in gov't.
Emmanuel
...Lack of capital to set up a firm respectively
Emmanuel
🙈
Emmanuel
I would like to bring in Educational levels can also be the cause the cause of the problem respectively
Emmanuel
I think the main causes of unemployment is lack of INFRASTRUCTURAL DEVELOPMENT OVER POPULATION OVER DEPENDENT ON GOVERNMENT LACK OF SELF EMPOWERMENT...
ananti
lack of skills among the new generation is the serious issue.
Vishal
Where I come from , I don't see why education or personal aspects seem to do with unimployment, technically the motivation and eigerness in all works of live is there , dispite the cultural influence and physical bearriors;the thing we lacking is Government Support and open market ethics.
Joe
sorry about that-(repation). We have a over powering ethical political system that's displacing the marketing asspects of economy and causing large scale unemployment right across the board...
Joe
can someone Explain Expansionary Monetary Policy and Contractionary Monetary Policy Using one of the instrument of Monetary Policy? Please am kinda lost here?. ta
Emmanuel Reply
using a graph show the case of substitute and compliment goods
Ade Reply
can anyone give me a simple explanation to Five Sector Macroeconomics?
Emmanuel
Can someone please define what economics is
jason Reply
economics simply is a social science subject that study human behavior.
dajan
economics is a social science which studies human behaviour as a relationship between ends and scarce means that has alternative uses
Alao
Can someone please tell me how to calculate GDP
Emmanuel
emmanual kapal to calculate GDP (Gross Domestic Product) has three method in calculating it (1)income approach (2) expenditure approach (3) value added method
Alao
thanks Alae
Emmanuel
u are welcome
Alao
in basic terms economics is revered to as battery system, it date back to when Men sees the need to exchange sapless goods and produce to gain , either wealth , basic necessities or to establish trading ties for personal benefit or social asspects in terms of coexistence and continuity, future .
Joe
what is the law of demand
Berlinda Reply
keep other thing constant, when the price increases demand decrease when the price decreases demand increases of the commodity.
sj
all things being equal,quantity demanded decrease as price increase and increase as price decrease
Seth
there's practial joke to it ..." the higher the demand ; scarcity, increase in production and drop in quality"... quite the controversy - for example China vs Europe, United States and we are all boxed up in between somewhere...
Joe
Other thing remain constant the low price of commodity the high quantity of commodity and vice versa is true
Baraka
Explain Effective demand
Anita Reply
What is effective demand
Anita
like Modi is in demand...best example of effective demand
Pranav
Don't get you
Anita
Anita you mean you don't get me or who?
Onyeking
level of demand that represents a real intention to purchase by people with the means to pay
Pranav
Difference between extinct and extici spicies
Amanpreet Reply
While the American heart association suggests that meditation might be used in conjunction with more traditional treatments as a way to manage hypertension
Beverly Reply
in a comparison of the stages of meiosis to the stage of mitosis, which stages are unique to meiosis and which stages have the same event in botg meiosis and mitosis
Leah Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





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