SlideShare a Scribd company logo
19/10/2024
Module 3
1
CSL 101-
Computer Programming
Computer Science and Engineering
Indian Institute of Information Technology,
Nagpur.
Varsha Kushwah
Arrays
• Array is a collection of similar data items or elements. Or we can say that array is a collection of homogeneous data
elements stored continuously under a single name. Array is defined as a set of homogeneous data items.
• Need for an array – When the number of variables of same type and nature are more then it is difficult to handle them.
So we need an array. Let us understand the use of array with an example:
#include <stdio.h>
int main() {
int i, a;
for(i=1;i<=5;i++)
{
printf(“nEnter the no.”);
scanf("%d", &a);
}
printf("Print the value of a %d", a);
return 0;
}
19/10/2024 3
• Declared using the following syntax
• type arrayname[size];
• Example:
int a[6];
• Here a is the name of the array.
• int is the data type of values which the array will store.
• 6 is the size of array and [] is called as subscript operator. The size of array must be a
constant.
• The most important thing is that all the values in the array should be of the same type.
Array Declaration
19/10/2024 4
• Initializing Arrays during Declaration
• Syntax: type arrayname[size] = {list of values};
• Example: int marks[5]={90, 82, 78, 95, 88};
Storing Values In Arrays:
19/10/2024 5
• Inputting Values from the Keyboard
int i, marks[5];
for(i=0; i<4; i++)
scanf("%d", &marks[i]);
• Assigning Values to Individual Elements
int marks[3];
marks[0] = 60;
marks[1] = 70;
marks[2] = 65;
Storing Values In Arrays:
19/10/2024 6
Storing Values In Arrays:
19/10/2024 7
• Address of data element A[k]
A[i] = BA + (i – lower_bound) * size
• BA is the base address of the array A,
• i is the index of the element of which we have to calculate the address,
• size is the size of one element in memory, i.e. size of int is 4
Address of Array Elements
19/10/2024 8
• Given an array
• int marks[8] = {99, 67, 78, 56, 88, 90, 34, 85}
• Calculate the address of marks[4] when the base address = 1000 when
integer size is 2 bytes.
Solution
marks[4] = 1000 + 2(4 – 0) = 1000 + 2(4) = 1008
Address of Array Elements
19/10/2024 9
• The length of an array is given by the number of elements stored in it
• General formula
• Length = upper_bound – lower_bound + 1
where
• upper_bound is the index of the last element
• lower_bound is the index of the first element
• Let Age[5] be an array of integers
Where Age[0] = 2, Age[1] = 5, Age[2] = 3, Age[3] = 1, Age[4] = 7.
Calculate the length of the array.
Calculating the Length of an Array
POINTS TO BE NOTED :
1) Array name should be unique
2) The elements in the array should be of same type
3) Subscript (array size) cannot be negative
4) Subscript must always be an integer
Types of Arrays
1) One Dimensional Array
2) Two Dimensional Array
3) Three Dimensional Array
• Arrays whose elements are specified by one subscript
are called One dimensional array or linear array.
• Syntax :
datatype arrayname[size];
• For Example :
• Note :
By default array index should starts with zero (0)
Single or One Dimensional
Arrays
PROGRAM-ARRAY INITIALIZATION
PROGRAM-
Copy an array element to another array
#include <stdio.h>
int main()
{
int i, arr1[5] = {3, 4, 5, 1, 2};
int arr2[5];
for(i = 0; i < 5; i++)
{
arr2[i] = arr1[i];
printf("Elements in arr2: ");
for(i = 0; i < 5; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}
PROGRAM-
Calculate the average
int i, sum = 0;
float average;
int marks[5] = {4,5,1,2,3};
for(i = 0; i < 5; i++)
sum += marks[i];
average = (float) sum/5;
Finding the largest element
int i, max;
int arr[10] = {4,5,6,1,2,3,7,8,9,10};
max = arr[0];
for (int i = ; i < 10; i++)
if (arr[i] > max)
max = arr[i];
printf("Largest element = %d", max);
Operations on Arrays
• Traversing an array
• Inserting an element in an array
• Searching an element in an array
• Deleting an element from an array
• Merging two arrays
• Sorting an array in ascending or descending order
• Traversing an Array
Accessing each and every element of the array
Traversing data elements of an array A can include
printing every element
counting the total number of elements
performing any process on these elements
Example
for (int i = 0; i < n; i++)
printf(“%dn”, arr[i]);
Search an element in an Array
arr[5] arr[6] arr[7] arr[8] arr[9]
arr[0] arr[1] arr[2] arr[3] arr[4]
120 124 128 132 136
10 20 30 40 50
100 104 108 112 116
flag = 0;
for(i = 0; i < n; i++)
if(arr[i] == val){
flag = 1;
break;
}
if(flag == 1)
printf("n Element is found at Position %d ", i+1);
Inserting an element in an Array
• Insertion at the End of an existing array
n = 5;
pos = 6;
val = 60;
arr[pos - 1] = val;
n++;
arr[5] arr[6] arr[7] arr[8] arr[9]
arr[0] arr[1] arr[2] arr[3] arr[4]
120 124 128 132 136
10 20 30 40 50
100 104 108 112 116
#include<stdio.h>
int main()
{
int arr[10],i,n;
printf("n Enter N Elements");
scanf("%d",&n);
if(n<=10 && n>=1 )
{
for(i=0;i<n;i++)
{
printf("enter arr[%d]=",i);
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{printf("%dn",arr[i]);}
}
else
{ printf("Envalid input");}
return 0;
}
Enter N Elements : 3
Enter arr[0] : 2
Enter arr[1] : 5
Enter arr[2] : 3
2
5
3
Write a program for entering data into an array &
Reading data from an array
Array Initialization
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={10,20,30,40,50};
int i;
for(i=0;i<5;i++)
{
printf("%dn",a[i]);
}
}
10
20
30
40
50
PROGRAM-ARRAY INITIALIZATION
Output
#include <stdio.h>
int main() {
int arr[] = {1, 2, 2, 4, 2, 5, 5, 4, 7, 8, 8};
int n = sizeof(arr) / sizeof(arr[0]);
int visited[n];
for (int i = 0; i < n; i++)
{
visited[i] = 0;
}
for (int i = 0; i < n; i++)
{
if (visited[i] == 1)
{
continue;
}
int count = 1;
for (int j = i + 1; j < n; j++)
{
if (arr[i] == arr[j])
{
count++;
visited[j] = 1;
}
}
if (count > 1)
{
printf("Element %d is a duplicate and occurs %d times.n", arr[i], count);
}
}
return 0;
}
Element 2 is a duplicate and occurs 3 times.
Element 4 is a duplicate and occurs 2 times.
Element 5 is a duplicate and occurs 2 times.
Element 8 is a duplicate and occurs 2 times.
19/10/2024 22
Program to print the largest and second
largest element of the array.
• A Arrays whose elements are specified by two subscript such
as row and column are called One dimensional array or
linear array.
• Row  means horizontally
• Column  means vertically
A two - dimensional array looks like a school time-table,
consisting of rows and columns.
A two – dimensional array is declared as –
– data_type array_name[row_size][column_size];
– Ex: int marks[2][3];
Two Dimensional Arrays
Two Dimensional Array Initialization
• Ex: Store the marks obtained by three students in five different subjects
int marks[3][5];
•Pictorial form of a two-dimensional array
The result of the above assignment will be as follows :
Storing a Two-Dimensional Array
• Row major order
– the elements of the array are stored row by row where n elements
of the first row will occupy the first n locations
•Column major order
– the elements of the array are stored column by column where m
elements of the first column will occupy the first m locations
Address of Array Element
• Formula to calculate the address of some element A[I][J]
When Array indices start with 1.
– Array elements are stored in row major order,
Address(A[I][J]) = BA + w{N(I – 1) + (J – 1)}
– Array elements are stored in column major order
Address(A[I][J]) = BA + w{M(J – 1) + (I – 1)}
where
w is the number of bytes required to store one element,
N is the number of columns,
M is the number of rows,
I and J are the subscripts of the array element.
Address of Array Element
• Consider a 20 x 5 two-dimensional array marks which has its
base address = 1000 and the size of an element = 2. Compute
the address of the element, marks[18][4] assuming that the
elements are stored in row major order, and array indices start
with 1.
•Solution
Address(A[I][J]) = BA + w{N(I – 1) + (J – 1)}
Address(marks[18][4]) = 1000 + 2 {5(18 – 1) + (4 – 1)}
= 1000 + 2 {5(17) + 3}
= 1000 + 2 (88)
= 1000 + 176 = 1176
19/10/2024 28
Write a “C” program to insert the elements matrices
Write a “C” program to perform the addition of two matrices
#include<stdio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
printf("Input A - Matrixn");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("Input B - Matrixn");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("Sum of A and B Matrix=n");
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
printf("%d",c[i][j]);
printf("n");
}
}
Write a “C” program to perform the subtraction of two matrices
#include<stdio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
printf("Input A - Matrixn");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("Input B - Matrixn");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]-b[i][j];
printf("Sum of A and B Matrix=n");
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
printf("%d",c[i][j]);
printf("n");
}
}
Write a “C” program to perform matrix multiplication using two dimensional array
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q,k;
printf("Input row and column of A matrix n");
scanf("%d %d",&n,&m);
printf(" Input row and column of B matrix n");
scanf("%d %d",&p,&q);
if(m==p)
{
printf(" Matrices can be Multiplied: n");
printf(" Input A-matrix n");
for(i=0;i<n;++i)
for(j=0;j<m;++j)
scanf("%d",&a[i][j]);
printf(" Input B-matrix n");
for(i=0;i<p;++i)
for(j=0;j<q;++j)
scanf("%d",&b[i][j]);
printf("The resultant matrix ist:n");
for(i=0;i<n;++i)
{
for(j=0;j<m;++j)
{
c[i][j]=0;
for(k=0;k<p;++k)
c[i][j]=c[i][j] + a[i][k] * b[k][j];
printf("%d",c[i][j]);
}
printf("n
}
else
printf("Matrices cannot be multiplied n");
}
Session Summary
 Arrayname should be a unique and valid “C” Variable name
 The number of elements in a multi dimensional array is the product of its subscripts
 Arrays can be initialized to the same type in which they are declared
 The character array receives the terminating ‘0’ in the string constant
 The individual values in the array are called as elements
 It is not necessary to specify the length of an array, explicitly in case if initializers are
provided for the array during declaration itself

More Related Content

PDF
arraysfor engineering students sdf ppt on arrays
PPTX
Arrays
PPTX
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
PPT
Array
PPTX
Chapter 13.pptx
PPTX
Array 2 hina
PPTX
PPTX
Unit4pptx__2024_11_ 11_10_16_09.pptx
arraysfor engineering students sdf ppt on arrays
Arrays
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
Array
Chapter 13.pptx
Array 2 hina
Unit4pptx__2024_11_ 11_10_16_09.pptx

Similar to Module_3_Arrays - Updated.pptx............ (20)

PPT
Basics of Data structure using C describing basics concepts
PDF
02 arrays
PPTX
Array ppt you can learn in very few slides.
PPT
358 33 powerpoint-slides_5-arrays_chapter-5
PPTX
arrays.pptx
PPT
Chapter 10.ppt
PPTX
array ppt of c programing language for exam
PDF
Array&amp;string
PPTX
Array
PPTX
C_Arrays.pptx
PDF
Array in C.pdf
PDF
Array.pdf
PPTX
PPTX
PPTX
ARRAY
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
PPTX
Array.pptx Array.pptxArray.pptx Array.pptxArray.pptxArray.pptx
PDF
SlideSet_4_Arraysnew.pdf
PPTX
Arrays in C language
PPTX
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
Basics of Data structure using C describing basics concepts
02 arrays
Array ppt you can learn in very few slides.
358 33 powerpoint-slides_5-arrays_chapter-5
arrays.pptx
Chapter 10.ppt
array ppt of c programing language for exam
Array&amp;string
Array
C_Arrays.pptx
Array in C.pdf
Array.pdf
ARRAY
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Array.pptx Array.pptxArray.pptx Array.pptxArray.pptxArray.pptx
SlideSet_4_Arraysnew.pdf
Arrays in C language
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
Ad

Recently uploaded (20)

PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Welding lecture in detail for understanding
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Well-logging-methods_new................
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
Foundation to blockchain - A guide to Blockchain Tech
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
UNIT 4 Total Quality Management .pptx
Welding lecture in detail for understanding
UNIT-1 - COAL BASED THERMAL POWER PLANTS
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
CH1 Production IntroductoryConcepts.pptx
Mechanical Engineering MATERIALS Selection
Lecture Notes Electrical Wiring System Components
Internet of Things (IOT) - A guide to understanding
bas. eng. economics group 4 presentation 1.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Well-logging-methods_new................
CYBER-CRIMES AND SECURITY A guide to understanding
Ad

Module_3_Arrays - Updated.pptx............

  • 1. 19/10/2024 Module 3 1 CSL 101- Computer Programming Computer Science and Engineering Indian Institute of Information Technology, Nagpur. Varsha Kushwah
  • 2. Arrays • Array is a collection of similar data items or elements. Or we can say that array is a collection of homogeneous data elements stored continuously under a single name. Array is defined as a set of homogeneous data items. • Need for an array – When the number of variables of same type and nature are more then it is difficult to handle them. So we need an array. Let us understand the use of array with an example: #include <stdio.h> int main() { int i, a; for(i=1;i<=5;i++) { printf(“nEnter the no.”); scanf("%d", &a); } printf("Print the value of a %d", a); return 0; }
  • 3. 19/10/2024 3 • Declared using the following syntax • type arrayname[size]; • Example: int a[6]; • Here a is the name of the array. • int is the data type of values which the array will store. • 6 is the size of array and [] is called as subscript operator. The size of array must be a constant. • The most important thing is that all the values in the array should be of the same type. Array Declaration
  • 4. 19/10/2024 4 • Initializing Arrays during Declaration • Syntax: type arrayname[size] = {list of values}; • Example: int marks[5]={90, 82, 78, 95, 88}; Storing Values In Arrays:
  • 5. 19/10/2024 5 • Inputting Values from the Keyboard int i, marks[5]; for(i=0; i<4; i++) scanf("%d", &marks[i]); • Assigning Values to Individual Elements int marks[3]; marks[0] = 60; marks[1] = 70; marks[2] = 65; Storing Values In Arrays:
  • 7. 19/10/2024 7 • Address of data element A[k] A[i] = BA + (i – lower_bound) * size • BA is the base address of the array A, • i is the index of the element of which we have to calculate the address, • size is the size of one element in memory, i.e. size of int is 4 Address of Array Elements
  • 8. 19/10/2024 8 • Given an array • int marks[8] = {99, 67, 78, 56, 88, 90, 34, 85} • Calculate the address of marks[4] when the base address = 1000 when integer size is 2 bytes. Solution marks[4] = 1000 + 2(4 – 0) = 1000 + 2(4) = 1008 Address of Array Elements
  • 9. 19/10/2024 9 • The length of an array is given by the number of elements stored in it • General formula • Length = upper_bound – lower_bound + 1 where • upper_bound is the index of the last element • lower_bound is the index of the first element • Let Age[5] be an array of integers Where Age[0] = 2, Age[1] = 5, Age[2] = 3, Age[3] = 1, Age[4] = 7. Calculate the length of the array. Calculating the Length of an Array
  • 10. POINTS TO BE NOTED : 1) Array name should be unique 2) The elements in the array should be of same type 3) Subscript (array size) cannot be negative 4) Subscript must always be an integer
  • 11. Types of Arrays 1) One Dimensional Array 2) Two Dimensional Array 3) Three Dimensional Array
  • 12. • Arrays whose elements are specified by one subscript are called One dimensional array or linear array. • Syntax : datatype arrayname[size]; • For Example : • Note : By default array index should starts with zero (0) Single or One Dimensional Arrays
  • 14. PROGRAM- Copy an array element to another array #include <stdio.h> int main() { int i, arr1[5] = {3, 4, 5, 1, 2}; int arr2[5]; for(i = 0; i < 5; i++) { arr2[i] = arr1[i]; printf("Elements in arr2: "); for(i = 0; i < 5; i++) { printf("%d ", arr2[i]); } return 0; }
  • 15. PROGRAM- Calculate the average int i, sum = 0; float average; int marks[5] = {4,5,1,2,3}; for(i = 0; i < 5; i++) sum += marks[i]; average = (float) sum/5; Finding the largest element int i, max; int arr[10] = {4,5,6,1,2,3,7,8,9,10}; max = arr[0]; for (int i = ; i < 10; i++) if (arr[i] > max) max = arr[i]; printf("Largest element = %d", max);
  • 16. Operations on Arrays • Traversing an array • Inserting an element in an array • Searching an element in an array • Deleting an element from an array • Merging two arrays • Sorting an array in ascending or descending order • Traversing an Array Accessing each and every element of the array Traversing data elements of an array A can include printing every element counting the total number of elements performing any process on these elements Example for (int i = 0; i < n; i++) printf(“%dn”, arr[i]);
  • 17. Search an element in an Array arr[5] arr[6] arr[7] arr[8] arr[9] arr[0] arr[1] arr[2] arr[3] arr[4] 120 124 128 132 136 10 20 30 40 50 100 104 108 112 116 flag = 0; for(i = 0; i < n; i++) if(arr[i] == val){ flag = 1; break; } if(flag == 1) printf("n Element is found at Position %d ", i+1);
  • 18. Inserting an element in an Array • Insertion at the End of an existing array n = 5; pos = 6; val = 60; arr[pos - 1] = val; n++; arr[5] arr[6] arr[7] arr[8] arr[9] arr[0] arr[1] arr[2] arr[3] arr[4] 120 124 128 132 136 10 20 30 40 50 100 104 108 112 116
  • 19. #include<stdio.h> int main() { int arr[10],i,n; printf("n Enter N Elements"); scanf("%d",&n); if(n<=10 && n>=1 ) { for(i=0;i<n;i++) { printf("enter arr[%d]=",i); scanf("%d",&arr[i]); } for(i=0;i<n;i++) {printf("%dn",arr[i]);} } else { printf("Envalid input");} return 0; } Enter N Elements : 3 Enter arr[0] : 2 Enter arr[1] : 5 Enter arr[2] : 3 2 5 3 Write a program for entering data into an array & Reading data from an array
  • 20. Array Initialization #include<stdio.h> #include<conio.h> void main() { int a[5]={10,20,30,40,50}; int i; for(i=0;i<5;i++) { printf("%dn",a[i]); } } 10 20 30 40 50 PROGRAM-ARRAY INITIALIZATION
  • 21. Output #include <stdio.h> int main() { int arr[] = {1, 2, 2, 4, 2, 5, 5, 4, 7, 8, 8}; int n = sizeof(arr) / sizeof(arr[0]); int visited[n]; for (int i = 0; i < n; i++) { visited[i] = 0; } for (int i = 0; i < n; i++) { if (visited[i] == 1) { continue; } int count = 1; for (int j = i + 1; j < n; j++) { if (arr[i] == arr[j]) { count++; visited[j] = 1; } } if (count > 1) { printf("Element %d is a duplicate and occurs %d times.n", arr[i], count); } } return 0; } Element 2 is a duplicate and occurs 3 times. Element 4 is a duplicate and occurs 2 times. Element 5 is a duplicate and occurs 2 times. Element 8 is a duplicate and occurs 2 times.
  • 22. 19/10/2024 22 Program to print the largest and second largest element of the array.
  • 23. • A Arrays whose elements are specified by two subscript such as row and column are called One dimensional array or linear array. • Row  means horizontally • Column  means vertically A two - dimensional array looks like a school time-table, consisting of rows and columns. A two – dimensional array is declared as – – data_type array_name[row_size][column_size]; – Ex: int marks[2][3]; Two Dimensional Arrays
  • 24. Two Dimensional Array Initialization • Ex: Store the marks obtained by three students in five different subjects int marks[3][5]; •Pictorial form of a two-dimensional array The result of the above assignment will be as follows :
  • 25. Storing a Two-Dimensional Array • Row major order – the elements of the array are stored row by row where n elements of the first row will occupy the first n locations •Column major order – the elements of the array are stored column by column where m elements of the first column will occupy the first m locations
  • 26. Address of Array Element • Formula to calculate the address of some element A[I][J] When Array indices start with 1. – Array elements are stored in row major order, Address(A[I][J]) = BA + w{N(I – 1) + (J – 1)} – Array elements are stored in column major order Address(A[I][J]) = BA + w{M(J – 1) + (I – 1)} where w is the number of bytes required to store one element, N is the number of columns, M is the number of rows, I and J are the subscripts of the array element.
  • 27. Address of Array Element • Consider a 20 x 5 two-dimensional array marks which has its base address = 1000 and the size of an element = 2. Compute the address of the element, marks[18][4] assuming that the elements are stored in row major order, and array indices start with 1. •Solution Address(A[I][J]) = BA + w{N(I – 1) + (J – 1)} Address(marks[18][4]) = 1000 + 2 {5(18 – 1) + (4 – 1)} = 1000 + 2 {5(17) + 3} = 1000 + 2 (88) = 1000 + 176 = 1176
  • 28. 19/10/2024 28 Write a “C” program to insert the elements matrices
  • 29. Write a “C” program to perform the addition of two matrices #include<stdio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j; printf("Input A - Matrixn"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("Input B - Matrixn"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); for(i=0;i<3;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j]; printf("Sum of A and B Matrix=n"); for(i=0;i<3;++i) { for(j=0;j<3;++j) printf("%d",c[i][j]); printf("n"); } }
  • 30. Write a “C” program to perform the subtraction of two matrices #include<stdio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j; printf("Input A - Matrixn"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("Input B - Matrixn"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); for(i=0;i<3;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]-b[i][j]; printf("Sum of A and B Matrix=n"); for(i=0;i<3;++i) { for(j=0;j<3;++j) printf("%d",c[i][j]); printf("n"); } }
  • 31. Write a “C” program to perform matrix multiplication using two dimensional array #include<stdio.h> void main() { int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q,k; printf("Input row and column of A matrix n"); scanf("%d %d",&n,&m); printf(" Input row and column of B matrix n"); scanf("%d %d",&p,&q); if(m==p) { printf(" Matrices can be Multiplied: n"); printf(" Input A-matrix n"); for(i=0;i<n;++i) for(j=0;j<m;++j) scanf("%d",&a[i][j]); printf(" Input B-matrix n"); for(i=0;i<p;++i) for(j=0;j<q;++j) scanf("%d",&b[i][j]); printf("The resultant matrix ist:n"); for(i=0;i<n;++i) { for(j=0;j<m;++j) { c[i][j]=0; for(k=0;k<p;++k) c[i][j]=c[i][j] + a[i][k] * b[k][j]; printf("%d",c[i][j]); } printf("n } else printf("Matrices cannot be multiplied n"); }
  • 32. Session Summary  Arrayname should be a unique and valid “C” Variable name  The number of elements in a multi dimensional array is the product of its subscripts  Arrays can be initialized to the same type in which they are declared  The character array receives the terminating ‘0’ in the string constant  The individual values in the array are called as elements  It is not necessary to specify the length of an array, explicitly in case if initializers are provided for the array during declaration itself

Editor's Notes

  • #2: flexibility, easing changes to programs easier to learn simpler to develop, maintain and analysize
  • #10: flexibility, easing changes to programs easier to learn simpler to develop, maintain and analysize
  • #11: flexibility, easing changes to programs easier to learn simpler to develop, maintain and analysize
  • #21: flexibility, easing changes to programs easier to learn simpler to develop, maintain and analysize