Try this C++ program using arrays!

Started by sukishan, Jul 10, 2009, 09:55 PM

Previous topic - Next topic

sukishan

Array

An array is a collection of several data items of the same data type. It consists of several contiguous memory locations storing data. It removes the cumbersome task of defining separate variables for each data item. A single variable can be used to store one or more data items. For example, if you want to store ages of 10 students of a class, you can easily declare an array of integer type of size 10 instead of declaring 10 variables of integer type each storing age of a student. The general form of the single dimension array is:-


type variable_name[size];

The type is the base type of the array and which is the type of each of the element of the array. The size is the no of the elements in the array. The variable_name is the name of the identifier holding the array. For example,

int age [10];



The age is an array of type integer whose size is 10. It can hold 10 data values. The specific element in the array  can be accessed using an index. An index is the offset from the first element of the array. For example, first element has an index of 0 while the third element has an index of 2. The integer age has elements from age[0] to age[9]. Here is a program which illustrates the working of arrays.

#include<iostream>
using namespacestd







#include<iostream>

using namespace std;

int main ()
{
           int age[10];
           int i,sum=0, avg=0;
           int max=0,min=100;
            for(i=0;i<10;i++)
            {
                       cout << "Enter the age of the student  " << i+1 <<endl;
                       cin >> age;
             }
            for(i=0;i<10;i++)

            {

                        sum=sum+age;

                        if(age>max)

                        {

                                    max=age;

                        }

                        if(age<min)

                        {

                                    min=age;

                        }

            }

            avg=sum/10;

            cout << "Average age of the students of the class : " << avg << endl;

            cout << "Maximum age of the student of the class : " << max << endl;

            cout << "Minimum age of the student of the class : " << min << endl;

            return(0);

}

A good beginning makes a good ending