SlideShare a Scribd company logo
Rumman Ansari
All arrays consist of contiguous memory locations. The lowest address corresponds
to the first element and the highest address to the last element.
Declaring Arrays
Type arrayName [ arraySize ];
double balance[10];
int balance[16];
float balance[15];
Array
one-dimensional array
two-dimensional array
three-dimensional array
Initialization of one-dimensional array:
int age[5]={2,4,34,3,4};
int age[]={2,4,34,3,4};
Accessing array elements
scanf("%d",&age[i]);
printf("%d",age[i]);
Example of array in C programming
C program to find the sum marks of n students using arrays
#include <stdio.h>
int main(){
int marks[10],i,n,sum=0;
printf("Enter number of students: ");
scanf("%d",&n);
for(i=0;i<n;++i){
printf("Enter marks of student%d: ",i+1);
scanf("%d",&marks[i]);
sum+=marks[i];
}
printf("Sum= %d",sum);
return 0;
}
Initialization of two-dimensional array:
float a[2][6];
int c[2][3]={ {1,3,0}, {-1,5,9} };
OR
int c[][3]={ {1,3,0}, {-1,5,9} };
OR
int c[2][3]={1,3,0,-1,5,9};
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
#include <stdio.h>
int main ()
{
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
/* output each array element's value */
for ( i = 0; i < 5; i++ )
{
for ( j = 0; j < 2; j++ )
{
printf("a[%d][%d] = %dn", i,j, a[i][j] );
}
}
return 0;
}
Accessing Two-Dimensional Array Elements:
Write a C program to find sum of two matrix of order 2*2 using multidimensional arrays where, elements of matrix
are entered by user.
#include <stdio.h>
int main(){
float a[2][2], b[2][2], c[2][2];
int i,j;
printf("Enter the elements of 1st matrixn");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("Enter a%d%d: ",i+1,j+1);
scanf("%f",&a[i][j]);
}
printf("Enter the elements of 2nd matrixn");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("Enter b%d%d: ",i+1,j+1);
scanf("%f",&b[i][j]);
}
for(i=0;i<2;++i)
for(j=0;j<2;++j){
c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */
}
printf("nSum Of Matrix:");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("%.1ft",c[i][j]);
if(j==1) /* To display matrix sum in order. */
printf("n");
}
return 0;
}
#include <stdio.h>
int main(){
int a[2][2], b[2][2], c[2][2];
int i,j;
printf("Enter the elements of 1st matrixn");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("Enter a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("Enter the elements of 2nd matrixn");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("Enter b%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
Write a C program to find sum of two matrix of order 2*2 using multidimensional
arrays where, elements of matrix are entered by user.
// this is 1st matrix
printf(" this is 1st matrix nn");
for(i=0;i<2;++i)
{
for(j=0;j<2;++j)
{
printf("%d ",a[i][j]);
}
printf(" n");
}
//this is second matrix
printf(" this is second matrix nn");
for(i=0;i<2;++i)
{
for(j=0;j<2;++j)
{
printf("%d ",b[i][j]);
}
printf(" n");
}
for(i=0;i<2;++i)
for(j=0;j<2;++j){
/* Writing the elements of multidimensional array using loop. */
c[i][j]=a[i][j]+b[i][j];
/* Sum of corresponding elements of two arrays. */
}
printf("nSum Of Matrix:");
for(i=0;i<2;++i)
{
for(j=0;j<2;++j)
{
printf("%dt",c[i][j]);
}
printf(" n");
}
return 0;
}
Display Largest Element of an array
// Display Largest Element of an array
#include <stdio.h>
int main(){
int i,n;
float arr[100];
printf("Enter total number of elements(1 to 100): ");
scanf("%d",&n);
printf("n");
for(i=0;i<n;++i) /* Stores number entered by user. */
{
printf("Enter Number %d: ",i+1);
scanf("%f",&arr[i]);
}
for(i=1;i<n;++i) /* Loop to store largest number to arr[0] */
{
if(arr[0]<arr[i]) /* Change < to > if you want to find smallest element*/
arr[0]=arr[i];
}
printf("Largest element = %.2f",arr[0]);
return 0;
}
Find Transpose of a Matrix
#include <stdio.h>
int main()
{
int a[10][10], trans[10][10], r, c, i, j;
printf("Enter rows and column of matrix: ");
scanf("%d %d", &r, &c);
/* Storing element of matrix entered by user in array a[][].
*/
printf("nEnter elements of matrix:n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
/* Displaying the matrix a[][] */
printf("nEntered Matrix: n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ",a[i][j]);
if(j==c-1)
printf("nn");
}
/* Finding transpose of matrix a[][] and storing it in
array trans[][]. */
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
trans[j][i]=a[i][j];
}
/* Displaying the transpose,i.e, Displaying array
trans[][]. */
printf("nTranspose of Matrix:n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",trans[i][j]);
if(j==r-1)
printf("nn");
}
return 0;
}
Multiply to matrix in C programming
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&r2, &c2);
/* If colum of first matrix in not equal to row of second matrix, asking user to enter
the size of matrix again. */
while (c1!=r2)
{
printf("Error! column of first matrix not equal to row of second.nn");
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&r2, &c2);
}
/* Storing elements of first matrix. */
printf("nEnter elements of matrix 1:n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
/* Storing elements of second matrix. */
printf("nEnter elements of matrix 2:n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
/* Initializing elements of matrix mult to 0.*/
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
mult[i][j]=0;
}
/* Multiplying matrix a and b and storing in array mult. */
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
mult[i][j]+=a[i][k]*b[k][j];
}
/* Displaying the multiplication of two matrix. */
printf("nOutput Matrix:n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ",mult[i][j]);
if(j==c2-1)
printf("nn");
}
return 0;
}

More Related Content

What's hot (20)

PPTX
Decision making and branching
Saranya saran
 
PDF
7 functions
MomenMostafa
 
PDF
C programming
Samsil Arefin
 
PPTX
Intro to c chapter cover 1 4
Hazwan Arif
 
PDF
1 introducing c language
MomenMostafa
 
PPT
Functions and pointers_unit_4
Saranya saran
 
PDF
8 arrays and pointers
MomenMostafa
 
PPSX
Concepts of C [Module 2]
Abhishek Sinha
 
PPT
Mesics lecture 5 input – output in ‘c’
eShikshak
 
PDF
9 character string &amp; string library
MomenMostafa
 
PDF
6 c control statements branching &amp; jumping
MomenMostafa
 
PDF
Understanding storage class using nm
mohamed sikander
 
PDF
4 operators, expressions &amp; statements
MomenMostafa
 
PPTX
Input output statement in C
Muthuganesh S
 
DOC
C tech questions
vijay00791
 
DOCX
C interview question answer 2
Amit Kapoor
 
PPTX
4. chapter iii
Chhom Karath
 
PPTX
3. chapter ii
Chhom Karath
 
PDF
Functions
SANTOSH RATH
 
PPTX
Function basics
mohamed sikander
 
Decision making and branching
Saranya saran
 
7 functions
MomenMostafa
 
C programming
Samsil Arefin
 
Intro to c chapter cover 1 4
Hazwan Arif
 
1 introducing c language
MomenMostafa
 
Functions and pointers_unit_4
Saranya saran
 
8 arrays and pointers
MomenMostafa
 
Concepts of C [Module 2]
Abhishek Sinha
 
Mesics lecture 5 input – output in ‘c’
eShikshak
 
9 character string &amp; string library
MomenMostafa
 
6 c control statements branching &amp; jumping
MomenMostafa
 
Understanding storage class using nm
mohamed sikander
 
4 operators, expressions &amp; statements
MomenMostafa
 
Input output statement in C
Muthuganesh S
 
C tech questions
vijay00791
 
C interview question answer 2
Amit Kapoor
 
4. chapter iii
Chhom Karath
 
3. chapter ii
Chhom Karath
 
Functions
SANTOSH RATH
 
Function basics
mohamed sikander
 

Viewers also liked (20)

PPTX
How c program execute in c program
Rumman Ansari
 
PPTX
C Programming Language Step by Step Part 1
Rumman Ansari
 
PPTX
C Programming Language Part 5
Rumman Ansari
 
PPTX
C Programming Language Step by Step Part 3
Rumman Ansari
 
PPTX
Basic c programming and explanation PPT1
Rumman Ansari
 
PPT
Basics of C programming
avikdhupar
 
PPTX
My first program in c, hello world !
Rumman Ansari
 
PDF
Build Features, Not Apps
Natasha Murashev
 
PPTX
Medicaid organization profile
Anurag Byala
 
ODP
C language. Introduction
Alexey Bovanenko
 
PPTX
Основи мови Ci
Escuela
 
PPTX
C programming tutorial for beginners
Thiyagarajan Soundhiran
 
