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 |
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. |
Thank you sir for your support . It really supported us while doing our assignments.
ReplyDelete