0% found this document useful (0 votes)
34 views

Single Demensional Array (SDA) : TH TH TH TH

The document provides information about arrays in C programming. It defines arrays as collections of similar data types that use consecutive memory locations. It discusses single dimensional, double dimensional, and multi-dimensional arrays. It provides examples of declaring, initializing, traversing, and performing operations like sorting, searching, and calculating sums on arrays. It also includes sample code snippets to implement various array operations like insertion, deletion, averaging, finding maximum/minimum elements, and converting numbers to binary representation using arrays.

Uploaded by

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

Single Demensional Array (SDA) : TH TH TH TH

The document provides information about arrays in C programming. It defines arrays as collections of similar data types that use consecutive memory locations. It discusses single dimensional, double dimensional, and multi-dimensional arrays. It provides examples of declaring, initializing, traversing, and performing operations like sorting, searching, and calculating sums on arrays. It also includes sample code snippets to implement various array operations like insertion, deletion, averaging, finding maximum/minimum elements, and converting numbers to binary representation using arrays.

Uploaded by

Sandeep sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

AIM POINT INFOSYSTEM PVT. LTD.

2019

ARRAY

Some Important points of array :

1:- Array is not a primitive data type, because array is an abstract data type(ADT) .
2:- Array is a collection of or group of similar data type .
3:- Array always provide consecutive memory.
4:- array always started by 0th element.
5:- The name of array refer to the base address(starting address of allocated
Memory.
6:- Array always referred to the static memory means we can not change the
Size of memory or deallocated memory during execution.
7:- Array doesn’t not support to the bound checking.

Type Of Array
1. Single Demensional Array ( SDA )
2. Double Demensional Array ( DDA )
3. Multi Demensional Array ( MDA )

Single Demensional Array ( SDA )

Syntax : datatype array_name[no of ele];

Declaration : int x[10];

int x[10];
Address 100 102 104 106 108 110 112 114 116 118
X 2 5 6 10 1 12 5 11 3 16
Position 0 1 2
3 4 5 6 7 8 9

Initialization of single dimensional array :


int X[10]; //garbage
int X[10]={10,3,52,7,89,12,4,56,90,33}; // initialize from 0th pos to 9th pos
int x[10]={2,7,1,15,6}; // initialize from 0th pos to 4th pos and by default remaining
// ele will be 0
int x[10]={12,3, , , 7,9}; // error
int X[10]={ }; // error
int X[10]={0}; // all 0’s
int X[ ]; // error
int X[ ]={12,3,10}; // valid , because compiler will allocate array 0f 4 elements
int X[5]={1,2,3,4,5,6,7,8,9}; // error

FACULTY : SANDEEP SAHU( M.Tech(CSE))Page 1


AIM POINT INFOSYSTEM PVT. LTD. 2019

#WAP to enter all elements in an array and print it and printf in reverse also.
#include<stdio.h>
void main( )
{ int x[10 ] , i;
printf("\nEnter Array X ");

for(i=0 ; i<10 ; i++)


scanf("%d", &x[ i ] ); // “&x[ i ] “ means address of ith element
printf("\n entered array is\n ");
for(i=0; i<10 ; i++)
printf(" %d", x[ i ] ); // “x[ i ] “ means value at ith position
printf("\nReverse array is \n");
for( i=9 ; i>=0 ;i -- ) //reverse printing of array
printf(" %d", x[ i ] );
}

#WAP to reverse the elements of array;


#include<stdio.h>
void main( )
{ int x[10 ] , i , j , t ;
printf("\nEnter Array X ");
for(i=0 ; i<10 ; i++)
scanf("%d", &x[ i ] );
for( i=0 , j = 9; j > i ; i++ , j- -)
{
t = x [ i ];
x[i]=x[j];
x[j] =t;
}
printf("\nReverse array is \n");
for( i=0 ; i<10 ;i ++ ) //reverse printing of array
printf(" %d", x[ i ] );
}

#WAP to calculate the avg of 10 nos. in an array


#include<stdio.h>
void main( )
{ int x[10 ] , i , sum = 0;
printf("\nEnter Array X ");
for(i=0 ; i<10 ; i++)
{
scanf("%d", &x[ i ] );
sum = sum + x [ i ] ;
}
printf("\nAverage Of Array is %f " , sum / 10.0 );
}
FACULTY : SANDEEP SAHU( M.Tech(CSE))Page 2
AIM POINT INFOSYSTEM PVT. LTD. 2019

#WAP to find the biggest no and its position from array of 10 elements
#include<stdio.h>
void main( )
{ int x[10 ] , i , big = -32768 , pos ;
printf("\nEnter Array X ");
for(i=0 ; i<10 ; i++)
{ scanf("%d", &x[ i ] );
if ( x[ i ] > big )
{ big = x[ i ] ;
pos = i + 1 ;
}
}
printf("\nBiggest no = %d and position = %d " , big , pos );
}

#WAP to find the smallest no and its position from array of 10 elements
#include<stdio.h>
void main( )
{ int x[10 ], i ,small = 32767 , pos ;
printf("\nEnter Array X ");
for(i=0 ; i<10 ; i++)
{ scanf("%d", &x[ i ] );
if ( x[ i ] < small )
{ small = x[ i ] ;
pos = i + 1 ;
}
}
printf("\nSmallest no = %d and position = %d " , small , pos );
}

#WAP to search an element in an array of 10 elements.( using linear search)


#include<stdio.h>
void main( )
{ int x[10] ,i ,se;
printf("\nEnte 10 Number:");
for(i=0;i<10;i++)
scanf("%d" , &x[ i ] );
printf("\nEnter searching no:");
scanf("%d" , &se) ;
for(i=0;i<10;i++)
{ if (se == x[ i ])
{ printf("\n element is found and position= %d " , i+1 );
return;
}
}
printf("\n element not found");
FACULTY : SANDEEP SAHU( M.Tech(CSE))Page 3
AIM POINT INFOSYSTEM PVT. LTD. 2019

}
#WAP to sort the all elements in an arrar( using bubble sort)
#include<stdio.h>
void main()
{ int x[10],i,j,t;
printf("\nEnter 10 number:");
for(i=0;i<10;i++)
scanf("%d" , &x[ i ] );
for( i=0 ; i<10 ; i++ )
{ for( j=0 ; j < 9 - i ; j++ )
{ if(x[ j ] > x[ j+1] )
{ t =x[ j ] ;
x[ j ] =x [ j+1 ] ;
x[ j+1 ] = t ;
}
}//end of j loop
}//end of i loop
printf("\n after sorting");
for( i=0; i<10; i++)
printf(" %d" , x[ i ]);

#WAP to sort the all elements in an array (using selection sort)


#include<stdio.h>
void main()
{ int x[10] , i , j ,t ;
printf("\nEnter 10 number:");
for(i=0;i<10;i++)
scanf("%d", &x[ i ] );

for( i=0; i<9; i++)


{ for( j=i+1; j<10 ; j++ )
{ if(x[ j ] < x[ i ] )
{ t = x[ i ];
x[ i ] = x[ j ];
x[ j ] = t ;
}
}//end of j loop
}
printf("\n after sorting:");
for( i=0; i<10; i++)
printf(" %d" , x[ i ] );
}

FACULTY : SANDEEP SAHU( M.Tech(CSE))Page 4


AIM POINT INFOSYSTEM PVT. LTD. 2019

#WAP to insert an element in array by valid position.


#include<stdio.h>
void main()
{ int x[10],i,pos;
printf("\nEnter 9 number:");
for(i=0;i<9;i++)
scanf("%d",&x[i]);
printf("\n enter position");
scanf("%d",&pos);
if(pos<1||pos>10)
{ printf("invalid position");
return;
}
for( i =9 ; i>=pos ; i-- )
x[ i ] = x[ i -1] ;
printf("enter new value");
scanf("%d", &x[ pos-1] );

printf("\nAfter Insertion Array is: ");


for(i=0;i<10;i++)
printf(" %d", x[ i ]);
}

#WAP to delete an element from array by valid position.


#include<stdio.h>
void main()
{ int x[10],i,pos;
printf("\nEnter 10 number:");
for(i=0;i<9;i++)
scanf("%d",&x[i]);
printf("\n enter position");
scanf("%d",&pos);
if(pos<1||pos>10)
{ printf("invalid position");
return;
}
for( i = pos-1 ; i < 9 ; i++ )
x[ i ] = x[ i +1] ;
printf("\nAfter Insertion Array is: ");
for( i=0;i < 9 ;i++)
printf(" %d", x[ i ]);
}

FACULTY : SANDEEP SAHU( M.Tech(CSE))Page 5


AIM POINT INFOSYSTEM PVT. LTD. 2019

#WAP to convert any decimal no into binary no in an array.


#include<stdio.h>
#include<conio.h>
void main()
{
int x[16],no,i=0;
clrscr();
printf("enter any no");
scanf("%d",&no);
while(no>0)
{ x[ i++ ] = no%2;
no/=2;
}
i-- ;
printf("\n binary no is is:");
for( ; i>=0 ; i-- )
printf(" %d" , x[ i ] );
}

DOUBLE DIMENSION ARRAY

Two dimension array is called DDA. Or collection of single dimension array is called
DDA
int X [ 3 ] [ 4 ] ;
2 3 7 4
1 8 9 3
6 2 4 5
Actual format
2 3 7 4 1 8 9 3 6 2 4 5
0 1 2 3 4 5 6 7 8 9 10 11

INITALIZATION OF DDA
int x[ 3 ] [ 4 ] ;
int x[ 3 ] [ 4 ] = {2,3,7,4,1,8,9,3,6,2,4,5};
int x[ 3 ] [ 4 ] ={ {2,3,4,5},{1,8,9,3},{6,2,4,5} };
int x[ 3 ] [ 4 ] ={2,3,7,4,1,8,3,6};
int x[ 3 ] [ 4 ] ={ {2,3,4},{1,2},{6,9} }
int x[ 4 ] [ ] = { 5,7,1,2,8,9 };

FACULTY : SANDEEP SAHU( M.Tech(CSE))Page 6


AIM POINT INFOSYSTEM PVT. LTD. 2019

int x[ ][ 3 ] = { 5,7,1,2,8,9 };

#WAP to scan the all elements into 3*4 matrix and print them.
#include<stdio.h>
void main()
{ int x[3][4],i,j;
printf("\nEnter Matrix X ");
for(i=0;i<3;i++)
{ for( j=0 ; j<4 ; j++ )
scanf("%d" , & x [ i ] [ j ] );
}
printf("\nEntered Matrix Is :\n");
for(i=0;i<3;i++)
{ for( j=0 ; j<4 ; j++ )
printf(" %d" , x [ i ] [ j ] );
printf("\n");
}
}

#WAP to calculate the sum of every row in 3*4 matrix


#include<stdio.h>
void main()
{ int x[3][4] , sumr[ 3 ] ={ 0 } , sumc[4]={0},i,j;
printf("enter Matrix X ");
for( i=0 ; i<3 ; i++ )
{
for( j=0 ; j<4 ; j++ )
{ scanf("%d" , &x[ i ] [ j ] );
Sumr [ i ] = sumr [ i ] +x [ i ] [ j ] ;
}
}

for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf(" %d" , x[ i ][ j ] );
}
printf(" %d \n ", sumr [ i ] );
FACULTY : SANDEEP SAHU( M.Tech(CSE))Page 7
AIM POINT INFOSYSTEM PVT. LTD. 2019

printf("\n");
}
}

