0% found this document useful (0 votes)
12 views

Lecture 6

Uploaded by

fakhrialamqazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lecture 6

Uploaded by

fakhrialamqazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

ARRAYS

LECTURE #6
ARRAYS
 An array is a sequence of same data type. The objects in
an array are also called elements of array.

 An array is represent in memory by a consecutive group


of storage locations. Theses locations are referenced by
a single variable called array name.

 Each element in the array is represented by its index


value or subscript (position) .
 In an array with “n” elements having the index value
from 0……n-1, where “0” represent the index value of
first element and “n-1” represents the index value of the
last elements.
 The elements of the array can be accessed the subscript
value.
 The subscript or index value is written inside a pair of
square brackets [ ] with the name of a array to
represent element of an array.

A= P A K I S T A N

such as A[5] = T, A[0] =P, A[7] =N


ARRAY
 Array are used to process a large amount of data of same
type.
 The array is accessed by a single variable name.

 The index values is used to access individual elements


of the array.

 Types of an array
 One –dimensional Array
 Multi-dimensional Array
ONE DIMENSIONAL ARRAY
 One dimensional array or also called linear array.

 It consist of single column or one row.

 For example, the student in a class are represent by the


student numbers.
Number
Number[0] Ali
Name of the array= number
Number[1] Ahmad
Number[2] Muhammad
Size of the array= 8
Number[3] Naveed
Number[4] Riaz
Number[5] Naeem
Number[6] Raees
Number[7] Rashid
DECLARING ONE DIMENSIONAL ARRAY
 An array can be declared by defining
 thename of an array
 Type of the array
 Total numbers of elements in an array
 When an array is declared, a memory block with
required numbers of locations is reserved in computer
for storing the data into elements of the array.

General Syntax of an array:


Type array_name[n]
Type = data type
Array_ name = the variable this used to represent an array.
N = size of an array
WRITE A PROGRAM TO INITIALIZE THE VALUES IN AN
ARRAY AND THEN PRINT THESE VALUES IN THE SCREEN
#include<iostream. h>
#include<conio. h>
Void main ()
{
Float temp[5];
temp [0]=66.2;
temp [1]=63.2;
temp [2]=69.6;
temp [3]=70.2;
temp [4]=55.4;
//print the values
For (i=0;i<=4; i++)
Cout<<temp [i]<<endl;
getch();
}
#include<iostream. h>
#include<conio. h>
Void main ()
{
Float temp[5]={66.2, 63.2, 69.6, 70.2, 55.4;}
//print the values
For (i=0;i<=4; i++)
Cout<<temp [i]<<endl;
getch();
}
#include<iostream.h>
#inclde<conio.h>
Void main()
{
Char name[5],i;
Name[0] =‘A’;
Name[1] =‘h’
Name[2] =‘m’
Name[3] =‘a’
Name[4] =‘d’
For(i=0;i<5;i++)
Cout<<name[i];
getch();
}
INITIALIZATION 2ND
METHOD
Write a program to enter integer type data into an array and
the print the values in reverse order.
#include<iostream. h>
#include<conio. h>
Void main ()
{
Int abc[5], i;
For (i=0;i<=4; i++)
{
Cout<<“enter value in element”<<i<<endl;
Cin>>abc [i];
}
Cout<<“output in reverse order”<<endl;
For(i=4; i>=0; i--)
Cout<<abc[i]<<endl;
getch();
WRITE A PROGRAM IN C++ TO FIND OUT AND PRINT THE
MAXIMUM VALUE IN THE ARRAY.
#include<iostream. h> Cout<<“Maximum value is =”
#include<conio. h>
Void main ()
<<max;
{ getch();
float abc[5], max;
}
Int i;
For (i=0;i<=4; i++)
{
Cout<<“enter value in element”
<<i<< “ = ”;
Cin>>abc [i];
}
Max = abc [0];
For(i=4; i<=4; i++)
{
if (abc [i] > max)
Max =abc [i];
}
WRITE A PROGRAM TO INPUT DATA INTO AN ARRAY OF 5
ELEMENTS CALCULATE THE SUM AND AVERAGE OF THE
ELEMENTS AND THEN PRINT THE SUM AND AVERAGE ON THE
SCREEN
#include<iostream. h> Sum=sum + abc [i];
#include<conio. h> Avg =sum / 5;
Void main () Cout<<“sum of array values =”<<sum<<endl;
{ Cout<<“average of array values =”<<avg;
float abc[5], sum, avg; getch();
Int i; }
For (i=0;i<=4; i++)
{
Cout<<“enter value in element”
<<i<< “ = ”;
Cin>>abc [i];
}
Sum=0;
Avg=0;
For(i=4; i>=1; i--)
WRITE A PROGRAM IN C++ TO INPUT DATA INTO AN ARRAY, ENTER A
VALUE FROM THE KEYBOARD AND FIND OUT THE LOCATION OF THE
ENTERED VALUE IN THE ARRAY. IF THE ENTERED NUMBER IS NOT FOUND
IN THE ARRAY, DISPLAY THE MESSAGE “NUMBER NOT FOUND”.

#include<iostream. h> For (i=0;i<=4; i++)


#include<conio. h> {
Void main () If (n = =abc [i])
{ {
int abc[5], n, i, p; P=i+1;
For (i=0;i<=4; i++) Break;
{ }
Cout<<“enter value in element” }
<<i<< “ = ”; if(p = =0)
Cin>>abc [i]; Cout<<“number not found”;
} else
p=0; Cout<<“number found at position =”<< p;
Cout<<“enter any integer value ?”; }
Cin>>n getch();
MULTI-DIMENSIONAL
ARRAY
 A multi dimensional array is an array of more than one
