-->
Showing posts with label grade 12 lab report solution. Show all posts
Showing posts with label grade 12 lab report solution. Show all posts

Lab works-09(data files)

 Questions:

q1) Write a program to enter name,roll_no, and marks of 10 students and store them in the file.

q2)Write a program to store empid,name and salary of 10 employees in a datafile and print the records from the file.

q3)Write a program using C language that reads successive records from new data file and display each record on the screen in an appropriate format.

q4)Write a program to store std_id,name and marks of 'n' students in a data file.Display the records in appropriate format reading from the file.

q5)Write a program to enter name,roll_no and marks of 10 students and store them in the file.Read and display the record from the file.

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

solution:-

q1) Write a program to enter name,roll_no, and marks of 10 students and store them in the file.

ans:-

code:

//to store roll,name and percentage of some students
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char name[30];                                                    //identifier declaration
int roll,count=1;                                                              //       "             "
float per;
k=fopen("student.txt","w");//file opening
do
{

printf("enter record=%d",count);
printf("\nenter roll\n");   //data input
scanf("%d",&roll);

printf("enter  name\n");           // getting inputs

scanf("%s",name);
printf("enter percentage\n");
scanf("%f",&per);

fprintf(k,"%d %s %f\n",roll,name,per); //writing data to data file
printf("-------------------------------\n");//prints message
count++;                           
}while(count<=10);        //writing repeats as you enter 'y'                             
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                       
getch();
return 0;
}

q2)Write a program to store empid,name and salary of 10 employees in a datafile and print the records from the file.

Ans:-

code:

//to store empid,name and salary of 10 employees
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char ename[30];                                                    //identifier declaration
int empid,count=1;                                                              //       "             "
float esalary;
k=fopen("emp.txt","w");//file opening
do
{

printf("enter record for=%d employee",count);
printf("\nenter empid\n");   //data input
scanf("%d",&empid);

printf("enter  ename\n");           // getting inputs
scanf("%s",ename);
printf("enter salary\n");
scanf("%f",&esalary);

fprintf(k,"%d %s %f\n",empid,ename,esalary); //writing data to data file
printf("-------------------------------\n");//prints message
count++;                           
}while(count<=10);        //writing repeats as you enter 'y'                             
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file    

k=fopen("emp.txt","r");//file opening

while((fscanf(k,"%d %s %f\n",empid,ename,esalary))!=EOF)   
//reading process goes on until the file ends(End of File)
{
printf("empid=%d name=%s salary=%f\n",empid,ename,esalary);   //printing on screen
}

printf("\n reading process completed successfully\n");                   
getch();
return 0;
}

q3)Write a program using C language that reads successive records from new data file and display each record on the screen in an appropriate format.
ans:
code:-
//to read empid,name and salary of  employees from data file
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char ename[30];                                                    //identifier declaration
int empid,count=1;                                                              //       "             "
float esalary;
k=fopen("emp.txt","r");//file opening
printf("read records are:\n");
while((fscanf(k,"%d %s %f\n",empid,ename,esalary))!=EOF)
//reading process goes on until the file ends(End of File)
{
printf("empid=%d name=%s salary=%f\n",empid,ename,esalary);   //printing on screen
}
printf("\n reading process completed successfully\n");
fclose(k);                                                           //closing data file
getch();
return 0;
}

q4)Write a program to store std_id,name and marks of 'n' students in a data file.Display the records in appropriate format reading from the file.

ans:-

code:-

//to store std_id,name and marks(5 subjects) of some students
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char sname[30],choice;                                                    //identifier declaration
int sid;                                                              //       "             "
int eng,acc,eco,bstd,csc;
k=fopen("sdetail.txt","w");//file opening
printf("we are going to write /store data\n");
do
{
printf("\nenter sid\n");   //data input
scanf("%d",&sid);

printf("enter  name\n");           // getting inputs
scanf("%s",sname);
printf("enter marks of english\n");
scanf("%d",&eng);

printf("enter marks of account\n");
scanf("%d",&acc);

printf("enter marks of eco.\n");
scanf("%d",&eco);

printf("enter marks of bstd\n");
scanf("%d",&bstd);

printf("enter marks of csc\n");
scanf("%d",&csc);

fprintf(k,"%d %s %d %d %d %d %d\n",sid,sname,eng,acc,eco,bstd,csc); //writing data to data file
printf("want to continue (y/n)\n");//prints message
choice=getche();                      //gets a character                                  //getting a character from user
}while(choice!='n');        //writing repeats as you enter 'y'                                    // validating the input
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
printf("-----------------------------------------------\n");
printf("read records are:\n");
k=fopen("sdetail.txt","r");//file opening