Assignment : #WAP to calculate the sum of every column in 3*4 matrix

#WAP to calculate the addition of two matrix.


#include<stdio.h>
void main()
{ int x[3][4],y[3][4],z[3][4],i,j;
printf("\n Enter Matrix X and Y ");
for( i=0 ; i<3 ; i++)
{ for( j=0 ; j<4 ; j++)
{ scanf("%d%d" , & x[ i ] [ j ] , &y[ i ] [ j ]);
z[ i ][ j ] = x [ i ] [ j ] + y [ i ] [ j ];
}
}

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


{ for( j=0 ; j<4 ; j++ )
printf(" %d", x[ i ] [ j ] );
for( j=0 ; j<4 ; j++ )
printf(" %d", y[ i ] [ j ] );
for( j=0 ; j<4 ; j++ )
printf(" %d", z[ i ] [ j ] );
printf("\n");
}
}

#WAP to calculate the transpose of any matrix.


#include<stdio.h>
void main()
{ int x[ 3 ] [ 3 ] ,y [ 3 ] [ 3 ] , i , j ;
printf(" Enter Matrix X ");
for( i=0 ; i<3 ; i++ )
{ for( j=0 ; j<3 ; j++ )
{ scanf("%d" , &x[ i ] [ j ] );
y[ j ][ i ] = x[ i ] [ j ];
FACULTY : SANDEEP SAHU( M.Tech(CSE))Page 8
AIM POINT INFOSYSTEM PVT. LTD. 2019

}
}
for( i=0 ; i<3 ; i++)
{ for( j=0 ; j<4 ; j++ )
printf(" %d", x[ i ] [ j ] );
for( j=0 ; j<4 ; j++ )
printf(" %d", y[ i ] [ j ] );
}
}

#WAP to calculate the multiplication of two matrix.

#Multiplication of diff order matrices

#WAP to calculate the addition of diagonal element, addition of upper triangle


element, addition of lower triangle elements.
#include<stdio.h>
void main()
{ int x[ 3 ] [ 3 ] ,sou=0,sod=0,sol=0 , i , j ;
printf(" Enter Matrix X");
for ( i=0 ; i<3 ; i++ )
{ for( j=0 ; j<3 ; j++ )
FACULTY : SANDEEP SAHU( M.Tech(CSE))Page 9
AIM POINT INFOSYSTEM PVT. LTD. 2019

{ scanf( "%d" , &x[ i ] [ j ] );


If ( j > i )
sou= sou + x[ i ] [ j ];
else if( i > j )
sol = sol + x [ i ] [ j ] ;
else
sod = sod + x [ i ] [ j ] ;
}
}
printf(" \n addition of digonal element= %d " , sod );
printf(" \n addition of upper element = %d" , sou);
printf(" \n addition of lower element = %d" , sol);
}

FACULTY : SANDEEP SAHU( M.Tech(CSE))Page 10

You might also like