Structured Data Type: Arrays

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 23

CHAPTER 12

STRUCTURED DATA
TYPE
ARRAYS
INTRODUCTION
An array is a collection of variables
of same type that are referenced by a
common name .
C++ allows more type of data i.e its
derived data type.(integer and floating
point).
TYPES OF ARRAYS

1.Single dimensional arrays


2. Multi-dimensional arrays(two-dimensional
arrays)
1.Single dimensional array
It is simplest type of array. C++
array’s index numbering starts with 0.
An array must be defined before it
can be used to store information . Ex.
Type array-name[size] ;
Base Type:
The data type of array elements is
known as the base type of the array.
Sample Program
void main()
{
float marks[10];
for(int i=0;i<10;i++)
{ cout<<“Enter marks of student”<<i+1<<“\n”;
cin>>marks[i];
}
cout<<“\n”;
for(i=0;i<10;i++)
cout<<“marks[“<<i<<“]=“<<marks[i]<<“\n”;
getch();
}
Searching Operation
void main()
{ int A[20],size,i,flag=0,num,pos;
cout<<“\nEnter the number of elements in the array:”;
cin>>size;
cout<<“\nEnter the elements of array(in ascending order):”;
for(i=0;i<size;i++)
{ cin>>A[i];}
cout<<“\nEnter the element to be searched for:”;
cin>>num;
for(i=0;i<size;i++)
if(A[i]==num)
{
flag=1;
pos=i;
break;
}
Searching Operation
if(flag==0)
cout<<“\n Element not found”;
else
cout<<“\n Element found at position”<<(pos+1);
getch();
}
Output:
Enter the number of elements in the array:5
Enter the elements of array(in ascending order):
1 4 7 9 8
Enter the element to be searched for: 9
Element found at position 4
Memory Representation of single
dimensional array
Single dimension arrays are essentially list of
information of same type and stored in
contiguous memory location in their index
order.
Ex .
char Grade [5];
Will have element grade[0] at first location,
grade[1] in the next position.
TYPES OF ARRAYS
. Grade[0] Grade[1] Grade[2] Grade[3] Grade[4]

Address 2000 2001 2002 2003 2004

The amount of storage required to


hold an array is directly related to its
type and size can be computed on the
following formula
Total bytes=sizeof(type)*size of array
STRING AS AN ARRAY
C++ does not have string data type
rather it implements string as a single –
dimension character array. A string is
defined as a character array that is
terminated by a null character.
Ex.
To declare an character array that
hold 10 character string we would write
char str[10];
TWO DIMENSIONAL ARRAY
A two dimensional array is an array in
which each element is itself an array. Ex
An array A[m][n] is an m by n table with
m rows and n columns containing m*n table.

0 N-1

M-1
PROCESSING 2 D ARRAY
We use nested loops to process 2 d array. one
loop process the rows and another columns .If
outer loops is for rows and inner loop for columns
then each row index, all columns are processed
and then the same process is repeated.
int A [2][3];
int i,j;
for(i=1;i<=2;++i)
{
for (j=1;j <=3; ++j)
{ cout<<“enter element”;
cin >> A [ i ] [ j ] ;
}
cout<<“\n”;
MEMORY REPRESENTATION
OF 2D ARRAY
Two dimension array are stored in a row
column matrix, where the first index
represents the row and second indicates the
column. This means that the second index
changes faster than first index when
accessing the elements in an array order.
The formula to calculate the total number
of bytes of two d array are:
Total bytes=no of rows *no of columns*size of base
MATRICES AS 2D ARRAYS

Matrix is a set of mn numbers arranged


in a rectangular array of m rows and n
columns. Such matrix is called m by n
matrix.
int Mat [5] [4] ; //if matrix contains integers
Or
float Mat [5] [4] ;
ARRAY OF STRINGS
An array of string is a two dimensional
character array . The size of the first index
determines the number of strings and size of
second index determines maximum length
of each string.
Eg:
char str[60];
ARRAY INITIALIZATION
C++ provides the facility of an array
initialization at the time of declaration. The
arrays are initialization in the same way as
other variable.ex .
Type array_name [size]={value-list};

Following code initializes an integer


array with 12 elements
int rno[10]={1,2,3,4,5,6,7,8,9,10};

This would place 1 is rno[0],2 is rno[2].


ARRAY INITIALIZATION
Character arrays can also be initialized like:
Char string [10]=“program”;
Will initialize it as
char string [10]={‘p’ , ‘r’ , ‘o’ , ‘g’ , ’r’ , ‘a’ ,’m’, \0}
Two dimension will do it in:
int cube [ 5 ] [2]= { 1 ,1,
2 , 8,
3, 27,
4, 68,
5, 25};
char string[2][7]={“Sunday”,”Monday”};
UNSIZED ARRAY
INITIALIZATIONS
C++ allows you to skip the size of an
array is the initialization . This is called an
unsized array. C++ calculates is size and
creates it as required.
char s1 [ ] =“first string”;
int val[ ]={2, 3, 4, 5, 6, 7, 8, 9};
float amount [ ]={2341.57, 1900.70};
CALLING FUNCTION WITH
ARRAY
C++ interprets an array name as the
address of its first element. C++ treats the
name of an array as if it were a pointer.
When an array is used as an argument to
a function, only the address of the array gets
passed not a copy of the entire array.
Program
void main()
{ void display(int age[10]);
int age[10],i;
cout<<“Enter elements:\n”;
for(i=0;i<10;i++)
cin>>age[i];
display(age);
getch();
}
void display(int a[10])
{ for(int i=0;i<10;i++)
cout<<“\n”<<a[i];
Program-unsized array
void main()
{ void display(int age[]);
int age[10],i,n;
cin>>n;
cout<<“Enter elements:\n”;
for(i=0;i<n;i++)
cin>>age[i];
display(age);
getch();
}
void display(int a[])
{ for(int i=0;i<n;i++)
cout<<“\n”<<a[i];
}
Program-pointer argument
void main()
{ void display(int age[10]);
int age[10],i;
cout<<“Enter elements:\n”;
for(i=0;i<10;i++)
cin>>age[i];
display(age);
getch();
}
void display(int *a)
{ for(int i=0;i<10;i++)
cout<<“\n”<<a[i];
THANK
YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy