Md. Ahaduzzaman: Array
Md. Ahaduzzaman: Array
Ahaduzzaman
Dipu
ARRAY
What is the one-dimensional array?
Ex : int ar [3 ] ={2, 5, 9}
Element of array can also be initialized at the time of declaration as in the case of every other variable.
Int ar [3 ] = {8 ,5, 6}
If user input a variable for rum the program this called Run time Initialization .After input the value
program should be run.
Some Program
Output
Output
Write a program to find the ascending number using arrays (Bubble sorting)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ar [5] = {5,2,7,1,10};
int i,j,temp;
for (i=0; i<5-1;i++){
for (j=0; j<5-i-1;j++){
if (ar[j]>ar[j+1]){
temp = ar[j];
ar[j] = ar[j+1];
ar[j+1] = temp;
}
}
}
for (i=0; i<5; i++){
printf("%d\t",ar[i]);
}
return 0;
}
Write a program for addition of two Matrix using arrays
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ar1[3][3];
int ar2[3][3];
int result_matrix[3][3];
int i,j;
/*Input of first matrix*/
for (i=0;i<3;i++){
for (j=0;j<3;j++){
scanf("%d",&ar1[i][j]);
}
}
/*Input of second matrix*/
for (i=0;i<3;i++){
for (j=0;j<3;j++){
scanf("%d",&ar2[i][j]);
}
}
/*matrix addition*/
for (i=0;i<3;i++){
for (j=0;j<3;j++){
result_matrix[i][j]= ar1[i][j]+ar2[i][j];
}
}
/*matrix output*/
for (i=0;i<3;i++){
printf("\n");
for (j=0;j<3;j++){
printf("%d\t", result_matrix[i][j] );
}
}
return 0;
}
Output
Write a program to find the Transpose Matrix. (The size of matrix to defined by
user)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m, n, i,j, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrix :\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of matrix:\n");
for (i= 0; i < m; i++){
for(j = 0; j < n; j++){
scanf("%d",&matrix[i][j]);
}
}
for (i = 0; i < m; i++){
for( j = 0 ; j < n ; j++){
transpose[j][i] = matrix[i][j];
}
}
printf("Transpose of matrix :-\n");
for (i = 0; i < n; i++) {
printf("\n");
for (j = 0; j < m; j++){
printf("%d\t",transpose[i][j]);
}
}
return 0;
}
Output
Function
Function definition:
Header
Name and type
Parameter / Argument
Function body
# include <stdio.h>
Int add (int a, int b); [function header ] & (int x, int y) is Parameter / Argument
Int main ( ) {
Int a, b, sum;
return 0;
}
int add (int a, int b) {
Function Declaration :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n, fact = 1;
printf("Enter a number to calculate the factorial:\n");
scanf("%d", &n);
for (i= 1; i <= n; i++)
fact = fact * i;
printf("Ans The Factorial = %d\n", fact);
return 0;
}
Output