PPT
C program compiler presentation
Rigvendra Kumar Vardhan
 
PPT
Software testing Training Syllabus Course
TOPS Technologies
 
PPT
Steps for Developing a 'C' program
Sahithi Naraparaju
 
PPT
Introduction to programming with c,
Hossain Md Shakhawat
 
PPTX
Programming in C Basics
Bharat Kalia
 
PDF
Composicio Digital _Practica Pa4
Marcos Baldovi
 
PPTX
Introduction to Basic C programming 02
Wingston
 
How c program execute in c program
Rumman Ansari
 
C Programming Language Step by Step Part 1
Rumman Ansari
 
C Programming Language Part 5
Rumman Ansari
 
C Programming Language Step by Step Part 3
Rumman Ansari
 
Basic c programming and explanation PPT1
Rumman Ansari
 
Basics of C programming
avikdhupar
 
My first program in c, hello world !
Rumman Ansari
 
Build Features, Not Apps
Natasha Murashev
 
Medicaid organization profile
Anurag Byala
 
C language. Introduction
Alexey Bovanenko
 
Основи мови Ci
Escuela
 
C programming tutorial for beginners
Thiyagarajan Soundhiran
 
C program compiler presentation
Rigvendra Kumar Vardhan
 
Software testing Training Syllabus Course
TOPS Technologies
 
Steps for Developing a 'C' program
Sahithi Naraparaju
 
Introduction to programming with c,
Hossain Md Shakhawat
 
Programming in C Basics
Bharat Kalia
 
Composicio Digital _Practica Pa4
Marcos Baldovi
 
Introduction to Basic C programming 02
Wingston
 
Ad

Similar to C Programming Language Part 8 (20)

PPTX
Arrays 2d Arrays 2d Arrays 2d Arrrays 2d
LakshayBhardwaj39
 
PDF
2D array
A. S. M. Shafi
 
PDF
CP Handout#9
trupti1976
 
PPTX
array ppt of c programing language for exam
shimishtrivedi
 
PPTX
Array,MULTI ARRAY, IN C
naveed jamali
 
PDF
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
PPTX
Array
Anil Dutt
 
DOC
Basic c programs updated on 31.8.2020
vrgokila
 
PPTX
Arrays
RaziyasultanaShaik
 
PDF
PPS Arrays Matrix operations
Sreedhar Chowdam
 
PPTX
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
PPT
presentation_arrays_1443553113_140676.ppt
NamakkalPasanga
 
PPTX
arrays in c programming - example programs
stalin721831
 
PPTX
Unit4pptx__2024_11_ 11_10_16_09.pptx
GImpact
 
PPT
Arrays
Saranya saran
 
PDF
2Darrays.pdf. data structure using c programming
maha280307
 
PPT
Array 31.8.2020 updated
vrgokila
 
PDF
C Language Lecture 10
Shahzaib Ajmal
 
PDF
Array Programs.pdf
RajKamal557276
 
Arrays 2d Arrays 2d Arrays 2d Arrrays 2d
LakshayBhardwaj39
 
2D array
A. S. M. Shafi
 
CP Handout#9
trupti1976
 
array ppt of c programing language for exam
shimishtrivedi
 
Array,MULTI ARRAY, IN C
naveed jamali
 
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
Array
Anil Dutt
 
Basic c programs updated on 31.8.2020
vrgokila
 
PPS Arrays Matrix operations
Sreedhar Chowdam
 
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
presentation_arrays_1443553113_140676.ppt
NamakkalPasanga
 
arrays in c programming - example programs
stalin721831
 
Unit4pptx__2024_11_ 11_10_16_09.pptx
GImpact
 
2Darrays.pdf. data structure using c programming
maha280307
 
Array 31.8.2020 updated
vrgokila
 
C Language Lecture 10
Shahzaib Ajmal
 
Array Programs.pdf
RajKamal557276
 
Ad

More from Rumman Ansari (17)

PDF
Sql tutorial
Rumman Ansari
 
PDF
C programming exercises and solutions
Rumman Ansari
 
PDF
Java Tutorial best website
Rumman Ansari
 
DOCX
Java Questions and Answers
Rumman Ansari
 
DOCX
servlet programming
Rumman Ansari
 
PPTX
C program to write c program without using main function
Rumman Ansari
 
PPTX
Steps for c program execution
Rumman Ansari
 
PPTX
Pointer in c program
Rumman Ansari
 
