Sunteți pe pagina 1din 17

LOVELY PROFESSIONAL UNIVERSITY

ASSIGNMENT 4
OF “C”

SUBMITTED TO: SUBMITTED BY:


MR. KETAN SOOD DEEPA VIJ
LECT. RE3902A13
MCA M-TECH (IST SEM)
PART A

Q.1. How can we store address of different elements of an array using


pointers?

Ans : An array is actually very much like pointer. We can declare the arrays first element as a[0]
or as int *a because a[0] is an address and *a is also an address the form of declaration is
equivalent. The difference is pointer is a variable and can appear on the left of the assignment
operator that is lvalue. The array name is constant and cannot appear as the left side of
assignment operator.

For exp:

main()
{
int a[100];
int i,j,n;
printf(“\nEnter the elements of the array\n”);
scanf(“%d”,&n);
printf(“Enter the array elements”);
for(I=0;I< n;I++)
scanf(“%d”,&a[I]);
printf(“Array element are”);
for(ptr=a,ptr< (a+n);ptr++)
printf(“Value of a[%d]=%d stored at address %u”,j+=,*ptr,ptr);
}

Strings are characters arrays and here last element is \0 arrays and pointers to char arrays can be
used to perform a number of string functions.

Q.3. How pointer is different from pointer to pointer give a program as an


example?

Ans. In c a pointer is a variable that points to or references a memory location in which data is
stored. Each memory cell in the computer has an address that can be used to access that location
so a pointer variable points to a memory location we can access and change the contents of this
memory location via the pointer.

Pointer declaration:
A pointer is a variable that contains the memory location of another variable. The syntax is as
shown below. One can start by specifying the type of data stored in the location identified by the
pointer. The asterisk tells the compiler that you are creating a pointer variable. Finally you give
the name of the variable.
type * variable name

Example:

int *ptr;
float *string;

Address operator:
Once we declare a pointer variable we must point it to something we can do this by assigning
to the pointer the address of the variable you want to point as in the following example:
ptr=&num;
This places the address where num is stores into the variable ptr. If num is stored in memory
21260 address then the variable ptr has the value 21260.

A program to illustrate pointer declaration

main()
{
int *ptr;
int sum;
sum=45;
ptr=∑
printf (“\n Sum is %d\n”, sum);
printf (“\n The sum pointer is %d”, ptr);
}

we will get the same result by assigning the address of num to a regular(non pointer) variable.
The benefit is that we can also refer to the pointer variable as *ptr the asterisk tells to the
computer that we are not interested in the value 21260 but in the value stored in that memory
location. While the value of pointer is 21260 the value of sum is 45 however we can assign a
value to the pointer * ptr as in *ptr=45.

This means place the value 45 in the memory address pointer by the variable ptr. Sincethe
pointer contains the address 21260 the value 45 is placed in that memory location. And since this
is the location of the variable num the value also becomes 45. this shows how we can change the
value of pointer directly using a pointer and the indirection pointer.

Program to display the contents of the variable their address using pointer variable
include< stdio.h >
{
int num, *intptr;
float x, *floptr;
char ch, *cptr;
num=123;
x=12.34;
ch=’a’;
intptr=&x;
cptr=&ch;
floptr=&x;
printf(“Num %d stored at address %u\n”,*intptr,intptr);
printf(“Value %f stored at address %u\n”,*floptr,floptr);
printf(“Character %c stored at address %u\n”,*cptr,cptr);
}

Q.3.WAP to compute sum of all the elements stored in a single dimension


array using pointers?

Ans. #include<stdio.h>
#include<conio.h>
main()
{
int a[10],s,*p,i,n;
*p=a[10];
clrscr();
printf("enter how many terms");
scanf("%d",&n);
for(i=0;i<n;i++)

scanf("%d",&*p);
s=0;
for(i=0;i<n;i++)
s=s+(*p);
printf("sum=%d",s);
getch();
}
Output:

