Single dimensional array

Single Dimensional Array

A list of items can be given one variable name using only one subscript and such a variable is called a single-subscripted variable or a one dimensional array.

Declaration

Like any other variable, array must be declared before they are you used. The general form of Array declaration is 

Syntax:-

Data_type array_name[size of array];

Ex:-int num[10];

>> the data type specifies the type of an element that you will be contained in the array such as int, float ,or char.

>>The size indicate the maximum number of elements that can be stored inside the array.

>> The size of Array should be constant value.

Example:-

float ht[20];

->>declares the height to be an array containing 20 real elements.

int group[15];

->>declares the group as an array to contain a maximum of 15 integers constant .

char ch[12];

->>declares the ch as a character array (string) variable that can hold a maximum of 12 characters.

Ex:-int num[10];

Memory allocation:-

Single-dimensional array are always allocated contiguous blocks of memory.

   This implies that all the elements in an array are always stored next to each other.


memory=no.of bytes × size

Memory allocation in array
  1. Value->num[0]=3
  2. Value->num[1]=6
  3. Value->num[2]=9
Program on single dimensional array
(W.A.P to find min,max of an array)

#include<iostream.h>
#include<conio.h>
class Arr
{
int num[10],i,min,max,sz;
public:
void insz()
{
cout<<"Enter size of array\n";
cin>>sz;
}
void in_arr();
int min_arr();
int max_arr();
};
void Arr :: in_arr()
{
cout<<"Enter values in array\n";
for(i=0;i<sz;i++)
cin>>num[i];
}
int Arr :: min_arr()
{
min=num[0];
for(i=1;i<sz;i++)
{
if(num[i]<min)
min=num[i];
}
cout<<"min="<<min<<endl;
}
int Arr :: max_arr()
{
max=num[0];
for(i=1;i<sz;i++)
{
if(num[i]>max)
max=num[i];
}
cout<<"max="<<max<<endl;
}
int main()
{
Arr a;
clrscr();
a.insz();
a.in_arr();
a.min_arr();
a.max_arr();
getch();
}
Out Put:-


Comments

Popular posts from this blog

History of C language

Lab programs of C

Algorithm Pseudopods Flow-chat