C++ Arrays - II

October 08, 2015 Posted by WithU Technologies
Using Arrays for calculations

#include <iostream>
using namespace std;

int main() {

      int mrp[5];

      int sum=0;

      cout<<"Calculate the Total bill for five products"<<endl;

      for(int x=0;x<5;x++){

            cout<<"Enter the MRP for Product no. "<<x<<"2568 "<<endl;// user enter the MRP of one product in one loop instance                                                

            cin>>mrp[x];// MRP for each product stored here in each loop instance

            sum+=mrp[x];// Each and every 5 MRP values are to be summed up

      }

      cout<<"Your Total product bill for 5 products: "<<sum;// The total sum is to be displayed

}

Output:

Calculate the Total bill for five products

Enter the MRP for Product no. 02568 369

Enter the MRP for Product no. 12568 569

Enter the MRP for Product no. 22568 236

Enter the MRP for Product no. 32568 321

Enter the MRP for Product no. 42568 356

Your Total product bill for 5 products: 1851

·         So, basically we create a total bill calculator for 5 products in a store.

·         Product no. is generated by using a default value with index location as the first digit which change in every loop, thus providing a different product no. in every loop.

·         Then the Price of the product is stored within the x value of mrp array.

·         After every loop that price is summed up by using sum+.

·         Then after the loop completes, an output is to be displayed containing the complete summation of the prices for 5 products.

Multidimensional Array

A multidimensional array is basically made up of rows and columns as like your MS Excel worksheets.

Format:

int variable [No. of Rows] [No. of Columns] = {

{Total No. of Rows},

{Total no. of elements in each row}

}
·         Here we initialize the array with two square brackets, where the first square brackets resemble the no. of rows to be used and the second one is for columns.

·         Then we specify the elements in two different curly braces enclosed within a curly brace, as here we create a table with two rows.

·         And as the table is consist of 3 columns, thus we are able to provide three elements for each row and hence 6 elements for two rows.

Example:
      int age [2] [3] = {

                  {7,6,4},  //1st Row

                  {6,8,7}   //2nd Row

      };

So, now we have a table with 2 rows and 3 columns, thus in total we have 6 elements in this array.



The placement of each element going to take place as shown above.

Index locations
·         In a single ray we already know how index location pinpoints elements.

·         But here we have a multidimensional array and for that the index locations also follow the same rule.

·         As we have two rows here, thus the index location for first row will be 0 and second will be 1 and as this it goes on.

·         And now for columns it also starts from 0 for the first element of any row, 1 for the second element and 2 for the 3rd element.




Red – Index location for rows

Green – Index location for columns

Accessing multidimensional Arrays

To access a multidimensional array element, we simply follow the row and column index location for that element.

As if we want to output 8 which is in the 2nd row and 2nd column. Thus it’s index location is 1 for the row and 1 for the column.

#include <iostream>
using namespace std;

int main() {

      int age [2] [3] = {

                  {7,6,4},

                  {6,8,7}

      };

      cout<<age[1][1];//Outputs 8

}