C Programming and Data Structures: Unit I: C Programming Fundamentals

Simple C Programs

C Programming and Data Structures: Unit I: C Programming Fundamentals : Simple Programs

Simple Programs

• Program 1.11.1: Sorting Program

AU: Dec.-18, Marks 13

AU: Dec.-18, Marks 6

/*****************************

Program for sorting the elements

*****************************/

/* Header Files*/

#include<stdio.h>

#include<conio.h>

#include <process.h>

/*Declaration*/

void sort(int a[20], int n)

{

int i,j,m,temp;

for(i=0;i<n-1;i++)

{

for(j=0;j<n;j++)

{

if(a[j]>a[j+1])

{ temp=a[j];

a[j]=a[j+1];

alj+1]=temp;

}

}

}

//Printing the sorted array//

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

printf("\n\t%d",a[i]);

}

void main( )

{

int a[20],i,n;

int ch;

clrscr( );

printf("\nEnter the number of elements in the sorting array: ");

scanf("%d", &n);

//Accepting n number of elements in the array

printf("\nEnter the elements for the array\n");

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

scanf("%d",&a[i]);

bubblesort (a,n);

getch( );

}

/**************** End Of Program Output ************/

Enter the number of elements in the sorting array: 5

Enter the elements for the array

30

20

50

40

10

10

20

30

40

50

Program 1.11.2 Searching Program   AU: Dec.-18, Marks 7

/***********************************************

Program to perform the linear search operation on some number of elements.

************************************************/

#include<stdio.h>

#include<conio.h>

#define MAX 10

int a[MAX],n,i;

/*

The create Function

Input :none

Output:none

Called By: main

Calls:none

*/

void create( )

{

printf("\n How Many Elements");

scanf("%d",&n);

printf("\n Enter The Elements");

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

scanf("%d",&a[i]);

}

/*

The display Function

Input:none

Output:none

Called By:main

Calls:none

*/

void display( )

{

printf("\n The Elements Are");

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

printf("\n%d",a[i]);

}

/*

The search function

Input:key element- which is to be searched

Output:the status

Called By:main

Calls:none

*/

search(int k)

{

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

{

if(a[i]==k)

return 1;

}

return 0;

}

void main( )

{

int status,key;

clrscr( );

create( );

display( );

printf("\n Enter the Element Which You wish to Search ");

scanf("%d", &key);

status=search(key);

if (status = =1)

printf("\n The Element is Present");

else

printf("\n The Element Is Not Found");

getch();

}

/**************** End Of Program' **************/

Output

How Many Elements 5

Enter The Elements 10

1

100

1000

10000

The Elements Are

10

1

100

1000

10000

Enter the Element Which You wish to Search 99

The Element Is Not Found

How Many Elements 5

Enter The Elements 10

1

100

1000

10000

The Elements Are

10

1

100

1000

10000

Enter the Element Which You wish to Search 100

The Element is Present

Program 1.11.3 Matrix Operations

The matrix can be represented by a two dimensional array in C program. Following is a C program that performs various matrix operations.

/*****************************************

Program to perform matrix operations

*****************************************/

#include<stdio.h>

#include<conio.h>

#define MAX 5

 

void main( )

{

int a[MAX][MAX],b[MAX] [MAX],c[MAX] [MAX];

int m1,n1,m2,n2;

int choice;

int i,j;

char ans='y';

 

void Print_Matrix(int c[MAX] [MAX],int,int);

void Addition(int a[MAX] [MAX], int m1,int n1,int b[MAX] [MAX], int c[MAX][MAX]);

void Subtraction(int a[MAX] [MAX], int m1,int n1,int b[MAX] [MAX], int c[MAX] [MAX]);

void Transpose(int a[MAX] [MAX], int m1,int n1,int c[MAX][MAX]);

void Multiplication(int a[MAX][MAX],int,int,int b[MAX] [MAX],int,int,int c[MAX][MAX]);

 

printf("\n\t Program for performing matrix operations ");

printf("\n Enter data for matrix a \n");

printf("\n Enter the total number of rows ");

scanf("%d",&m1);

printf("\n Enter the total number of columns ");

scanf("%d", &n1);

printf("\n Enter the elements\n");

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

for(j=0;j<n1;j++)

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

printf("\n Enter data for matrix b");

printf("\n Enter the total number of rows ");

scanf("%d", &m2);

printf("\n Enter the total number of columns ");

scanf("%d", &n2);

printf("\n Enter the elements\n");

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

for(j=0;j<n2;j++)

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

do

{

printf("\n\t Main Menu");

printf("\n 1. Addition 2. Subtraction 3. Multiplication 4. Transpose");

printf("\n Enter your choice");

scanf("%d", &choice);

switch(choice)

{

case 1:printf("\n The addition of two matrices is \n");

Addition(a,m1,1,b,c);

Print_Matrix(c,m1,n1);

break;

 

case 2:printf("\n The subtraction of two matrices is \n");

Subtraction(a,m1,n1,b,c);

Print_Matrix(c,m1,n1); -

break;

case 3:printf("\n The multiplication of two matrices is \n");

if(n1= =m2)

{

Multiplication(a,m1,n1,b,m2,n2,c);

Print_Matrix(c,m1,n2);

}.

else

printf("\n Multiplication is not possible");

break;

case 4:printf("\n The transpose of matrix a is \n");

Transpose(a,m1,n1,c);

Print_Matrix(c,m1,n1);

break;

}

printf("\n Do you want to continue?");

ans = getch( );

} while(ans= ='y');

getch( );

}