PPTX
What is token c programming
Rumman Ansari
 
PPTX
What is identifier c programming
Rumman Ansari
 
PPTX
What is keyword in c programming
Rumman Ansari
 
PPTX
Type casting in c programming
Rumman Ansari
 
DOCX
C Programming
Rumman Ansari
 
PPTX
Tail recursion
Rumman Ansari
 
PPTX
Tail Recursion in data structure
Rumman Ansari
 
PDF
Spyware manual
Rumman Ansari
 
PPTX
Linked list
Rumman Ansari
 
Sql tutorial
Rumman Ansari
 
C programming exercises and solutions
Rumman Ansari
 
Java Tutorial best website
Rumman Ansari
 
Java Questions and Answers
Rumman Ansari
 
servlet programming
Rumman Ansari
 
C program to write c program without using main function
Rumman Ansari
 
Steps for c program execution
Rumman Ansari
 
Pointer in c program
Rumman Ansari
 
What is token c programming
Rumman Ansari
 
What is identifier c programming
Rumman Ansari
 
What is keyword in c programming
Rumman Ansari
 
Type casting in c programming
Rumman Ansari
 
C Programming
Rumman Ansari
 
Tail recursion
Rumman Ansari
 
Tail Recursion in data structure
Rumman Ansari
 
Spyware manual
Rumman Ansari
 
Linked list
Rumman Ansari
 

Recently uploaded (20)

PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
Distribution reservoir and service storage pptx
dhanashree78
 
PDF
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
PPTX
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PPTX
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
PDF
Digital water marking system project report
Kamal Acharya
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
Design Thinking basics for Engineers.pdf
CMR University
 
Distribution reservoir and service storage pptx
dhanashree78
 
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
Digital water marking system project report
Kamal Acharya
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 