while((fscanf(k,"%d %s %d %d %d %d %d",&sid,sname,&eng,&acc,&eco,&bstd,&csc))!=EOF)
//inputs goes until the file ends(End of File)
{
printf("sid=%d sname=%s ,eng=%d acc=%d,eco=%d,bstd=%d,csc=%d\n",sid,sname,eng,acc,eco,bstd,csc);  //printing on screen
}
printf("\n reading process completed successfully\n");
fclose(k);                                                           //closing data file

getch();
return 0;
}

q5)Write a program to enter name,roll_no and marks of 10 students and store them in the file.Read and display the record from the file.

ans:-

code:

//to store std_id,name and marks(5 subjects) of 10 students
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char sname[30];                                                    //identifier declaration
int roll_no,count=1;                                                              //       "             "
int eng,acc,eco,bstd,csc;
k=fopen("sdetail.txt","w");//file opening
printf("we are going to write /store data\n");
do
{

printf("enter record for %d student\n",count);
printf("\nenter roll no.\n");   //data input
scanf("%d",&roll_no);

printf("enter  name\n");           // getting inputs
scanf("%s",sname);
printf("enter marks of english\n");
scanf("%d",&eng);

printf("enter marks of account\n");
scanf("%d",&acc);

printf("enter marks of eco.\n");
scanf("%d",&eco);

printf("enter marks of bstd\n");
scanf("%d",&bstd);

printf("enter marks of csc\n");
scanf("%d",&csc);

fprintf(k,"%d %s %d %d %d %d %d\n",roll_no,sname,eng,acc,eco,bstd,csc); //writing data to data file
count++;

printf("-----------------------------------\n");
}while(count<=10);      
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
printf("-----------------------------------------------\n");
printf("read records are:\n");
k=fopen("sdetail.txt","r");//file opening

while((fscanf(k,"%d %s %d %d %d %d %d",&roll_no,sname,&eng,&acc,&eco,&bstd,&csc))!=EOF)
//inputs goes until the file ends(End of File)
{
printf("roll no=%d sname=%s ,eng=%d acc=%d,eco=%d,bstd=%d,csc=%d\n",roll_no,sname,eng,acc,eco,bstd,csc);  //printing on screen
}
printf("\n reading process completed successfully\n");
fclose(k);                                                           //closing data file

getch();
return 0;
}


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

case study:

Write a menu driven program which performs the following tasks.

1.Create a data file named "class12.data" and write record(roll_no,name,class and age) of student until 'n' is typed.

2.read all records from same file and display on the screen in appropriate format.

3.Display records of students whose age is more than 18.

4.Delete the particular record from the datafile on the basis of roll

5.Exit


Lab works-08(Pointer)

 Questions:-

q1)Write a program to swap two numbers using pointer.

q2)Write a program using call by value and call by reference to swap two numbers

3.)Write a progrm to input 25 numbers and display them in ascending order.

4.)Write a program to input 100 numbers and search a given number given by user.

5.)Write a program to calculate the sum and average of 10 numbers.

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

solution:

q1)Write a program to swap two numbers using pointer.

ans:-

code:

//pointer to swap two numbers
#include <stdio.h>

int main()
{
    int num1,num2,t;                //variables declaration
    int *p1,*p2;                     //pointer to store address
    printf("enter two numbers\n");
    scanf("%d%d",&num1,&num2);      //gets input
    printf("before swapping\n");
    printf("num1=%d,num2=%d\n",num1,num2);//display of values before swap
    p1=&num1;                             //assignment of address to first pointer
    p2=&num2;                              // assignment of address to second pointer
    t=*p1;                                //assigning first value to third variable
    *p1=*p2;                              //assigning second value to first pointer
    *p2=t;                                 //assigning first  value to second pointer
     printf("after swapping\n");
    printf("num1=%d,num2=%d\n",num1,num2);//prints values after swapping
    return 0;
}

q2)Write a program using call by value and call by reference to swap two numbers

ans:-

code:

call by value:

// call by value
#include <stdio.h>

void callbyvalue(int, int);                            /* function Prototype i.e. function declaration */

void main()                                                 /* Main function */
{
  int x = 10, y= 20;                                 // variables declaration

                                                                    /* actual arguments will be as it is */
  callbyalue(x, y);                             // passing value of x and y
  printf("x= %d, y= %d\n", x, y);    // displaying the values
}

