Thursday, January 3, 2013

Handling Arrays in C++

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.  

For an instance, let's say you need to store five numbers of the same type (let's say integer). one of your option is to declare five variables of type int and initialize values to them. But it's more efficient to use an array of size five and type int to store the same values. Since array elements has indexes, you can identify each value with unique identifiers. 

assume the array is the one shown in the image here. Since the indexes of the arrays in C++ starts from zero, we call them 'zero based arrays'
Consider the following points when creating an array

  • array should have a name (name of the array)
  • array should have a type (what type of values that you are going to store in the array)
  • array should have a size (how many elements that you are going to store in the array)
how to declare the above array (assume the name of the array is marksArr)?

int marksArr [5];

how to initialize values to marksArr?


marksArr [0]=15;
marksArr [1]=25;
marksArr [2]=48;
marksArr [3]=92;
marksArr [4]=89;

how to declare and initialize at the same line?

int marksArr [5]={15,25,48,92,89};


note that the size of an array must be a constant. which means once you declare a size for the array, you can't change it. the maximum number of elements that you can store in the array is equal to the size you declared.

A simple program to explain how to handle arrays in C++

The task that I am giving you is to let the user enter marks of ten students one after the other, get the sum of all the marks and calculate the average. Finally display the results on terminal.

Source Code :-

#include<iostream>
using namespace std;
int main()
{
int const size=10;                 //declaring a constant variable for the size of the array
float marks[size];                 //declaring the array with the constant size 
float sum=0,avg=0;             //declaring two variables to old the sum and the average

for(int c=0;c<10;c++)        //running the for loop ten times to get marks of ten students
{
cout<<"Enter marks of a student "<<c+1<<":"; //prompt the user to enter mark 
cin>>marks[c];                             //get the user input to marks array's cth position

sum=sum+marks[c]; //adding the mark to the sum variable
}
cout<<"You have entered 10 stuednts' marks up to now."<<endl;   //display message
cout<<"Sum is "<<sum<<endl;                                                       //display sum
avg=sum/size;                                                                                //calculate average
cout<<"Average is "<<avg<<endl;                                                 //display average

return 0;
}

output:-



















Multidimensional arrays in C++

Multidimensional arrays can be described as "arrays of arrays". For example, a bidimensional array can be imagined as a bidimensional table made of elements, all of them of a same data type.

Let's say a teacher want to enter marks of students according to their positions seated in the class room. that is each student has a row number and a column number. 

imagine now your array is something that is shown in the image here. Now each mark of the student has a unique identifier which is the row number and the column number together. 
Assume there are five columns and four rows in the class room.


how to declare and assign values to a two dimensional array?

int marksArr[4][5] ={{15,25,78,51,96},
                                 {25,55,74,17,24},
                                 {86,24,98,41,67},
                                 {10,66,78,71,95}
                                }

Now, redo the above shown example to let the user enter the row number and the column number along with the marks of the students and modify the code.

A simple example to demonstrate how two dimensional arrays work

Write a C++ program to get the following output. Assume that you can hard code the values of the array that you use.









Source Code :-


#include<iostream>
using namespace std;

void table(int data[][5],int r,int c);
void print();

int main()
{

int array[4][5]={{10,20,30,40,50},{20,30,40,50,60},{30,40,50,60,70},{40,50,60,70,80}};


table(array,4,5);

return 0;
}

void table(int data[][5],int r,int c)   
{
for(r=0;r<4;r++)
{
for(c=0;c<5;c++)
{
cout<<data[r][c]<<" ";
}
cout<<endl;
print();
}
}

void print()
{
cout<<"________________"<<endl;
}


if you are passing a two dimensional array as a parameter, the size of the row is optional but you must mention the size of the column.

If it is a single dimensional array, you just need a for loop to traverse through the array values but if you have a two dimensional array, you have to use a two level nested for loop (outer for loop for row numbers and inner for loop for column numbers)

No comments:

Post a Comment