Sei sulla pagina 1di 7

Md.

Ahaduzzaman
Dipu
ARRAY
What is the one-dimensional array?

A one-dimensional array is organized linearly and only one direction.

Ex : int ar [3 ] ={2, 5, 9}

Declaration of one-dimensional array.

Declaring an array means specifying three things:

Data type int, float , char, double.


Name - to identify the array.
Size The maximum number of value that the array can hold.

Type Name [size] : int ar [5]

Initialization of one-dimensional array.

Element of array can also be initialized at the time of declaration as in the case of every other variable.

Array are initialized by writing : Type Name [size] = {list of value}

Int ar [3 ] = {8 ,5, 6}

Run time Initialization.

If user input a variable for rum the program this called Run time Initialization .After input the value
program should be run.

Ex: int ar [5]

for (i=0; i<5; i++){

scanf (%d, &ar[i]);

Initialization of two-dimensional array.


A two two-dimensional array is initialized in the same way of one-dimensional array.
Ex : int ar [2] [3] = {5, 7,9} ,{4, 6, 8}
The given two-dimensional array has 2 rows and 3 columns.
Memory layout of two-dimensional array.

int ar [2] [3] = {2, 3, 4}, {1, 3, 5}

ar [0] [0] = 2 ar [1] [0] = 1


ar [0] [1] = 3 ar [1] [1] = 3

ar [0] [2] = 4 ar [1] [2] = 5

Some Program

Write a program to read and display N number using array.


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

int main()
{
int arr [50];
int n,i;
printf("\n Enter the number of Element :");
scanf("%d",&n);
for (i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("\n The arry element are:");
for(i=0;i<n;i++){
printf("%d\t",arr[i]);
}
return 0;
}

Output

Write a program to find the mean of 10 number using arrays


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

int main()
{
int arr [10];
int i,sum=0,avg;
printf("Enter the 10 number:");
for (i=0;i<10;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<10;i++){
sum=sum+arr[i];
avg=sum/10;
printf("\n Mean is:%d",avg);
}
return 0;
}

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

In following Example I have indicate following terms:

# include <stdio.h>

Int add (int a, int b); [function header ] & (int x, int y) is Parameter / Argument

Int main ( ) {

Int a, b, sum;

Int add (20, 25); [function calling]

printf (%d %d , & a, & b);

return 0;

}
int add (int a, int b) {

Sum = a+b; in this {} { function body}

return sum; [function return type]

Function Declaration :

return _data_type function _name (data _type variable1 , variable2);

int add (int a, int b);

Write a program to find the Factorial Number.

#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

Md. Ahaduzzaman Dipu

Potrebbero piacerti anche