C Programming Language Part 8

  • 2. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Declaring Arrays Type arrayName [ arraySize ]; double balance[10]; int balance[16]; float balance[15];
  • 3. Array one-dimensional array two-dimensional array three-dimensional array Initialization of one-dimensional array: int age[5]={2,4,34,3,4}; int age[]={2,4,34,3,4};
  • 4. Accessing array elements scanf("%d",&age[i]); printf("%d",age[i]); Example of array in C programming C program to find the sum marks of n students using arrays #include <stdio.h> int main(){ int marks[10],i,n,sum=0; printf("Enter number of students: "); scanf("%d",&n); for(i=0;i<n;++i){ printf("Enter marks of student%d: ",i+1); scanf("%d",&marks[i]); sum+=marks[i]; } printf("Sum= %d",sum); return 0; }
  • 5. Initialization of two-dimensional array: float a[2][6]; int c[2][3]={ {1,3,0}, {-1,5,9} }; OR int c[][3]={ {1,3,0}, {-1,5,9} }; OR int c[2][3]={1,3,0,-1,5,9}; int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /* initializers for row indexed by 2 */ }; int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
  • 6. #include <stdio.h> int main () { /* an array with 5 rows and 2 columns*/ int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; int i, j; /* output each array element's value */ for ( i = 0; i < 5; i++ ) { for ( j = 0; j < 2; j++ ) { printf("a[%d][%d] = %dn", i,j, a[i][j] ); } } return 0; } Accessing Two-Dimensional Array Elements:
  • 7. Write a C program to find sum of two matrix of order 2*2 using multidimensional arrays where, elements of matrix are entered by user. #include <stdio.h> int main(){ float a[2][2], b[2][2], c[2][2]; int i,j; printf("Enter the elements of 1st matrixn"); for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("Enter a%d%d: ",i+1,j+1); scanf("%f",&a[i][j]); } printf("Enter the elements of 2nd matrixn"); for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("Enter b%d%d: ",i+1,j+1); scanf("%f",&b[i][j]); } for(i=0;i<2;++i) for(j=0;j<2;++j){ c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */ } printf("nSum Of Matrix:"); for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("%.1ft",c[i][j]); if(j==1) /* To display matrix sum in order. */ printf("n"); } return 0; }
  • 8. #include <stdio.h> int main(){ int a[2][2], b[2][2], c[2][2]; int i,j; printf("Enter the elements of 1st matrixn"); for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("Enter a%d%d: ",i+1,j+1); scanf("%d",&a[i][j]); } printf("Enter the elements of 2nd matrixn"); for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("Enter b%d%d: ",i+1,j+1); scanf("%d",&b[i][j]); } Write a C program to find sum of two matrix of order 2*2 using multidimensional arrays where, elements of matrix are entered by user.
  • 9. // this is 1st matrix printf(" this is 1st matrix nn"); for(i=0;i<2;++i) { for(j=0;j<2;++j) { printf("%d ",a[i][j]); } printf(" n"); } //this is second matrix printf(" this is second matrix nn"); for(i=0;i<2;++i) { for(j=0;j<2;++j) { printf("%d ",b[i][j]); } printf(" n"); }
  • 10. for(i=0;i<2;++i) for(j=0;j<2;++j){ /* Writing the elements of multidimensional array using loop. */ c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */ } printf("nSum Of Matrix:"); for(i=0;i<2;++i) { for(j=0;j<2;++j) { printf("%dt",c[i][j]); } printf(" n"); } return 0; }
  • 11. Display Largest Element of an array // Display Largest Element of an array #include <stdio.h> int main(){ int i,n; float arr[100]; printf("Enter total number of elements(1 to 100): "); scanf("%d",&n); printf("n"); for(i=0;i<n;++i) /* Stores number entered by user. */ { printf("Enter Number %d: ",i+1); scanf("%f",&arr[i]); } for(i=1;i<n;++i) /* Loop to store largest number to arr[0] */ { if(arr[0]<arr[i]) /* Change < to > if you want to find smallest element*/ arr[0]=arr[i]; } printf("Largest element = %.2f",arr[0]); return 0; }
  • 12. Find Transpose of a Matrix #include <stdio.h> int main() { int a[10][10], trans[10][10], r, c, i, j; printf("Enter rows and column of matrix: "); scanf("%d %d", &r, &c); /* Storing element of matrix entered by user in array a[][]. */ printf("nEnter elements of matrix:n"); for(i=0; i<r; ++i) for(j=0; j<c; ++j) { printf("Enter elements a%d%d: ",i+1,j+1); scanf("%d",&a[i][j]); }
  • 13. /* Displaying the matrix a[][] */ printf("nEntered Matrix: n"); for(i=0; i<r; ++i) for(j=0; j<c; ++j) { printf("%d ",a[i][j]); if(j==c-1) printf("nn"); } /* Finding transpose of matrix a[][] and storing it in array trans[][]. */ for(i=0; i<r; ++i) for(j=0; j<c; ++j) { trans[j][i]=a[i][j]; }
  • 14. /* Displaying the transpose,i.e, Displaying array trans[][]. */ printf("nTranspose of Matrix:n"); for(i=0; i<c; ++i) for(j=0; j<r; ++j) { printf("%d ",trans[i][j]); if(j==r-1) printf("nn"); } return 0; }
  • 15. Multiply to matrix in C programming #include <stdio.h> int main() { int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k; printf("Enter rows and column for first matrix: "); scanf("%d%d", &r1, &c1); printf("Enter rows and column for second matrix: "); scanf("%d%d",&r2, &c2); /* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */ while (c1!=r2) { printf("Error! column of first matrix not equal to row of second.nn"); printf("Enter rows and column for first matrix: "); scanf("%d%d", &r1, &c1); printf("Enter rows and column for second matrix: "); scanf("%d%d",&r2, &c2); }
  • 16. /* Storing elements of first matrix. */ printf("nEnter elements of matrix 1:n"); for(i=0; i<r1; ++i) for(j=0; j<c1; ++j) { printf("Enter elements a%d%d: ",i+1,j+1); scanf("%d",&a[i][j]); } /* Storing elements of second matrix. */ printf("nEnter elements of matrix 2:n"); for(i=0; i<r2; ++i) for(j=0; j<c2; ++j) { printf("Enter elements b%d%d: ",i+1,j+1); scanf("%d",&b[i][j]); }
  • 17. /* Initializing elements of matrix mult to 0.*/ for(i=0; i<r1; ++i) for(j=0; j<c2; ++j) { mult[i][j]=0; } /* Multiplying matrix a and b and storing in array mult. */ for(i=0; i<r1; ++i) for(j=0; j<c2; ++j) for(k=0; k<c1; ++k) { mult[i][j]+=a[i][k]*b[k][j]; }
  • 18. /* Displaying the multiplication of two matrix. */ printf("nOutput Matrix:n"); for(i=0; i<r1; ++i) for(j=0; j<c2; ++j) { printf("%d ",mult[i][j]); if(j==c2-1) printf("nn"); } return 0; }