Answer to Question #20184 in C++ for apoorva

Question #20184
can u explain me the concept of aarays with some examples and how to start programming with them.
Please tell me how to do programming with difficult questions of array concept (1D).
Expert's answer
1-D Arrays

By definition, an array is collection of data that is contiguous in memory. Like any variable, an array must be declared before it is used. There are also numerous types of arrays. The first type is the standard 1 dimensional array. A 1D array contains 1 long row of data.

Here is how to declare a 1D array:

& type name[ capacity ];

where type is the data type of the array (i.e int, char, bool), name is an appropriate name for the array and capacity is the amount of space in the array. A rule is that the capacity cannot be negative or zero and is always an integer.

Notice the syntax of the array declaration. There are square brackets indicating the capacity. The brackets are used to show there is an array declared.

Data in an array is accessed with the [] operator. What goes inside the brackets is the index. By definition, each individual cell in an array is called an index. Indexes of an array ALWAYS BEGIN AT 0 (zero) and end at capacity-1.

Here are some examples of declaring a 1D array:

//declares an array of 5 integers.
//Indices are 0 thru 4:
int myarr[5];

//declares an array of 10 boolean values.
//Indices are 0 thru 9:
bool arr[10];

//declares an array of 1000 characters.
//indices are 0 thru 999:
char ch[1000];

Example 1:
Random numbers and 1D arrays

Below is a short program demonstrating a one-dimensional array:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main(){

& //guarantees a new random each time:

& //declare the array:
int myarr[10];

& //loop to assign the values to the array:
for(int i = 0; i < 10; i++)
& myarr[i] = rand()%10+1;

& //print out the values:
for(int j = 0; j < 10; j++)
& cout << myarr[j] << " ";

return 0;

The program will randomly fill a one-dimensional integer array with numbers from 1 to 10 and then print them out. One possible output is this:

7 4 2 3 5 6 8 9 9 2

Run the program a few times and see the different outputs each time you run it.
Example 2:
Storing scores

This program will allow a user to enter 5 quiz scores to calculate an average. It will then output the average on screen.

#include <iostream>
using namespace std;

int main() {
int scores[5];
int total = 0;

cout << "Enter 5 scores to compute an average."
lt;< "One at a time when prompted: " << endl;

for(int i = 0; i <= 4; i++){
cout << "Enter score[" << i << "]: ";
cin >> scores[i];
cout << endl;

for (i = 0; i <= 4; i++)
total += scores[i];

cout << "The average was: " << total / 5.0 << endl;
return 0;

The first thing the program does is calculate allow the user to input, via a for loop, the 5 quiz scores. Second, it will add the values up to the total variable. Finally, it will display the average on screen at the end.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


Assignment Expert
20.12.12, 15:06

You're welcome. We are glad to be helpful.
If you really liked our service please press like-button beside answer field. Thank you!

20.12.12, 14:31

thank u so much :)

Leave a comment

Ask Your question

New on Blog