void callbyvalue(int m, int n)             // passing values to formal parameter m and n
{
  int t;
  t = m;
m = n;
 n = t;                              // swapping takes place
}

call by reference:

// call by value reference
#include <stdio.h>
void callbyref(int*, int*);             /* function Prototype with two pointer as parameter*/

int main()                            /* Main function */
{
  int x = 10, y = 20;                 //variable declaration

    printf("before calling\n");                        
  printf("x=%d,y=%d\n",x,y);        //value display before call
  callbyref(&x, &y);              // passing address; /* actual arguments will be altered */
  printf("after calling\n");
  printf("x= %d, y= %d\n", x, y);   //displaying value after calling
  return 0;
}

void callbyref(int *m, int *n)
{
  int t;
  t = *m;
  *m = *n;
  *n = t;                       //swapping being done
}

3.)Write a program to input 25 numbers and display them in ascending order.

ans:-

code:

//program to sort numbers using pointer //comment of program
#include <stdio.h>            //header file
int main()                   //main function
{
    int *p;                   //pointer declaration
    int a[25],i,j,k;          //variable declaration
    printf("enter  numbers\n");
    for(i=0;i<25;i++)      //runs from 0 to one less than total
    {
        scanf("%d",a+i);   //gets value for given address. This input goes upto last value of address
    }
    printf("before sorting,values are\n");
    for(i=0;i<25;i++)
    {
        printf("%d\n",*(a+i));//displays its value with the help of address before sorting
    }
    printf("after sorting,values are\n");
    for(i=0;i<25;i++)
    {
        for(j=i+1;j<25;j++)
        {
            if(*(a+i)>*(a+j))               //sorting swapping taking place with its value
            {
                k=*(a+i);
                *(a+i)=*(a+j);
                *(a+j)=k;
            }
        }
    }
    for(i=0;i<25;i++)              //value display in sorted form
{
    printf("%d\n",*(a+i));
}
    return 0;
}

4.)Write a program to input 100 numbers and search a given number given by user.

ans:-

code:

//program to print all locations of a number  array. //comment of program
#include <stdio.h>            //header file
int main()                   //main function
{
    int *p;                   //pointer declaration
    int a[100],i,k,num;          //variable declaration

int found=0;
    printf("enter numbers\n");
    
    p=a;                       //stores base address
    for(i=0;i<100;i++)      //runs from 0 to one less than 100
    {
        scanf("%d",p+i);   //gets value for given address. This input goes upto last value of address
    }
    printf("values are\n");
    for(i=0;i<100;i++)
    {
        printf("%d\n",*(p+i));//displays its value with the help of address
    }
    printf("enter a number to be searched\n");
    scanf("%d",&num);                    //gets a number to be searched
    for(i=0;i<100;i++)
    {
            if(*(p+i)==num)               //comparison being done  with its value
            {
                 found=1;

                    break;

            }
    }

if(found==1)

{

printf("num=%d found and its location=%d\n",num,i);//prints value

}

else

{

printf("%d not found in the list.\n",num);

}
    return 0;
}

5.)Write a program to calculate the sum and average of 10 numbers.

Ans:

code:-

//program to print sum and average of 10 numbers using pointer. 
#include <stdio.h>           
int main()                  
{
    int *p;                   
    int a[20],i,k,sum=0;          //variable declaration
    
    printf("enter numbers\n");
    
    p=a;                       //stores base address
    for(i=0;i<10;i++)      //runs from 0 to one less than total
    {
        scanf("%d",p+i);   //gets value for given address. This input goes upto last value of address
        sum=sum+*(p+i);   // finds sum
    }
    printf("the sum=%d\n",sum);          // prints sum
    printf("the average =%f\n",(float)sum/total);//prints average
    return 0;
}

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

case study:-

Discuss the similarity and differences between array and pointer.

ans:-

Similarity:-

With the help of following points, we can show some similarities between array and pointer.

1.array and pointer , both are variables.

2.For an array a[4], if we write a+0, it  is same as p+0 using pointer after assigning base address.

3.They both can store multiple data items.

Differences:

Array

Pointer

Array is a constant pointer.

Pointer variable can be changed.

It refers directly to the elements.

It refers address of the variable.

Memory allocation is in sequence.

Memory allocation is random.

Allocates the memory space which cannot resize or reassigned.

Allocated memory size can be resized.

It is a group of elements.

It is not a group of elements. It is a single variable.

Array can be initialized at definition. Example
int num[] = { 2, 4, 5}

Pointer can’t be initialized at a definition

The assembly code of Array is different than Pointer.

The assembly code of Pointer is different than Array.