-->

neb37:Differentiate between while and do.. while loop.

Differentiate between while and do.. while loop.
ans:
Following are differences between while and do...while loop




while
Do..while
1
It is called pre-testing loop.
it is called post-testing loop
2
It uses keyword ‘while’.
it uses keyword ‘do.. while’
3
Its syntax is
Initialization;
While(condition)
{
Statement;
Increment/decrement;
}
 Its syntax is
Initialization;
do
{
Statement;
Increment/decrement;
              }while(condition);
4
nothing will happen if the condition is false.
 even after false(condition), it will run at least once.
5
Its flowchart is



Its flowchart is


6
example is
Int i=1;
While(i<=5)
{
Printf(“%d”,i);
I++;
}
 example is
Int i=1;
do
{
Printf(“%d”,i);
I++;
}while(i<=5);

q36)Wap to display those records which have price >340. Records are with fields name,price and edition.

//Wap to display those records which have price >340. Records are with fields name,price and edition.
#include<stdio.h>
include<conio.h>
void main()
{
FILE *k;                                                                    //  pointer for file declaration
char book_name[30],author_name[40];                   //identifier declaration
int book_edition;                                                         //       "             "
k=fopen("book.dat","r");

while((fscanf(k,"%d %s %d",&book_price,book_name,&book_edition)!=EOF))//reading data from data file
{
 if(book_price>340)                                                   // condition validating
{
printf("book name=%s ,book edition=%d and price=%d",book_name,book edition,book_price); //displaying data
}
}
fclose(k);                                                                                   //closing data file
getch();
}

NEB35:WAP top read all records stored in a file book.dat. Fields are name,price and author's name.

//WAP top read all records stored in a file book.dat. Fields are name,price and author's name.
#include<stdio.h>
include<conio.h>
void main()
{
FILE *k;                                                                    //  pointer for file declaration
char book_name[30],author_name[40];                   //identifier declaration
int book_price;                                                         //       "             "
k=fopen("book.dat","r");

while((fscanf(k,"%d %s %s",&book_price,book_name,author_name)!=EOF))//reading data from data file
{
printf("book name=%s ,author name=%s and price=%d",book_name,author_name,book_price); //displaying data
}
fclose(k);                                                                                   //closing data file
getch();
}

NEB34:WAP to store details about book(any 'n') having fields name,price,edition.

//WAP to store details about book(any 'n') having fields name,price,edition.
#include<stdio.h>
include<conio.h>
void main()
{
FILE *k;                                                                    //  pointer for file declaration
char book_name[30],choice;                                                    //identifier declaration
int book_price,edition;                                             //       "             "
k=fopen("book.dat","w");
do
{
printf("enter book's name\n");                                       // getting inuts
gets(book_name);
printf("enter edition\n");
scanf("%d",&edition);
printf("enter book's price\n");
scanf("%d",&book_price);
fprintf(k,"%s %d %d",book_name,edition,book_price);   //writing data to data file
printf("want to continue y/n\n");
choice=getche();                                                        //getting a character from user  
}while(choice!='n');                                                      // validating the input
printf("writing process completed successfully\n");
fclose(k);                                                                                   //closing data file
getch();
}

NEB33":WAP to store book's name,price and author's name in a file namd 'book.dat'.

//WAP to store book's name,price and author's name in a file named 'book.dat'.
#include<stdio.h>
include<conio.h>
void main()
{
FILE *k;                                                                    //  pointer for file declaration
char book_name[30],author_name[40];                   //identifier declaration
int book_price;                                                         //       "             "
k=fopen("book.dat","w");
printf("enter book's name and author's name\n");   // getting inuts
gets(book_name);
printf("enter author's name\n");
gets(author_name);
printf("enter book's price\n");
scanf("%d",&book_price);
fprintf(k,"%s %s %d",book_name,author_name,book_price);   //writing data to data file
printf("writing process completed successflly\n");
fclose(k);                                                                                   //closing data file
getch();
}




NEB32:three data file handling functions with examples.

Explain any three data file handling functions with examples.
ans:-
There are many data file functions which we use while programming. Following are some functions used to handle data stored in data file.

1) fopen(): It is used to open the file in particular mode. Its syntax is
                FILE *fopen(const char *filename, const char *mode); 
or 
FILE pointer= fopen("file name","mode");

Example
FILE *k;
k=fopen("data.txt","w");
This statement opens the file named data.txt in writing mode(w). Here ,writing mode means, we are going to store data in data file .

2)fclose(): It is used to close the opened file.As we have finished working with data file,it needs to be closed; so, at last we close it using fclose().

syntax
fclose(FILE name);
or fclose(FILE stream);

example
FILE *k;

k=fopen("data.txt","w");

..................

fclose(k);
Here we have closed data file using fclose() function.

3) fprintf(): It is used to write data/records in an opened data file i.e it writes data in an opened data file.

 syntax
int fprintf(FILE *stream,constant char *format);

example;
FILE *k;

k=fopen("data.txt","w");

fprintf(k,"%s %s  %s","hello","world", "of c");

this writes data (mentioned above) in an opened file data.txt.
4) fscan(): It is used to read data/records from opened data file. 
 syntax
int fscanf(FILE *stream,constant char *format,...);

example;
FILE *k;

k=fopen("data.txt","r");
char string[100];fscanf(k,"%s %s  %s",string,string,string);

this reads data (mentioned above) from opened file data.txt.


NEB31:What is pointer?explain it. With the help of program, explain about 'call by value' and 'call by reference'.

ans:
first part:-
                A pointer is a variable which points to the address and not its value. with the help of pointer, we can
                       ->we can allocate memory dynamically
                       ->better memory management
                        ->passing of array and string to the functions more efficiently
                        ->it helps us to return more values etc.
example:
   int *k;
here , k is a pointer and will store address of a variable.

second part:-
call by value:-
                         We can pass argments/parameters either by value or address.
 passing by value means we are going to copy the value directly to the formal arguments from real arguments. the change is formal arguments have no effect in real argument.Example is here.
// 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
}

out put is: x=10,y=20
Here, the values of x and y are no affected by  values of m and n. so there will be no swapping.

call by reference:- We have now second method to pass our arguments/paramenter.In this, we use pointer to pass addressof parametersto the functions. in this way the values are copied to formalparameter and process continues there.example is given here.


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

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

                                                                                                  /* actual arguments will be altered */
  callbyref(&x, &y);                                                               // passing addresss
  printf("x: %d, y: %d\n", x, y);                                            //displaying value after altering
}

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


output: x=20,y=10
In above example we have passed address of identifier x and y to the function.while passing address,its vale is copied to formal parameter and then swapping takes place. It means by passing address we can alter the value in real parameter which was not possible in call by value,