0% found this document useful (0 votes)
4 views39 pages

Lecture 5 - Array

The document is a session on C programming focusing on arrays, including their declaration, initialization, and methods for input and output. It covers one-dimensional and two-dimensional arrays, sorting algorithms like bubble sort, and passing arrays to functions. Additionally, it includes exercises and examples for practical understanding of array manipulation in C.

Uploaded by

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

Lecture 5 - Array

The document is a session on C programming focusing on arrays, including their declaration, initialization, and methods for input and output. It covers one-dimensional and two-dimensional arrays, sorting algorithms like bubble sort, and passing arrays to functions. Additionally, it includes exercises and examples for practical understanding of array manipulation in C.

Uploaded by

nesab39
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ME 172

Introduction to Computer
Programming Language
Sessional

Md. Tusher Ahmed


Assistant Professor, Dept. of ME, BUET

19 May 2025 ME 172 : C Programming Sessional 1


Lecture 5
Array
19 May 2025 ME 172 : C Programming Sessional 2
Array(s)
• An array is a collection of data items, all of the same type, accessed using a
common name. [[Link]]
• Arrays in C act to store related data under a single variable name with an index,
also known as a subscript. It is easiest to think of an array as simply a list or
ordered grouping for variables of the same type.
[[Link]
• An array is used to store a collection of data, but it is often more useful to think
of an array as a collection of variables of the same type.
[[Link]
• All arrays consist of contiguous memory locations.

19 May 2025 ME 172 : C Programming Sessional 3


One dimensional array
Declaration
Memory allocation for arrays
type variable_name [size];

Example: int Numbers[10];

float numb2[15];

REMEMBER!!!: There is a zeroth element in all arrays!! So array_name [5] is


actually the sixth element in the memory!!

19 May 2025 ME 172 : C Programming Sessional 4


One dimensional array
Methods of Declaration
* If any element of an array is not initialized, it holds some garbage value. So always be careful about the number of element up to
which you initialize an array!!!!
* You can initialize all elements of an array in the first line like arr_name[50] = {0}.

Sample Code
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a[20], b[20] = {0};

a[0] = 5;
printf("%d\n",a[2]);
printf("%d",b[5]); Output
return 0;
}
19 May 2025 ME 172 : C Programming Sessional 5
One dimensional array
Methods of Declaration
* You can also declare an array without mentioning the size. In that case , the compiler adjusts the size of
array. But it is not a good practice.

Sample Code
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a[20], b[] = {0,1,2,3};

a[0] = 5;
printf("%d\n",a[2]);
printf("%d",b[2]); Output
return 0;
}
19 May 2025 ME 172 : C Programming Sessional 6
One dimensional array
Methods of Declaration
* For char type data, however, the method works in a different way.

For example, char s[] = ‘abc’;

…………..is taken by the compiler as

char s[] = {‘a’, ‘b’, ‘c’, ‘\0’};

19 May 2025 ME 172 : C Programming Sessional 7


One dimensional array
Taking inputs of a one dimensional array

#include<stdio.h>

void main (void)


{
int i[5], j; Declaration
for( j=0; j<5; j++)
{ Taking Inputs
scanf(“%d”,&i[j]);
}
for( j=0; j<5; j++)
{
printf(“i[%d]=%d\n”,j,i[j]);
}
} Input: 10 1 5 6 8

i[0] i[1] i[2] i[3] i[4]


19 May 2025 ME 172 : C Programming Sessional 8
One dimensional array
Example: Array - 1
Write a program that will take 10 numbers as input and find average of them

19 May 2025 ME 172 : C Programming Sessional 9


Exercise
Write a program that will take a number as input and find summation
of all the digits of the number.

991%10=1
99%10=9
9%10=9

Example: 991 991/10=99


99/10=9
9/10=0

Summation of all the digits is 9+9+1=19

5/19/2025 ME 172 : C Programming Sessional 10


Solution
int main ()
{ int a,b,i,x,s=0; //b is the temporary variable
int c[20];
printf("Enter the number(integer)");
scanf("%d",&a);
b = a;
for(i = 1;;i++)
{
c[i] = b%10;
b = b/10;
s = s + c[i];
if (b==0) break;
}
printf("\nThe summation of all the digit of the number %d",a);
printf("= %d",s);
return 0;
}

19 May 2025 ME 172 : C Programming Sessional 11


Sorting in one dimensional array

• Say you take numbers as input in an integer array and you want to
sort it from smallest to largest.

• Sorting can be done by a few methods:


1. Bubble sort
2. Selection sort
3. Insertion sort

Only the bubble sort will be shown in details in the class, the
selection and insertion sort codes will be provided in these
slides, but will be left for students to figure out how the code
works.
19 May 2025 ME 172 : C Programming Sessional 12
Bubble Sorting
❑Bubble sorting is also known as sinking sort or comparison sort. Actually it compares between
two elements at a time in an array and swaps those elements if those are in wrong order.
❑The algorithm has its name because here smaller elements bubble up like air bubbles in water.
❑It is not an efficient algorithm for sorting but certainly an easy one!!

Courtesy: [Link]

19 May 2025 ME 172 : C Programming Sessional 13


Bubble Sorting

19 May 2025 ME 172 : C Programming Sessional 14


Bubble Sorting

19 May 2025 ME 172 : C Programming Sessional 15


Bubble Sorting

Say the input is:

The output after each loop:

If your array consists of n integers, it will take n-1 loops for complete
sorting (as it will take n-1 swaps to bring the last integer to the first
place, should the last integer be the lowest integer)
19 May 2025 ME 172 : C Programming Sessional 16
Selection Sort

