JAYASHREE ORACLE CERTIFIED JAIN UNIVERSITY

Friday, October 8, 2010

C PART B LAB PROGRAM

PART B
//Program 11:SEARCHING AN ELEMENT IN AN ARRAY USING POINTERS.

#include
#include
void main()
{
int a[100],i, s,n, flag=0;
int *p;

clrscr();
printf("\nEnter Number of elements in the array ");
scanf("%d",&n);

printf("\n Enter all the elements of the array\n\n");
for (i=0 ; i scanf("%d", &a[i]);

printf("\n\nEnter Search Element ");
scanf("%d", &s);

p=r;
for ( i=0; i < n ; i++)
{
if (*p == s)
{
flag=1;
break;
}
p++;
}

if (flag == 1)
printf("\nSerach element found") ;
else
printf("\nSerach Element Not found");

getch();
}
-----------------------------------------------------------------------
//program 12://CHECKING WHETHER THE GIVEN MATRIX IS AN IDENTITY MATRIX OR NOT
#include
#include
void main()
{
int a[10][10];
int i,j,r,c,flag=1;
clrscr();
printf("Enter the order of the Matrix ");
scanf("%d%d",&r,&c);
printf("Enter the elements of matrix ");
for(i=0;i {
for(j=0;j {
scanf("%d",&a[i][j]);
}
}

printf("Matrix A is \n");
for(i=0;i {
for(j=0;j {
printf("%3d",a[i][j]);
}
printf("\n");
}

/*Check for unit (or identity) matrix*/
for(i=0;i {
for(j=0;j {
if((i ==j ) & a[i][j] != 1 || ( i!=j) && (a[j][i] != 0 ))
{
flag=0;
break;
}
}
}
if(flag == 1)
printf("It is an identity matrix\n");
else
printf("It is not an identity matrix\n");
getch();
}

-------------------------------------------------------------------------------------

/*DECLARE 13 POINTER VARIABLES TO STORE A CHARACTER, A CHARACTER STRING AND AN INTEGER.DISPLAY THE ADDRESS AND

CONTENT OF EACH VARIABLE */
#include
#include
void main()
{
int a;
char b;
char c[10];
int *p;char *q,*r;

clrscr();
printf("\nEnter the integer value: ");
scanf("%d",&a);
p=&a;
fflush(stdin);

printf("\nEnter the character value: ");
b=getchar();
q=&b;
fflush(stdin);

printf("\nEnter the string: ");
gets(c);
r=c;

printf("\n\nThe address of %d = %u",a,p);
printf("\nThe content of %u = %d",p,*p);
printf("\n\nThe address of %c = %u",b,q);
printf("\nThe content of %u = %c",q,*q);
printf("\n\nThe address of %s= %u",c,r);

while(*r!='\0')
{
printf("\nThe content of %u = %c",r,*r);
++r;
}
getch();
}
---------------------------------------------------------------------------------------------------------
/*Program 14:Program to define a structure with three members and display the same*/

#include
#include

/* Structure Definition*/
struct book
{
char title[30];
char author[25];
float price;
int pages;
};

void main()
{
struct book book1;
clrscr();

/* Accepting Book Details*/
printf("Enter the title of the book: ");
gets(book1.title);
printf("Enter the Author of the book: ");
gets(book1.author);
printf("Enter the price of the book: ");
scanf("%f",&book1.price);
printf("Enter number of pages in the book: ");
scanf("%d",&book1.pages);

clrscr();
/* Displaying the Book Details*/
printf("Book Title: ");
printf("%s\n",book1.title);
printf("Author: ");
printf("%s\n",book1.author);
printf("Price: Rs.");
printf("%.2f\n",book1.price);
printf("Pages: ");
printf("%d",book1.pages);
getch();
}
--------------------------------------------------------------------------------------------
//Program 15:Program to declare a union with 3 members of type int,char&string
#include
#include
void main()
{

union student
{
int regno;
char section;
char name[20];
};

union student ns;

clrscr();
printf("\nEnter the student reg.no ");
scanf("%d",&ns.regno);
printf("\nEnter the student section ");
scanf("%s",&ns.section);
printf("\nEnter the name of student ");
scanf("%s",&ns.name);

printf("\n\n\nDetails of a student");
printf("\n------------------------\n");
printf("Size of union type variable is %d",sizeof(ns));
printf("\n\nThe value stored at the end is %s ",ns.name);
getch();

}
-------------------------------------------------------------------------------------
/* Program 16 :Sorting using Bubble Sort Method */
#include
#include
void main()
{
int array[10];
int i,j,n,temp;

clrscr();
printf("\n Sorting Using Bubble Sort Method \n\n");
printf("Enter Number of Elements: ");
scanf("%d",&n);

printf("\nEnter the elements one by one: ");
for(i=0;i {
scanf("%d",&array[i]);
}

/*Bubble sorting begins(ASCENDING ORDER)*/
for(i=0;i {
for(j=i+1;j {
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}

printf("\n\nsorted array in ASCENDING ORDER....\n");
for(i=0;i {
printf("%3d",array[i]);
}


printf("\n\nsorted array in DESCENDING ORDER....\n");
for(i=n-1;i>=0;i--)
{
printf("%3d",array[i]);
}
getch();
}
-----------------------------------------------------------------------
//Program 17: Addition and subtraction of two matrices.


#include
#include
void main()
{
int a[10][10],b[10][10],c[10][10],d[10][10],i,j,m,n,p,q;
clrscr();

printf("Enter the order of the matrix A\n");
scanf("%d%d",&m,&n);
printf("Enter the order of the matrix B\n");
scanf("%d%d",&p,&q);

if ((m==p)&&(n==q))
{
printf("Enter the elements of matrix A\n");
for(i=0;i for(j=0;j scanf("%d",&a[i][j]);

printf("Enter the elements of matrix B\n");
for(i=0;i for(j=0;j scanf("%3d",&b[i][j]);

printf("Matrix A\n");
for(i=0;i {
for(j=0;j {
printf("%3d",a[i][j]);
}
printf("\n");
}

printf("Matrix B\n");
for(i=0;i {
for(j=0;j {
printf("%3d",b[i][j]);
}
printf("\n");
}

for(i=0;i for(j=0;j c[i][j]=a[i][j]+b[i][j];

for(i=0;i for(j=0;j d[i][j]=a[i][j]-b[i][j];

printf("sum of matrices\n");
for(i=0;i {
for(j=0;j {
printf("%3d",c[i][j]);
}
printf("\n");
}

printf("Diference of matrices\n");
for(i=0;i {
for(j=0;j {
printf("%3d",d[i][j]);
}
printf("\n");
}
getch();
}
}
--------------------------------------------------------------------------------
//Program 18: Multiplication of two matrices.
#include
#include
void main()
{
int a[10][10],b[10][10],prod[10][10],i,j,k,n;
clrscr();

printf("\n Enter the order of matrix: ");
scanf("%d",&n);

printf("\n Enter the elements of matrix a: ");
for(i=0;i for(j=0;j scanf("%d",&a[i][j]);

printf(" Enter elements of Matrix b: ");
for(i=0;i for(j=0;j scanf("%d",&b[i][j]);

/*Compute product of matrix a & b */
for(i=0;i {
for(j=0;j {
prod[i][j]=0;
for(k=0;k prod[i][j]=prod[i][j] + a[i][k] * b[k][j];
}
}

printf("\n The product of a and b matrix \n");
for(i=0;i {
for(j=0;j {
printf("%4d",prod[i][j]);
}
printf("\n");
}

getch();
}
--------------------------------------------------------------------------
//Program 19: Check whether the given string is a palindrome or not.


-----------------------------------------------------------
//Program 20 Applying binary search to a set of N numbers by using a function.
//Binary search
#include
#include
void main()
{
int a[100];
int n,i,item,beg,end,mid;
clrscr();

printf("Enter no of elements");
scanf("%d",&n);

printf("\nEnter the elements");
for(i=0;i scanf("%d",&a[i]);

printf("Enter number to be searched");
scanf("%d",&item);
beg=0;
end=n-1;
mid=(beg + end) / 2;
if((beg<=end)&&(item!=a[mid]))
beg=mid+1;
else
end=mid-1;

mid=(beg+end)/2;
if ( a[mid]==item )
printf("\n %d is found in position %d",item,mid+1);
else
printf("\n Item does not exist");

}








No comments:

Post a Comment