array.
 An array having two one dimensional arrays is called
two-dimensional array. It is also known as table or
matrix.
 A two dimensional array consists of columns and
rows.
 Each element of the two dimensional array is
referenced by its index values or subscript.
 A two dimensional array consist of two subscript
values.
 One subscript represents the row number and the
second subscript represents the column number.
For example: if “temp” is a two dimensional array having
7 rows and 3 columns, its first element is temp [0][0]
and the last element is temp [6][2].
Temp [0][0] Temp [0][1] Temp [0][2]

Temp [1][0] Temp [1][1] ---

--- --- ---

--- --- ---

--- --- ---

--- --- ---

--- --- Temp [6][2]


DECLARATION OF TWO-DIMENSIONAL ARRAY
 A two dimensional array is declared by giving two indexed
values enclosed in square brackets.
 The first indexed value represents the total number of rows
and the second represents the total number of columns.

Syntax Of two Dimensional Array


Type array_name [r][c]

Type represents the data type of the array


Array_name represents the name of the two
dimensional array.
R represents the number of rows
C represents the number of Columns
In C++ variables one-dimensional arrays and multi
dimensional arrays all of same data types can be
declared in a single statement, e.g.

Float a, b, c[10], x[8][4], y, ab[11][2];


ACCESSING DATA IN TWO-DIMENSIONAL ARRAYS
 Data is entered into individual elements of a two dimensional
array.

 To enter data, the elements is referenced by its index or subscript


value.

 Similarly, data is retrieved from an array from individual


elements of the array.

 Usually nested loop are used to access elements of two


dimensional array.

 In nested loop the upper loop is used to change the index value of