19 May 2025 ME 172 : C Programming Sessional 17


Selection Sort

19 May 2025 ME 172 : C Programming Sessional 18


Insertion Sort

19 May 2025 ME 172 : C Programming Sessional 19


Insertion Sort

19 May 2025 ME 172 : C Programming Sessional 20


Two dimensional array

Declaration:

type variable_name [row size][column size];

Example: int matrix [5] [5];

float number[10][15]; Two dimensional grid

19 May 2025 ME 172 : C Programming Sessional 21


19 May 2025 ME 172 : C Programming Sessional 22
Two dimensional array
Taking inputs of a void main (void)
{ i[0][0] i[0][1] i[0][2]
two dimensional int i[2][3], j, k;
array for( j=0; j<2; j++) i[1][0] i[1][1] i[1][2]
{
for( k=0; k<3; k++)
{
scanf(“%d”,&i[j][k]);
}
}
for( j=0; j<2; j++)
{
for( k=0; k<3; k++)
{
printf(“%d”,i[j][k]);
}
printf(“\n”);
} } Input: 1 2 3 4 5 6
i[0][0] i[0][1] i[0][2] i[1][0] i[1][1] i[1][2]
19 May 2025 ME 172 : C Programming Sessional 23
Two dimensional array

Exercise 1: Two Dimensional Array

Write a program that declares a 3×4 array and takes the


following matrix elements as input and prints them on the
screen as follows:
Input 1 2 3 4 5 6 7 8 9 10 11 12

Output
1 4 7 10
2 5 8 11
3 6 9 12

19 May 2025 ME 172 : C Programming Sessional 24


Two dimensional array
Solution
int i[3][4], j, k;
for( j=0; j<3; j++)
{ for( k=0; k<4; k++)
{ scanf("%d",&i[j][k]); }}

for( j=0; j<3; j++)


{
for( k=0; k<4; k++)
{
printf("%5d",i[j][k]);
}
printf("\n");
}
OUTPUT

19 May 2025 ME 172 : C Programming Sessional 25


Array passed to a function

❑ There are two ways of passing elements from array to functions.


❑ One can pass a particular element from an array to the function just like any normal
variable. Example: arr_name[10] = 25;
int avg(int x);
n = avg(arr_name[10]);

❑ Another way of passing arrays to a function is call by reference. It basically means


that the programmer declares and works with an array in the main program. This exact
array, with all its elements, can be passed to a function. Later, the user defined function
can modify the array from original memory location.

19 May 2025 ME 172 : C Programming Sessional 26


Array passed to a function

19 May 2025 ME 172 : C Programming Sessional 27


Array passed to a function

19 May 2025 ME 172 : C Programming Sessional 28


Array passed to a function

int func_name(int a[5][5]);


Passing Two
void main(void)
dimensional arrays {
int a[5][5];
func_name(a);
………...
}

int func_name(int a[5][5])


{
statements;
}

19 May 2025 ME 172 : C Programming Sessional 29


Array passed to a function

19 May 2025 ME 172 : C Programming Sessional 30


Example

## Write a program that will take the elements of a 3×4 matrix as input and finds the transpose of that
matrix.

Print both matrices on the screen.

0 2
Hints:
4
0 5 6 15 Transpose 5 
4   3 7
 3 1 11
6 1 8
2 7 8 9 3×4  
15 11 9  4×3

19 May 2025 ME 172 : C Programming Sessional 31


Program to Find Transpose of a Matrix

19 May 2025 ME 172 : C Programming Sessional 32


Exercise
Write a C program for the multiplication of two matrices

5[0][0] 3[0][1] 6[0][0] 7[0][1]


4[1][0] 2[1][1] 4[1][0] 8[1][1]

5*6+3*4 5*7+3*8
[0][0] [0][1]
4*6+2*4 4*7+2*8
[1][0] [1][1]
19 May 2025 ME 172 : C Programming Sessional 33
Exercise on 2D Array (solution)

#include<stdio.h>
#include<math.h>
main(void)
{ int a[20][20],b[20][20],c[20][20];
int i,j,k,r1,c1,r2,c2;
printf("Give No of Rows and Columns of Matrix1 \n");
scanf("%d %d", &r1, &c1);
printf("\nGive No of Rows and Columns of Matrix2 \n");
scanf("%d %d",&r2,&c2);

if(c1!=r2)
printf("Reverse dimension of Matrix ");

Continued …

19 May 2025 ME 172 : C Programming Sessional 34


Continued from previous page…

else {
printf("put the Elements of Matrix 1 \n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[ i ][ j ] );

printf("put the Elements of matrix 2 \n");


for(i=0;i<r2;i++)
for(j=0;j<c2;j++) scanf("%d",&b[ i ][ j ]);
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
{ c[i][j]=0;
for(k=0;k<c1;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}

Continued …
19 May 2025 ME 172 : C Programming Sessional 35
Continued from previous page …

printf("Out put\n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
getch();
}

19 May 2025 ME 172 : C Programming Sessional 36


Class Performance
Write a C program to calculate the trace of 3X3 matrix

19 May 2025 ME 172 ME


: C Programming
172 : C Programming
SessionalSessional 37
ASSIGNMENT

Write a program that will take a 3 3 matrix as input in


the calling function (main function) and find it’s
determinant using a different function. Print the input
matrix and output in the main function.

19 May 2025 ME 172 : C Programming Sessional 38


Thank you

19 May 2025 ME 172 : C Programming Sessional 39

You might also like