So far in constructing our building we’ve named each brick (variable). That is fine for a
small number of bricks, but what happens once we want to construct something larger?

We would like to point to a stack of bricks and say, “That’s for the left wall. That’s brick 1, brick 2, brick 3. . . .” Arrays allow us to do something similar with variables.An array is a set of consecutive memory locations used to store data.

Each item within the array is named a component . The number of elements in an array is named the dimension of the array. A typical array declaration is:

 // List of data to be sorted and averaged int data_list[3];

This declares data_list to be an array of the three elements data_list[0], data_list [1], and data_list[2], which are separate variables.

To reference an element of an array, you employ variety called the index (the number inside the square brackets []).

C++ is a funny language and likes to start counting at 0, so these three elements are
numbered 0-2.

calcluate total and average of five numbers In Arrays

#include <iostream.h>

 float data[5]; // data to average and total float total; 

 float total;  // the total of the data items float average; 

 float average; // average of the items;

main ()
{
data[0] = 34.0;
data[1] = 27.0;
data[2] = 46.5;
data[3] = 82.0;
data[4] = 22.0;

total = data[0] + data[1] + data[2] + data[3] + data[4];
average = total / 5.0;

cout << "Total "<< total << " Average " << average << 'n';

return (0);
}

Output:Total 211.5 Average 42.3

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: What Is an Array In Cpp

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password