One
Dimensional Array:
1. Sort an integer
array of 10 elements in descending order (large to small) using bubble sort,
selection sort and
insertion sort.
a. Variables: integer
array A[10].
b. Initialize the
array with values: 56,34,5,-68,-1,0,6,4,2,19.
c. You must print the
array before sorting and also after sorting to help understand
that the sorting
really happened.
solution:
Bubble
sorting:
#include< stdio.h >
int main( void ){
int temp;
int A[ 10 ] = { 56,34,5,-68,-1,0,6,4,2,19 };
printf("Before Bubble Sort:\n\n");
int i,m,n,j;
for( i = 0 ; i < 10 ; i++ ) printf( "%d " , A[ i ] );
printf("\n\n");
for( m = 0 ; m < 10 - 1 ; m++ ){
for( n = 0 ; n < 10 - 1 - m ; n++ ){
if( A[ n ] < A[ n + 1 ] ){
temp = A[ n ];
A[ n ] = A[ n + 1 ];
A[ n + 1 ] = temp;
}
}
}
printf("After Bubble Sort:\n\n");
for( j = 0 ; j < 10 ; j++ ) printf( "%d " , A[ j ] );
return 0;
}
int main( void ){
int temp;
int A[ 10 ] = { 56,34,5,-68,-1,0,6,4,2,19 };
printf("Before Bubble Sort:\n\n");
int i,m,n,j;
for( i = 0 ; i < 10 ; i++ ) printf( "%d " , A[ i ] );
printf("\n\n");
for( m = 0 ; m < 10 - 1 ; m++ ){
for( n = 0 ; n < 10 - 1 - m ; n++ ){
if( A[ n ] < A[ n + 1 ] ){
temp = A[ n ];
A[ n ] = A[ n + 1 ];
A[ n + 1 ] = temp;
}
}
}
printf("After Bubble Sort:\n\n");
for( j = 0 ; j < 10 ; j++ ) printf( "%d " , A[ j ] );
return 0;
}
insertion
sorting:
#include< stdio.h >
int main( void ){
int temp;
int A[ 10 ] = { 56,34,5,-68,-1,0,6,4,2,19 };
printf("Before Insertion Sort:\n\n");
int i,m,n,j;
for( i = 0 ; i < 10 ; i++ ) printf( "%d " , A[ i ] );
printf("\n\n");
for( m = 1 ; m <= 10 - 1 ; m++ ){
temp = A[ m ];
for( n = m - 1 ; n >= 0 && A[ n ] < temp ; n-- ){
A[ n + 1 ] = A[ n ];
A[ n ] = temp;
}
}
printf("After Insertion Sort:\n\n");
for( j = 0 ; j < 10 ; j++ ) printf( "%d " , A[ j ] );
return 0;
}
int main( void ){
int temp;
int A[ 10 ] = { 56,34,5,-68,-1,0,6,4,2,19 };
printf("Before Insertion Sort:\n\n");
int i,m,n,j;
for( i = 0 ; i < 10 ; i++ ) printf( "%d " , A[ i ] );
printf("\n\n");
for( m = 1 ; m <= 10 - 1 ; m++ ){
temp = A[ m ];
for( n = m - 1 ; n >= 0 && A[ n ] < temp ; n-- ){
A[ n + 1 ] = A[ n ];
A[ n ] = temp;
}
}
printf("After Insertion Sort:\n\n");
for( j = 0 ; j < 10 ; j++ ) printf( "%d " , A[ j ] );
return 0;
}
selection
sorting:
#include< stdio.h >
int main( void ){
int temp;
int A[ 10 ] = { 56,34,5,-68,-1,0,6,4,2,19 };
printf("Before Selection Sort:\n\n");
int i,m,n,j;
for( i = 0 ; i < 10 ; i++ ) printf( "%d " , A[ i ] );
printf("\n\n");
for( m = 0 ; m < 10 - 1 ; m++ ){
for( n = m + 1 ; n < 10 ; n++ ){
if( A[ m ] < A[ n ] ){
temp = A[ m ];
A[ m ] = A[ n ];
A[ n ] = temp;
}
}
}
printf("After Selection Sort:\n\n");
for( j = 0 ; j < 10 ; j++ ) printf( "%d " , A[ j ] );
return 0;
}
int main( void ){
int temp;
int A[ 10 ] = { 56,34,5,-68,-1,0,6,4,2,19 };
printf("Before Selection Sort:\n\n");
int i,m,n,j;
for( i = 0 ; i < 10 ; i++ ) printf( "%d " , A[ i ] );
printf("\n\n");
for( m = 0 ; m < 10 - 1 ; m++ ){
for( n = m + 1 ; n < 10 ; n++ ){
if( A[ m ] < A[ n ] ){
temp = A[ m ];
A[ m ] = A[ n ];
A[ n ] = temp;
}
}
}
printf("After Selection Sort:\n\n");
for( j = 0 ; j < 10 ; j++ ) printf( "%d " , A[ j ] );
return 0;
}
2.
Find the median number of an integer array A and B.
a. Algorithm: If the size of array is even (N) then median = average of Array[N/2] +
A[N/2-1]. If the size of array is odd then median = Array[N/2].
b. Variables: integer array A[10], B[11].
c. Initialize the array with values: A = 1,2,3,4,5,6,7,8,9,10 and B =
11,12,13,14,15,16,17,18,19,20,21.
d. You must print the median for both arrays.
a. Algorithm: If the size of array is even (N) then median = average of Array[N/2] +
A[N/2-1]. If the size of array is odd then median = Array[N/2].
b. Variables: integer array A[10], B[11].
c. Initialize the array with values: A = 1,2,3,4,5,6,7,8,9,10 and B =
11,12,13,14,15,16,17,18,19,20,21.
d. You must print the median for both arrays.
solution:
#include< stdio.h >
int main( void ){
int temp;
int A[ 10] = { 1,2,3,4,5,6,7,8,9,10 };
int B[11]={11,12,13,14,15,16,17,18,19,20,21};
printf("Before Selection Sort of A[11]:\n\n");
int i,m,n,j;
for( i = 0 ; i < 10 ; i++ ) printf( "%d " , A[ i ] );
printf("\n\n");
printf("Before Selection Sort of B[11]:\n\n");
for( i = 0 ; i < 10 ; i++ ) printf( "%d " , B[ i ] );
printf("\n\n");
for( m = 0 ; m < 10 - 1 ; m++ ){
for( n = m + 1 ; n < 10 ; n++ ){
if( A[ m ] > A[ n ] ){
temp = A[ m ];
A[ m ] = A[ n ];
A[ n ] = temp;
}
}
}
for( m = 0 ; m < 10 - 1 ; m++ ){
for( n = m + 1 ; n < 10 ; n++ ){
if( B[ m ] > B[ n ] ){
temp = B[ m ];
B[ m ] = B[ n ];
B[ n ] = temp;
}
}
}
printf("After Selection Sort of A[10]:\n\n");
for( j = 0 ; j < 10 ; j++ ) printf( "%d " , A[ j ] );
printf("\n\n");
printf("After Selection Sort of B[11]:\n\n");
for( j = 0 ; j < 10 ; j++ ) printf( "%d " , B[ j ] );
printf("\n\n");
int N=10,L=11;
float median;
median=(A[N/2]+A[(N/2)-1]);
median=median/2;
printf("median of A[10]=%f",median);
printf("\n\n");
median=B[L/2];
printf("median of B[11]=%f",median);
return 0;
}
int main( void ){
int temp;
int A[ 10] = { 1,2,3,4,5,6,7,8,9,10 };
int B[11]={11,12,13,14,15,16,17,18,19,20,21};
printf("Before Selection Sort of A[11]:\n\n");
int i,m,n,j;
for( i = 0 ; i < 10 ; i++ ) printf( "%d " , A[ i ] );
printf("\n\n");
printf("Before Selection Sort of B[11]:\n\n");
for( i = 0 ; i < 10 ; i++ ) printf( "%d " , B[ i ] );
printf("\n\n");
for( m = 0 ; m < 10 - 1 ; m++ ){
for( n = m + 1 ; n < 10 ; n++ ){
if( A[ m ] > A[ n ] ){
temp = A[ m ];
A[ m ] = A[ n ];
A[ n ] = temp;
}
}
}
for( m = 0 ; m < 10 - 1 ; m++ ){
for( n = m + 1 ; n < 10 ; n++ ){
if( B[ m ] > B[ n ] ){
temp = B[ m ];
B[ m ] = B[ n ];
B[ n ] = temp;
}
}
}
printf("After Selection Sort of A[10]:\n\n");
for( j = 0 ; j < 10 ; j++ ) printf( "%d " , A[ j ] );
printf("\n\n");
printf("After Selection Sort of B[11]:\n\n");
for( j = 0 ; j < 10 ; j++ ) printf( "%d " , B[ j ] );
printf("\n\n");
int N=10,L=11;
float median;
median=(A[N/2]+A[(N/2)-1]);
median=median/2;
printf("median of A[10]=%f",median);
printf("\n\n");
median=B[L/2];
printf("median of B[11]=%f",median);
return 0;
}
3.
Write a program that will transpose (converting the row values to column values
and viceversa)
4 by 4 matrix.
a. Variable: integer array A[4][4].
b. Desired Output: transpose of A[4][4]
4 by 4 matrix.
a. Variable: integer array A[4][4].
b. Desired Output: transpose of A[4][4]
solution:
#include<
stdio.h >
#define ROW 3
#define COLUMN 3
int main(){
int A[ROW][COLUMN]={1,2,3,4,5,6,7,8,9};
int i,k;
int B[ROW][COLUMN];
for ( i=0 ; i < ROW ; i++){
for ( k=0 ; k < COLUMN ; k++){
printf("%d\t",A[i][k]);
}
printf("\n");
}
printf("\n\n");
for ( k=0 ; k < ROW ; k++){
for ( i=0 ; i < COLUMN ; i++){
B[k][i]=A[i][k];
printf("%d\t",B[k][i]);
}
printf("\n");
}
return 0;
}
#define ROW 3
#define COLUMN 3
int main(){
int A[ROW][COLUMN]={1,2,3,4,5,6,7,8,9};
int i,k;
int B[ROW][COLUMN];
for ( i=0 ; i < ROW ; i++){
for ( k=0 ; k < COLUMN ; k++){
printf("%d\t",A[i][k]);
}
printf("\n");
}
printf("\n\n");
for ( k=0 ; k < ROW ; k++){
for ( i=0 ; i < COLUMN ; i++){
B[k][i]=A[i][k];
printf("%d\t",B[k][i]);
}
printf("\n");
}
return 0;
}
2.Write
a program that will add two 4 by 4 matrix A and B respectively and store in
another 4
by 4 matrix C.
a. Variables: integer array A[4][4], B[4][4] and C[4][4].
b. Desired Output: C[4][4] = A[4][4] + B[4][4]
by 4 matrix C.
a. Variables: integer array A[4][4], B[4][4] and C[4][4].
b. Desired Output: C[4][4] = A[4][4] + B[4][4]
solution:
#include<
stdio.h >
#define ROW 4
#define COLUMN 4
int main(){
int A[ROW][COLUMN]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int B[ROW][COLUMN]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int C[ROW][COLUMN];
int i , sum , k;
for ( i=0 ; i < ROW ; i++){
for ( k=0 ; k < COLUMN ; k++){
C[i][k]=A[i][k]+B[i][k];
}
}
for ( i=0 ; i < ROW ; i++){
for ( k=0 ; k < COLUMN ; k++){
printf("C[%d][%d]:%d\t",i,k,C[i][k]);
}
printf("\n");
}
return 0;
}
#define ROW 4
#define COLUMN 4
int main(){
int A[ROW][COLUMN]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int B[ROW][COLUMN]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int C[ROW][COLUMN];
int i , sum , k;
for ( i=0 ; i < ROW ; i++){
for ( k=0 ; k < COLUMN ; k++){
C[i][k]=A[i][k]+B[i][k];
}
}
for ( i=0 ; i < ROW ; i++){
for ( k=0 ; k < COLUMN ; k++){
printf("C[%d][%d]:%d\t",i,k,C[i][k]);
}
printf("\n");
}
return 0;
}
1.
Implement the following series using function.
a. Input: an integer number.
b. Desired Output: The series will be printed as
a. Input: an integer number.
b. Desired Output: The series will be printed as
input:4
output:1+1+3+1+3+5+1+3+5+7
c.
Note: You need to print the series not the summation result.
solution:
#include<
stdio.h >
int series(int n){
int i , k , m , s;
for ( k=1 ; k <= n ; k++){
for (i=1 ; i <= k ; i++){
m=((2*i)-1);
printf(" %d " , m);
printf(" + ");
}
}
return 0;
}
int main(){
int x;
scanf("%d",&x);
series(x);
return 0;
}
int series(int n){
int i , k , m , s;
for ( k=1 ; k <= n ; k++){
for (i=1 ; i <= k ; i++){
m=((2*i)-1);
printf(" %d " , m);
printf(" + ");
}
}
return 0;
}
int main(){
int x;
scanf("%d",&x);
series(x);
return 0;
}
2.
Implement the following using function.
a. Input: an integer number.
b. Desired Output: The series will be printed as
a. Input: an integer number.
b. Desired Output: The series will be printed as
input:5
output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2
1 2 3
1 2 3 4
1 2 3 4 5
c.
Note: You must show the output as given in the sample.
solution:
#include<
stdio.h >
int myfunc(int x){
int i ,j;
for ( i=1 ; i <= x ; i++){
int z=1;
for (j=1 ; j <= i ; j++ ){
printf(" %d " , z);
z++;
}
printf(" \n ");
}
return 0;
}
int main (){
int n;
printf(" no of input:\n\n ");
scanf(" %d ", &n);
myfunc(n);
return 0;
}
int myfunc(int x){
int i ,j;
for ( i=1 ; i <= x ; i++){
int z=1;
for (j=1 ; j <= i ; j++ ){
printf(" %d " , z);
z++;
}
printf(" \n ");
}
return 0;
}
int main (){
int n;
printf(" no of input:\n\n ");
scanf(" %d ", &n);
myfunc(n);
return 0;
}
No comments:
Post a Comment