Q.4. WAP to determine length of a string using pointers


Ans.

#include<stdio.h>
#include<conio.h>
int str_len(char *);
main()
{
char s[30];
int count;
printf("enter the string :");
gets(s);
count=str_len(s);
printf("the length is :%d",count);
getch();
}
int str_len(char *a)
{
int i=0;
while(*a!='\0')
{
a++;
i++;
}
return i;
}

Q.5. WAP for swapping values of two variables using pointers as arguments
to functions?
Ans.

#include<stdio.h>
void swap(int *x, int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
main() {
int m = 10, n = 20;
printf("\n Before executing swap m=%d n=%d", m, n);
swap(&m, &n);
printf("\n After executing swap m=%d n=%d", m, n);
getch();
}
PART B

Q. 1.Write a program to list of odd and even numbers entered in file using
getw() and putw() functions?

Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fpa,*fpe,*fpo;
int n,ce,co;
clrscr();
fpa=fopen("all num","w");
printf("input some integer number");
for(;;) //infinte loop
{
scanf("%d",&n);
if(n==-1)
break;
putw(n,fpa);
}
fclose(fpa);
fpa=fopen("all num","r");
fpe=fopen("even","w");
fpo=fopen("odd","w");
ce=0;
co=0;
while((n=getw(fpa))!=EOF)
{
if(n%2==0)
{
putw(n,fpe);
ce+=1;
}
else
{
putw(n,fpo);
co+=1;
}
}
fclose(fpa);
fclose(fpe);
fclose(fpo);
fpa=fopen("odd","r");
fpe=fopen("even","r");
printf("\n list of even");
while((n=getw(fpe))!=EOF)
printf("%d",n);
fclose(fpe);
printf("\n\n total number of even elements=%d",ce);
printf("\n\n list of odd");
while((n=getw(fpo))!=EOF)
printf("%d\t",n);
fclose(fpo);
printf("fpo");
printf("\n total odd=%d",co);
getch();
}

Output:
Q. 2.Write a program to display file contents 20 lines at a time. The program
pauses after displaying 20 lines until the user presses either Q to quit or
Return to display the next 20 lines
Ans:

#include<stdio.h>

#include<conio.h>

Main(int argc, char *argv[])

FILE *f1, *f2, f3;

Int I,m;

Char a,b;

F1= fopen(argv[1],”w”);

F2= fopen(argv[2],”w”);

Printf(“\n enter the no of elememts”);

Scanf(“%d”, &m);

Printf(“\n data for first file”);

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

A=getche();

Putc9a,f1);

Printf(“\n data for second file”);

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

B=getche();
Putc(b,f2);

Fclose(f1);

Fclose(f2);

F1= fopen(argv[1],”r+”);

F2= fopen(argv[2],”r+”);

F3= fopen(argv[3], “w”);

While ((a=getc(f1))!= EOF)&&(b=getc(f2)!=EOF))

If (a>b)

putc(b,f3);

Fseek(f1,-1,1);

Else

Putc(a,f3);

Fseek(f2,-1,1);

}}

If (a==EOF)

Putc (b,f3);

}}

If(b==EOF)

{
While ((a=getch(f1)!=EOF)

Putc(b,f3);

}}

If(b==EOF)

While((a=getch(f1))!=EOF)

Putc(a,f3);

Fclose(f3);

F1= fopen(argv[3],”r”);

Printf(“\n final data is”);

For (i=0;i<2;i++)

a=getc(f1);

Printf(“%c”, a);

Fclose(f1);

Getch();

Q.3.Write a program to copy the contents of one file to another?


Ans.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>

void main()
{
FILE *fs,*ft;
char ch;
printf("copying contents of one file to another file\n");
fs=fopen("payrpll.c","r");
if(fs==NULL)
{
perror("fopen");
puts("canot open the source file\n");
}

ft=fopen("payroll2.c","a");
if(ft==NULL)
{
puts("\ncannot open target file\n");
}

while(1)
{
ch=fgetc(fs);
if(ch==EOF)
break;
else
fputc(ch,ft);
}

fclose(fs);
fclose(ft);
getch();
}

