Bcse102L Structured and Object Oriented Programming: Module-2 Array An in C

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

BCSE102L STRUCTURED

AND OBJECT ORIENTED


PROGRAMMING
Module-2
Array an in C
Dr. R. Jothi SCOPE
Outline
• Array
• Need
• Declaration
• Initialization
• Examples
• Matrix
• Given a list of test scores, determine the maximum
and minimum scores
• Do you use different variable for each score?
• score1, score2, score3, etc.
Or
• Use same name ‘score’ for referring all the score values.
• score[1] for first value, score[2] for second value etc.
Array
• An Array is a collection of data elements that are of the
same type (e.g., a collection of integers, collection of
characters, collection of doubles)
• E.g.
• a list of student names
• a list of marks
• a matrix
Array Declaration
Datatype Arrayname[size];

// array of 10 uninitialized ints


int Score[10];

0 1 2 3 4 5 6 7 8 9
Ar -- -- -- -- -- -- -- -- -- --

0 1 2 3 4 5
Subscripting
• Declare an array of 10 integers:
int Ar[10]; // array of 10 ints
• To access an individual element we must apply a subscript
to array named Ar.
– A subscript is a bracketed expression.
• The expression in the brackets is known as the index
• Index is always a positive integer
– First element of array has index 0.
Ar[0]
– Second element of array has index 1, and so on.
Ar[1], Ar[2], Ar[3],…
– Last element has an index one less than the size of the array.
Ar[9]
• Incorrect indexing is a common error!.
Subscripting

// array of 10 uninitialized ints


int Ar[10];
--
int x
Ar[3] = 1; 1 --
x = Ar[3];
-- -- --

0 1 2 3 4 5 6 7 8 9
Ar -- -- -- 1 -- -- -- -- -- --
Ar[0] Ar[1] Ar[2] Ar[3] Ar[4] Ar[5] Ar[6] Ar[7] Ar[8] Ar[9]
Array Element Manipulation

• Consider
int Ar[10], i = 7, j = 2, k = 4;
Ar[0] = 1;
Ar[i] = 5;
Ar[j] = Ar[i] + 3;
Ar[j+1] = Ar[i] + Ar[0];
Ar[8] = 12;

0 1 2 3 4 5 6 7 8 9
Ar 1 -- 8 6 3 -- -- 5 12 --
Ar[0] Ar[1] Ar[2] Ar[3] Ar[4] Ar[5] Ar[6] Ar[7] Ar[8] Ar[9]
Program to read 10 score values and find their
sum
void main()
{
int score[10]; Array index always starts at 0.
For n element array : index values
int i,sum=0; are 0 to n-1
//read 10 scores
for(i=0;i<10;i++)
{
printf(“Enter score %d : “,i);
scanf(“%d”,&score[i]);
}
for(i=0;i<10;i++)
{
printf(“Score %d: %d”,i,score[i]);
sum=sum+score[i];
}
printf(“Sum of scores= %d”,sum);
}
Program to read 10 score values and find their
Min and Max
void main()
{
int score[10], i, min=1000,max=0;
//read 10 scores
for(i=0;i<10;i++)
{
printf(“Enter score %d : “,i);
scanf(“%d”,&score[i]);
}
for(i=0;i<10;i++)
{printf(“Score %d : %d”,i,score[i]);
if(score[i]<min) min=score[i];
if(score[i]>max) max=score[i];
}
printf(“Min score = %d, Max score = %d”, min,max);
}
Initialization
Only A[0] initialized
// To initialize all elements with 0 void main() to 1; rest A[1] to
void main() { A[9] are set to 0
{ int i, A[10]={1};
int i, A[10]={0}; //or A[i]={}; for(i=0;i<10;i++)
for(i=0;i<10;i++) printf("%d\t",A[i]);
printf("%d\t",A[i]); }
}

Elements with missing values will be initialized to


0:

int A[10] = { 1, 2 }; // initialize to 1,2,0,0,0...


Initialization
Size will be
void main() automatically set to
{ 5
int i, A[]={1,2,3,4,5};
for(i=0;i<5;i++)
printf("%d\t",A[i]);
}
Matrix
• 2-dimensional array
int Ar[3][3]; //declares 3 by 3 matrix
Ar[0][0] 2001,2002

Ar[0][0] Ar[0][1] Ar[0][2] Ar[0][1] 2003


Ar[1][0] Ar[1][1] Ar[1][2] Ar[0][2] 2005
Ar[2][0] Ar[2][1] Ar[2][2] Ar[1][0] 2007
Ar[1][1] 2009
Ar[1][2] 2011
Ar[2][0] 2013
Ar[2][1] 2015
Ar[2][2] 2017,2018
Read matrix
int A[6][3];
for(i=0;i<6;i++)  for number of rows
for(j=0;j<3;j++)  for number of columns
scanf(“%d”, &A[i][j]);
Matrix Operations
• C = A+B, C=A-B, C=A*B
• Transpose B=AT
Sum of two matrices
void main()
{ for(i=0;i<rows;i++)
int A[10][10],B[10][10],C[10][10]; for(j=0;j<cols;j++)
int i,j,rows,cols; C[i][j]=A[i][j]+B[i][j];
clrscr(); for(i=0;i<rows;i++)
printf("enter no. of rows and cols of {
matrices : "); for(j=0;j<cols;j++)
scanf("%d%d",&rows,&cols); printf("%d\t",C[i][j]);
printf("\nEnter Matrix A : "); printf("\n");
for(i=0;i<rows;i++) }
for(j=0;j<cols;j++) getch();
scanf("%d",&A[i][j]); }
printf("\nEnter Matrix B : ");
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
scanf("%d",&B[i][j]);
Matrix Multiplication
Product of Matrices
for(i=0;i<row;i++)
for(j=0;j<col;j++)
{
C[i][j]=0;
for(k=0;k<row;k++)
C[i][j]=C[i][j]+A[i][k]*B[k][j];
}
Transpose of Matrix
Determinant of 3 x 3 Matrix
determinant = a[0][0] * ((a[1][1]*a[2][2]) - (a[2]
[1]*a[1][2])) -a[0][1] * (a[1][0]
* a[2][2] - a[2][0] * a[1][2]) + a[0][2] * (a[1][0] *
a[2][1] - a[2][0] * a[1][1]);
Matrix Initialization
void main()
{
int Mat[3][3]={{1, 2, 3},{4, 5, 6},{7, 8, 9}};
int i,j;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d\t",Mat[i][j]);
printf("\n");
}
getch();
}
//Both initializes 3x2 matrix
int a[3][2] = { { 1 , 2 }, { 3 , 4 }, { 5 , 6 } };
OR
int a[3][2] = { 1, 2, 3, 4, 5, 6 };

//3x3 Identity Matrix


int I[3][3]={{1,0,0},{0,1,0},{0,0,1}};
OR
int I[][]={{1,0,0},{0,1,0},{0,0,1}}; //size 3 x 3

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