Saturday 19 March, 2011

Write a program to create linear linked list interactively and print out the list and total number of items in the list.


#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
}*head,*last,*temp,*n;
void menu(); // to select the choise
void create(); // insert at last
void display(); // to print the link list
int i;
void main()
{
i=0;
clrscr();
menu();
getch();
}
void menu()
{
int ch;
printf("1. Create\n");
printf("2. Display\n");
printf("3. Exit\n");
printf("Enter choise : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : create();
case 2 : display();
case 3 : printf("You have choose to exit\n");
getch(); exit(0);
default : printf("Invalid choise\n"); menu();
}
}
void create()
{
n= new node;
printf("Enter the information : ");
scanf("%d",&n->info);
if(i==0)
{
head=n;
i++;
}
else
{
last->next=n;
i++;
}
last=n;
last->next=NULL;
menu();
}
void display()
{
if(head==NULL)
{
printf("No information in list\n");
menu();
}
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->info);
temp=temp->next;
}
printf("\n");
printf("Total number of elements in the link list are : %d\n",i);
menu();
}
Output:
1. Create
2. Display
3. Exit
Enter choise : 1
Enter the information : 12
1. Create
2. Display
3. Exit
Enter choise : 1
Enter the information : 23
1. Create
2. Display
3. Exit
Enter choise : 1
Enter the information : 45
1. Create
2. Display
3. Exit
Enter choise : 2
12->23->45->
Total number of elements in the link list are : 3
1. Create
2. Display
3. Exit
Enter choise : 3
You have choose to exit

Write a program to read data from keyboard, write it to a file named STUDENT again read the same data from STUDENT file and write it into DATA file. Same data should be displayed on the screen


#include<stdio.h>
#include<conio.h>
#include<process.h>
struct stud
{
int rno;
char *nm;
};
void main()
{
struct stud *s;
int n,i;
FILE *fp,*fp1;
clrscr();
printf("Enter how many record you want to input : ");
scanf("%d",&n);
s=(struct stud *)malloc(n*sizeof(struct stud));
fp=fopen("STUD.txt","w");
for(i=0;i<n;i++)
{
printf("\n\tInformation for student : %d\n",i+1);
printf("Enter Roll No : ");
scanf("%d",&s[i].rno);
printf("Enter Name : ");
fflush(stdin);
gets(s[i].nm);
fprintf(fp,"%5d %-20s\n",s[i].rno,s[i].nm);
}
fclose(fp);
fp=fopen("STUD.txt","r");
fp1=fopen("DATA.txt","w");
printf("\nContent of the STUD.txt file is\n");
printf("Roll No Name\n");
printf("---------------------------\n");
while(!feof(fp))
{
fscanf(fp,"%5d %20s\n",&s[i].rno,s[i].nm);
fprintf(fp1,"%5d %-20s\n",s[i].rno,s[i].nm);
printf("%7d %-20s\n",s[i].rno,s[i].nm);
}
fcloseall();
getch();
}
Output:
Enter how many record you want to input : 3
Information for student : 1
Enter Roll No : 1
Enter Name : happy
Information for student : 2
Enter Roll No : 2
Enter Name : Umesh
Information for student : 3
Enter Roll No : 3
Enter Name : Mehul
Content of the STUD.txt file is
Roll No Name
---------------------------
1 happy
2 Umesh
3 Mehul

Write a function using pointers to add two matrices and to return the resultant matrix to the calling function.


#include<stdio.h>
#include<conio.h>
int a[5][5],b[5][5],row,col;
void add(int(*)[5]);
int main()
{
int c[5][5],i,j;
clrscr();
printf("Enter row : ");
scanf("%d",&row);
printf("Enter column : ");
scanf("%d",&col);
printf("Enter matrix A :\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter matrix B :\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&b[i][j]);
}
}
add(c);
printf("Addition :\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
return 0;
}
void add(int c[5][5])
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
/*
Enter row : 3
Enter column : 3
Enter matrix A :
333
333
333
Enter matrix B :
555
555
555
Addition :
8
8
8
8
8
8
8
8
8