the rows and the inner value is used to change the index value of
the columns.
PROGRAM TO PRINT OUT DATA
FROM THE ELEMENTS OF A TABLE
#include<iostream. h> r=r+1;
#include<conio. h> }
Void main () Cout<<“printing data from the
{ table”<<endl;
int abc[2][3], r, c; r =0;
Clrscr (); While(r<=1)
Cout<<“input data into table”<<endl; {
r=0; C=0;
While(r<=1) While(c<=2)
{ {
C=0; Cout<<abc[][]<<“\t”;
While(c<=2) C=c+1;
{ }
Cout<<“enter value in row =” ; Cout<<endl;
Cout<<r<<“column =”<<c<< “ ”; r=r+1;
Cin>>abc [r] [c]; }
c=c+1; getch();
} }
WRITE A PROGRAM TO INPUT FLOAT DATA TYPE INTO A
TABLE HAVING 5 ROWS AND 4 COLUMNS. FIND OUT THE
MAXIMUM NUMBER ENTERED IN THE TABLE AND PRINT
OUT ON THE SCREEN

#include<iostream. h> For(c=0;c<4;c++)


#include<conio. h> For(r=0;r<=3;r++)
Void main ()
If(m<aa[r][c])
{
int r, c, m, aa[5][4];
Clrscr (); m=aa[r][c];
For (c=0; c<=4;c++) Cout<<“the maximum
For(r=0;r<=3; r++) values is =”<<m<<endl;
{ getch();
Cout<<“enter data in c and r”;
}
Cin>>aa[r][c];
}
m=aa[0][0];
INITIALIZING TABLES
 Assigning values to the elements of a table at the time of
its declaration is called initialization of the table.

 The values are assigned to the elements of a table at the


time of its declaration in the same manner as they are
assigned to the elements of a list.

 For example, to assign values to a table abc[2][3] that


has two rows and three columns, values are assigned
row –wise.
 The declaration and initialization in the above table will
be:
Int abc[2][3] ={{10,20,30},{11,12,13}};
WRITE A PROGRAM TO INITIALIZE VALUE TO A TABLE XYZ
HAVING 4 COLUMNS AND 3 ROWS AT THE TIME OF
DECLARATION. ALSO PRINT DATA FROM THE TABLE IN
TABULAR FORM

#include<iostream. h>
#include<conio. h>
Void main ()
{
int xyz[3][4]={{2,3,10,1},{6,12,16,3},{4,3,13,17}};
int r, c;
For (r=0; r<=2;r++)
{
For(c=0;c<=3; c++)
{
Cout<<xyz[r][c]<<“\t”;
}
Cout<<endl;
}
getch();
}
INITIALIZING CHARACTER TYPE TABLES
 The values in a table of “char” type is also assigned in
the same way as other data type.

 Char st[5][3]={{‘a’, ‘x’, ‘y’},{‘p’, ‘e’, ‘t’},{‘p’, ‘a’,


‘k’},{‘a’, ‘b’, ‘c’},{‘3’, ‘1’, ‘6’}};

 Each character is enclosed in single quotation marks.

 Char st[5][4]={“xyz”, “pet”, “pak”, “abc”, “316”};


WRITE A PROGRAM THAT STORE THE NAME OF 7
DAYS OF THE WEEK INTO SEVEN ELEMENTS OF ARRAY
“DAYS” AND THEN PRINT THEM
#include<iostream. h>
#include<conio. h>
Void main ()
{
char days[7][10]={“Monday”, “Tuesday” , “Wednesday” ,
“Thursday” , “Friday”, “Saturday”, “Sunday”};
int i;
For (i=0; i<7;i++)
Cout<<days[i]<<endl;
getch();
}
WRITE A PROGRAM THAT WILL ACCEPT 15 VALUES FROM
THE USER AND PLACE THESE VALUES INTO THE
ELEMENTS OF ARRAY “NUM”, AND THEN FIND OUT AND
PRINT THE LARGEST AND SMALLEST ELEMENTS OF THE
ARRAY “NUM”
#include<iostream. h> For(c=1;c<15;c++)
#include<conio. h> {
Void main () If(NUM[c]>large)
{ Large=NUM[c];
Int Num[15], c, large,small;
Cout<<“enter any 15 values\n”; If(NUM[c]<small)
For(c=0;c<15;c++) small=NUM[c];
Cin>>Num[c]; }
//Assume 1st element is the largest Cout<<“”largest element
element is:”<<large<<endl;
//Assume 1st element is the smallest
Cout<<“smallest element
element
is :”<<small;
Large=Num[0];
Getch();
Small=NUM[0];
}
WRITE A PROGRAM TO STORE, PRINT AND COMPUTE THE OVERALL SUM OF
THE FOLLOWING MATRIX 2 1 4
3 2 7
5 6 8
#include<iostream. h> For(c=0;c<3;c++)
#include<conio. h> Sum=sum +mat[r][c];
#include<string.h> cout<<“sum=”<<sum;
Void main ()
getch();
{
}
int mat[3][3]={2,1,4,3,2,7,5,6,8};
int r, c, sum;
sum=0;
For (r=0; r<3; r++)
{
For (c=0;c<3; c++)
Cout<<mat[r][c]<<“\t”;
Cout<<endl;
}
// compute the sum f the matrix
For(r=0; r<3; r++)
WRITE A PROGRAM TO FIND OUT THE PRODUCT OF ANY TWO
MATRICES
#include<iostream. h>
Cout<<“2nd matrix is ….\n”;For (r=0; r<3; r++)
#include<conio. h>
{
#include<string.h> For (c=0;c<3; c++)
Void main () Cout<<mat2[r][c]<<“\t”;
{ Cout<<endl;
int mat1[3][3]={1,2,3,4,5,6,7,8,9}; }
int mat2[3][3]={1,2,3,4,5,6,7,8,9};
//product of two matrices are
int mat3[3][3]={0};
For(int i=0;i<3;i++)
int r, c;
For(int j=0;j<3;j++)
clrscr( ); For(int k=0;k<3;k++)
Cout<<“1st Matrix is ….\n”; Mat3[i][j]=mat3[i][j]+(mat1[i][k] *mat2[k][j]);
For (r=0; r<3; r++) Cout<<“product of two matrices is……\n”;
{ For(r=0;r<3;r++)
For (c=0;c<3; c++) {
Cout<<mat1[r][c]<<“\t”; For(c=0;c<3;c++)
Cout<<mat3[r][c]<<“\t”;
Cout<<endl;
Cout<<endl;
}
}
getch();
WRITE A PROGRAM THAT SORT ANY GIVEN SIX NAMES
ALPHABETICALLY
#include<iostream. h> {
#include<conio. h>
strcpy (temp, name[i]);
#include<string.h>
Void main () strcpy (name[j],name [j+1]);
{ strcpy (name [j+1],temp);
int name[6], temp[10]; }
int i, j, c;
Clrscr();
Cout<<the entered name in sorted
Cout<<“Enter any six names \n”; order are….\n”;
For(c=0;c<6;c++)
Cin>>name[c]; For(c=0;c<6;c++)
\\Sorting process
For(i=0;i<6;i++)
Cout<<name[c];
For(j=0;j<5;j++)
getch();
If (strcmp(name[j],name[j+1])>0)
ASSIGNMENTS
1. Write a program that takes the transpose of the following matrix?
7 2 3 4
4 2 9 9
5 3 5 2
1 5 2 8
2. Write a program to sum the diagonal elements of the following 3*3 matrix ?.
3 2 9
5 6 5
2 7 2

3. Write a program to sum the diagonal elements of the following 3*3 matrix ?.
A= 1 2 3
4 5 6
7 8 9

B= 1 2 3
4 5 6
7 8 9

You might also like