Sunday, 20 November 2016

Arrays in C++

Arrays:

An array is a group of consecutive memory location with same name and type. An array is the collection of different adjacent  memory locations. All these memory locations have one collective name and type. The memory locations in the array are known as elements of array. The total  numbers of elements in the array is called length of array. Arrays are used to store large amount of similar type of data.

Advantages of Arrays:

Some advantages of arrays are as follows:
  • Arrays can store a large amount of values with single name. 
  • Arrays are used  to process many values easily and quickly. 
  • The values stored in array can be sorted easily. 
  • A search process can be applied on array easily. 

How to access array elements: 

Each element in the array is accessed with reference to its position of location in the array. This position is called subscript or index . The index of the first element is 0 and the index of the last element of array is length-1. Each element of the array has unique index . The values of index is written in brackets / subscript operator ([] ) along with the name of the operator. 

One-Dimensional Array: 

A type of array in which  all elements are arranged in the form of a list is known as  one-dimensional array. It is also called single dimensional array or linear list. It consists of one column or one row.  The  process of specifying array name, length and data type is called array declaration.
Syntax: 
The syntax of the declaring one-dimension array is as follows : 
Data_Type Identifier[Length];

Data_Type: It indicates the data types of the values to be stored in the array. 
Identifier: It indicates the name of the array. 
Length: It indicates total number of elements in the array. It must be a literal constant or symbolic                          constant.
For example: 
int marks[5]; 
In this example an integer array  marks  is declared of five elements. It allocates the five consecutive locations in memory. The index of first element is 0 and index of last element is 4.


Array Initialization: 

The process of assigning values to array elements at the time of declaration is called array initialization. The values are  separated with comma(,) and enclosed in braces ({}). If the number of initialized values are less then the size of array, the remaining array elements are initialized  to zero.
Syntax:
data_type identifier[Length] = {List of values};
Example:
int marks[5] ={85,98,95,80,91};
The above statement declares an array of marks with  5 elements. It also initialized the array with values like at
marks[0] is initialized to 85
marks[1] is initialized to 98
marks[2] is initialized to 95
marks[3] is initialized to 80
marks[4] is initialized to 91

Write a C++ program  that input five integers from the user and store them in an array , display them without using loops.


        #include <iostream.h>
        #include <conio.h>
        int main ()
        {
           int num[5]; 

           cout<< "Enter five  number: " ; 
           cin>> num[0] ;
           cin>> num[1] ;
           cin>> num[2] ;
           cin>> num[3] ;
           cin>> num[4] ;

           cout<< "The values in array are: \n " ; 
           cout<<num[0]<<endl ;
           cout<<num[1]<<endl ;
           cout<<num[2]<<endl ;
           cout<<num[3]<<endl ;
           cout<<num[4]<<endl ;
          
          return 0;
        }


Write a C++ program  that input five integers from the user and store them in an array , display them  using loops.

       
        #include <iostream.h>
        #include <conio.h>
        int main ()
        {
           int num[5] , i ;

           for (i= 0; i 5; i++)
           {
                cout<< "Enter  number: " ;
                cin>> num[i] ;
           }

           cout<< "The values in array are: \n " ;
           for (i= 0; i 5; i++)
           {
                cout<<num[i]<<endl ;
           }

          return 0;
           }


Two-Dimensional Array: 

Two-dimensional array is considered as a table that consist of  rows and columns. It is referred  with the help of  two indexes. One index is used for row and the second index is for column of the element.
Syntax:
 Data_type Identifier[row][column];
Example:
 int marks[5][1]
The above statements declares a two-dimensional array with five rows and 1 column.

Write a C++ program  that input  integers  values from the user and store them in an array of 3 rows and 2 columns.

         
        #include <iostream.h>
        #include <conio.h>
        int main ()
        {
           int num[3][2] , i  , j ;

           for (i= 0; i < 3; i++)
              for (i= 0; i < 2; i++)
              {
                    cout<< "Enter  number: " ;
                    cin>> num[i][j] ;

              }

           for (i= 0; i < 3; i++)
           {
                  for (i= 0; i < 2; i++)
                       cout<<num[i][j];
                 cout<<endl ;
           }


          return 0;
           }


Programs: 

1.  Write a program that inputs ten integers in an array. It displays the number of occurrences of each numbers in the array.

2.  Write a program that inputs the names and monthly salaries of ten employees.The program checks annual salary of each person. if annual salary is greater than or equal to Rs. 2,50000/- then it prints name, salary and a message " Tax to be paid" else it prints name, salary and a message " No Tax".

3.  Write a program that uses two arrays to store the roll number and marks of students.It inputs roll numbers and marks of five students and stores them in corresponding elements of the arrays. The program finally displays the roll number and marks of the students with highest marks.

4.  Write a program that inputs ten integers in an array and counts all prime numbers entered by the user. The program finally displays total number of primes in array.

5.  Write a C++ program that input 5 values from user and stores them in array and display sum and average of these values.


No comments:

Post a Comment

If you have any doubt please let me know

Methods in JAVA Language

 In general, a way may be thanks to perform some task. Similarly, the tactic in Java may be a collection of instructions that performs a sel...