void Print_Matrix(int c[MAX] [MAX], int m,int n)

{

int i,j;

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

{

for(j=0;j<n;j++)

{

printf("%d",c[i][j]);

}

printf("\n");

}

}

void Addition(int a[MAX] [MAX], int m1,int n1,int b[MAX][MAX], int c[MAX][MAX])

{

int i,j;

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

{

for(j=0;j<n1;j++)

{

c[i][j]=a[i][j]+b[i][j];

}

}

}

void Subtraction(int a[MAX] [MAX], int m1,int n1,int b[MAX] [MAX], int c[MAX][MAX])

{

int i,j;

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

{

for(j=0;j<n1;j++)

{

c[i][j]=a[i][j]-b[i][j];

}

}

}

void Multiplication(int a[MAX][MAX], int m1,int n1,int b[MAX][MAX], int m2,int n2,int c[MAX][MAX])

{

int i,j,k;

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

{

for(j=0;j<n2;j++)

{

c[i][j]=0;

for(k=0;k<m2;k++)

c[i][j]=c[i][j]+a[i][k]*b[k][j];

}

}

}

void Transpose(int a[MAX] [MAX], int m1,int n1,int c[MAX][MAX])

{

int i,j;

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

{

for(j=0;j<n1;j++)

{

c[i][j]=a[j][i];

}

}

}

Output

Program for performing matrix operations

Enter data for matrix a

 

Enter the total number of rows 3

 

Enter the total number of columns 3

 

Enter the elements

123

456

789

 

Enter data for matrix b

Enter the total number of rows 3

 

Enter the total number of columns 3

 

Enter the elements

1 1 1

222

333

 

Main Menu

1. Addition 2. Subtraction 3. Multiplication

4. Transpose

Enter your choice 1

 

The addition of two matrices is

234

678

10 11 12

 

Do you want to continue?

 

Ex. 1.11.1: Write a C program to get a 4 digit number and display the reverse of the same number.

AU: Dec.-18, Marks 6

Sol.:

#include <stdio.h>

int main( )

{

int n, reverse = 0;

printf("\nEnter a four digit number: ");

scanf("%d",&n);

while (n!= 0)

{

reverse = reverse * 10

int digit n%10;

reverse = reverse + digit;

n = n/10;

}

printf("\nReverse number is = %d", reverse);

return 0;

}

 

Ex.1.11.2 Write a C program to find the following, where A, B and C are the matrices whose orders are M*N, M*N and N*N respectively. A = A+B*C

AU: Dec.-18, Marks 13

Sol. :

#include<stdio.h>

int main( )

{

int A[3][3],B[3][3],C[3][3],Temp[3][3],i,j,k,m,n;

printf("Enter the order of the matrix::\n");

scanf("%d %d",&m,&n);

printf("Enter the A matrix::\n");

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

{

for(j=0;j<n;j++)

{

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

}

}

printf("Enter the B matrix::\n");

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

{

for(j=0;j<n;j++)

{

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

}

}

printf("Enter the C matrix::\n");

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

{

for(j=0;j<n;j++)

{

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

}

}

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

{

for(j=0;j<n;j++)

{

Temp[i][j]=0;

}

}

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

{

for(j=0;j<n;j++)

{

for(k=0;k<n;k++)

{

Temp[i][j]=Temp[i][j]+B[i] [k]*C[k][j]; //performing B*C

}

}

}

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

{

for(j=0;j<n;j++)

{

A[i][j]=A[i][j]+Temp[i][j]; //performing A+B*C

}

}

printf("The resultant matrix is::\n");

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

{

for(j=0;j<n;j++)

{

printf("%d ",A[i][j]);

}

printf("\n");

}

}

 

C Programming and Data Structures: Unit I: C Programming Fundamentals : Tag: : - Simple C Programs