Output:
Q. 4.What are the different modes in which files can be opened?
Ans: A file is opened by using the fopen() in built high level input/output function. The general
format for declaring and opening a file is as:

FILE *fp;

fp= fopen(“filename”,”mode”);

Here FILE is the data structure and is the low level reverse word of c language for the crating a
buffer area. Here fp is the file pointer which tells us the beginning of the buffer area for storing
the data, and file name is the name of the data file, and mode is the way in which file can be
opened.

These modes are as follows:

1. w – open the file for writing only

2. r – open the file for reading only

3. a – open the file for appending only


When ever we use “ w” mode, new file will be created. If the file is aready created, then
it will overwrite the data.

For exp:

FILE *p1, *p2, *p3;

p1 = fopen(“hello”, “r”);

It means the file opened for reading the data from “ hello” file

p2= fopen(“hello”, “w”);

It means the file opened for writing the data to “hello” file.

p3 = fopen(“hello”, “a”);

It means file opened for insert more data to “hello” file which is already created file.

Q.5. Distinguish between:

a) getc() and getchar()


b) printf() and fprintf()
c) feof() and ferror()

Ans:

a.) Difference between getc() and getchar()

getc(): the purpose of the getc() is to read a single character from the existing data file. The
general syntax used for this purpose is:
v= getc(fp);

here fp is the file pointer, v is the character type variable and getc() is the high level input output
function which is used for reading the character data.
For exp:
main()
{
FILE *fp;
fp= fopen(“hello”,”r”);
Char ab;
ab= getc(fp);
Printf(“\n the data stored in file is”);
Printf(“%c”,ab);
fclose(f1);
getche();
}

Getchar(): this is an input function. It is used to read a single character from keyboard it is a
buffered function. It gets the input from the keyboard and store it in the memory buffer
temporarily until user press the enter key. Syntax is:
v= getchar();
Here v is the variable of character type.

For exp:
#include<stdio.h>
main()
{
Char x;
x= getchar();
}

b.) Difference between printf() and fprintf():

Printf(): this is an output finction . it is used to display a text message and to display the
mixed type i.e. int, float, char etc of dat on the screen.
The syntax used is:
Printf(“ control string”, v1,v2…vn);
Or
Printf(“ message line or text line”);

For exp:

#include<stdio.h>
main()
{
Int a,b;

Printf(“\n enter the number”);


Scanf(“\n %d”, &a);
Printf(“ the data is %d”, a);
Getch();
}

fprintf(): this is used to write mixed data type in the data file. The mixed data type are
of integer, float, character etc. the general syntax used for this is:

fprintf(fp, “ contol strings”, v1,v2…..vn);

Here fp is the file pointer, control strings has all format codes or character conversion
codes. Here v1, v2…vn is the list of variables.

For exp:

main()
{
FILE *f1;
f1= fopen(“hello”, “w”);
Int rn;
Char name[20];
Printf(“\n enter the name and rollno”);
Scanf(“%d%s”, &rn,name);
fprintf(f1, “%d%s”, rn, name);
fclose(f1);
Getch();
}

c.) Difference between feof() and ferror():

feof(): this function is used to test the condition , whether a file has at end or not it stores
a pointer as argument and returns a non zero integer value, if all the data from the
specified file has been read and return the zero value in the other case. This function is
used to handle errors that occurs during the execution of files.
This function can be used as:

If ( feof(fp))
Printf(“\n end of the file reached”);

ferror(): this function tells us the status of the file. It also stores file pointer as argument
and returns a non zero value of integer type if any error occurs, otherwise it will return
the zero value means no error occurs. This function can be used as:
If( ferror(fp) 1=0)
{
Printf(“\n an error has been occurred”);
}

S-ar putea să vă placă și