Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
32 views59 pages

C Lab Assignments

This document contains a series of programming assignments for a Master of Computer Applications course, focusing on C programming. It includes various tasks such as calculating tax, manipulating fractions, determining the largest and smallest numbers, and working with dates. Each task is accompanied by sample code and expected input/output formats.

Uploaded by

Riya Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views59 pages

C Lab Assignments

This document contains a series of programming assignments for a Master of Computer Applications course, focusing on C programming. It includes various tasks such as calculating tax, manipulating fractions, determining the largest and smallest numbers, and working with dates. Each task is accompanied by sample code and expected input/output formats.

Uploaded by

Riya Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

NAME – RIYA GARG

SCHOLAR NO. : 212120132

COURSE – MASTER OF COMPUTER APPLICATIONS


ASSIGNMENT
OF
C LAB

SUBMITTED TO :
Mr. Anirban Chaudhary
C LAB QUESTIONS
Lab -1 Questions
Q1. WAP to ask a user to enter Rs. 100.00 and then display the amount with
5% tax added.
#include<stdio.h>
int main()
{
float amount , taxamount ;
printf("Enter the amount");
scanf("%f",&amount);
taxamount = ((0.05*amount) + amount);
printf("Amount after tax is %f",taxamount);
return 0;
}

Q2. WAP that ask user to enter a value of x and display the value of the
following form
(i) 3x^5 +2x^4-5x^3-x^2+7x-6
(ii) ((((3x+2)x-5)x-1)x+7)x-6
#include<stdio.h>
int main()
{
int x,a,b;
printf("Enter the value of x");
scanf("%d",&x);
a = 3*x*x*x*x*x+ 2*x*x*x*x- 5*x*x*x-x*x+7*x-6;
b = ((((3*x + 2)*x-5)*x-1)*x+7)*x-6;
printf("Result is %d and %d",a,b);
return 0;

Q3. Input: Enter first fraction : 5/6


Enter second fraction : 3/4
Output: Sum of 2 fractions = 38/24
#include<stdio.h>
int main()
{
int a,b,c,d,e;
printf("Enter the values of a,b,c,d(numerator and denominator of 2
fractions)");
scanf("%d%d%d%d",&a,&b,&c,&d);
e = ((a*d)+(b*c));
printf("Sum is %d/%d",e,b*d);
return 0;

LAB-2 Questions
Q1.WAP that asks user to enter a two-digit numbet, you have to
print the number with digit reverse(don’t use for loop)
#include<stdio.h>
int main()
{
int n,a,b;
printf("Enter 2-digit number");
scanf("%d",&n);
a=n/10;
b=n%10;
printf("Reverse digit is %d",((b*10)+a));
return 0;

}
Q2. WAP to calculate how many digit a number contains. Assume
the number contains no more than 3-digit.
I: Give a number:375
O: The number 375 has 3-digit(don’t use for loop)
#include<stdio.h>
int main()
{
int n;
printf("Enter a number which does not have more than 4-digit");
scanf("%d",&n);
if((n/1000)!=0)
{
printf("This number has 4 digits");
}
else if((n/100)!=0)
{
printf("This number has 3-digits");
}
else if((n/10)!=0)
{
printf("This number has 2-digits");
}
else
{
printf("This number has 1-digit");
}
return 0;
}

Q3. WAP that finds the largest and smallest of three number
entered by the user.
I : Give me three integer :21,43,10
O : Largest : 43
Smallest : 10
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter three numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("Largest number is %d\n",a);
}
else
{
printf("Largest number is %d\n",c);
}

}
else
{
if(b>c)
{
printf("Largest number is %d\n",b);
}
else
{
printf("Largest number is %d\n",c);
}
}
if(a<b)
{
if(a<c)
{
printf("Smallest number is %d\n",a);
}
else
{
printf("Smallest number is %d\n",c);

}
}
else
{
if(b<c)
{
printf("Smallest number is %d\n",b);
}
else
{
printf("Smallest number is %d\n",c);
}
}
return 0;
}
Q4. WAP that prompt the user to enter two dates and then print
which date comes before
#include<stdio.h>
int main()
{
int a1,b1,c1,a2,b2,c2;
printf("Give first data(dd/mm/yy)");
scanf("%d%d%d",&a1,&b1,&c1);
printf("Give second date(dd/mm/yy)");
scanf("%d%d%d",&a2,&b2,&c2);

if(c1>c2)
{
printf("%d/%d/%d is earlier",a2,b2,c2);
}
else if(c1=c2)
{
if(b1>b2)
{
printf("%d/%d/%d is earlier",a2,b2,c2);
}
else if(b1=b2)
{
if(a1>a2)
{
printf("%d/%d/%d is earlier",a2,b2,c2);
}
else
{
printf("%d/%d/%d is earlier",a1,b1,c1);
}
}
else
{
printf("%d/%d/%d is earlier",a1,b1,c1);
}
}
else
{
printf("%d/%d/%d is earlier",a1,b1,c1);
}
return 0;
}

Q5. In a state , a residence is subject to tax


Not over Rs 750 1% of income
750 – 2250 7.50 + 2% of over 2250
2250 – 3750 37.50 + 35 of over 3750
3750 - 5250 82.50 + 4% of over 5250
5250 – 7000 142.50 + 5% of over 7000
Over 7000 230.00 + 6& of 7000
#include<stdio.h>
int main()
{
int a;
float b;
printf("Enter income");
scanf("%d",&a);
if(a>=0&&a<750)
{
b = 0.01*a;
printf("%f",b);
}
else if(a>=750&&a<2250)
{
b = ((0.02*2250)+7.50);
printf("%f",b);
}
else if(a>=2250&&a<3750)
{
b = ((0.03*3750)+37.50);
printf("%f",b);
}
else if(a>=3750&&a<5250)
{
b = ((0.04*5250)+82.50);
printf("%f",b);
}
else if(a>=5250&&a<7000)
{
b = ((0.05*7000)+142.50);
printf("%f",b);
}
else if(a>=7000)
{
b = ((0.06*7000)+230.00);
printf("%f",b);
}
return 0;
}

LAB – 3 QUESTIONS
Q1. Input: Enter date(dd/mm/yyyy) : 19/07/2021
Output : Dated this 19th day of July ,2021
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter date(dd/mm/yyyy)");
scanf("%d%d%d",&a,&b,&c);
switch(a)
{
case 1:case 21:case 31:
printf("Dated this %dst day of\t",a);
break;
case 2:case 22:
printf("Dated this %dnd day of\t",a);
break;
case 3:case 23:
printf("Dated this %drd day of\t",a);
break;
default:
printf("Dated this %dth day of\t",a);
}
switch(b)
{
case 1:
printf("Jan,%d",c);
break;
case 2:
printf("Feb,%d",c);
break;
case 3:
printf("March,%d",c);
break;
case 4:
printf("April,%d",c);
break;
case 5:
printf("May,%d",c);
break;
case 6:
printf("June,%d",c);
break;
case 7:
printf("July,%d",c);
break;
case 8:
printf("August,%d",c);
break;
case 9:
printf("Sep,%d",c);
break;
case 10:
printf("Oct,%d",c);
break;
case 11:
printf("Nov,%d",c);
break;
case 12:
printf("Dec,%d",c);
break;
}
return 0;
}

Q2. Using switch statement write a program that converts a


numerical grade into letter grade.
I: Give a numerical grade
O: Letter grade:
A = 90-100
B = 80-89
C = 70-79
D = 60-69
F = 0-59
Print an error message if grade is larger than 100 and
smaller than 0.
#include<stdio.h>
int main()
{
int a,b;
printf("Give a numerical grade");
scanf("%d",&a);
b = a/10;
switch(b)
{
case 10:
printf("Letter grade:A");
break;
case 9:
printf("Letter grade:A");
break;
case 8:
printf("Letter grade:B");
break;
case 7:
printf("Letter grade:C");
break;
case 6:
printf("Letter grade:D");
break;
case 5:
printf("Letter grade F");
break;
case 4:case 3:case 2:case 1:case 0:
printf("Letter grade:F");
break;
default:
printf("Error");

}
return 0;
}
LAB 4 QUESTIONS
Q1. The square root of a positive number n , can be
computed as a series of approximations , each more
accurate than the last . The first-approximation is one and
successive approximations are given by the following
formula
a(i+1) = (ai + (n/ai))/2
Haxe your program stop computing , when the
approximation is equal to the previous one within certain
epsilon.
#include<stdio.h>
#include<math.h>
int main()
{
double r=1;
int n;
printf("Enter the number");
scanf("%d",&n);
double P;
do{
P=r;
r=(r+(n/r))/2;
}
while(fabs(r-P)>0.01);
printf("Approximate value of square root of %d is %f",n,r);
return 0;

Q2. Write a program that asks the user to enter two-


integers and then calculate and displays their GCD
I: Give two integers : 12,18
O : GCD : 6
#include<stdio.h>
int main()
{
int a,b;
printf("Give two integers");
scanf("%d%d",&a,&b);
int i=a;
for(i=a;i>=0;i--)
{
if((a%i==0)&&(b%i==0))
{
printf("GCD:%d",i);
break;
}

}
return 0;

Q3. WAP to ask the user to enter a fraction, then reduce the
fraction into lowest-form
I: Enter a fraction: 6/12
O: In lowest-form : ½
#include<stdio.h>
int main()
{
int a,b,gcd,c,d,i;
printf("Enter a fraction:");
scanf("%d%d",&a,&b);
for(i=a;i>=1;i--)
{
if((a%i==0)&&(b%i==0))
{
gcd = i;
break;
}
}
c = a/gcd;
d = b/gcd;
printf("In lowest-form:%d/%d",c,d);
return 0;
}

Q4.The value of the mathematical-constant e is given by :


e = 1+ 1/1! + 1/2! + --- + 1/n!
WAP to approximate e by computing value of
e = 1+ 1/1! + 1/2! + --- + 1/n!
where n is an integer entered by the user.
#include<stdio.h>
int main()
{
int n,j,i;
float sum=0,mul=1;
printf("Enter the value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
mul *= i;
sum += 1/mul;

}
printf("%f",sum+1);
return 0;
}
Q5. WAP to find power of a number
I: Give a number and exponent: 2,10
O: 2^10 : 1024
#include<stdio.h>
int main()
{
int a,n,i;
int mul = 1;
printf("Give a number and exponent");
scanf("%d%d",&a,&n);
for(i=1;i<=n;i++)
{
mul *= a;

}
printf("%d",mul);
return 0;
}
LAB-5 QUESTIONS
Q1. Write a function digit(n,k) that returns the kth digit
from the right in n
Eg. digit(829,1) 9
Digit(829,2) 2
If k is greater than number of digits then function will return
zero
#include<stdio.h>
int digit(int n,int k)
{
int a,b,count=0;
while(n>=10)
{
a=n/10;
b=n%10;
count++;
if(count==k)
return b;
else
n=a;
}
if(count+1==k)
return a;
else
return 0;

void main()
{
int x,y,s;
printf("Enter the number");
scanf("%d",&x);
printf("Enter the digit number from the right which you
want to evaluate");
scanf("%d",&y);
s=digit(x,y);
if(s==0)
printf("This digit number is not valid for this number");
else
printf("The digit corresponding to %d digit number is
%d",y,s);

Q2. Write the following function:


Void find_two_largest(int *arr , int n,int *largest,int
*second_largest)
Where arr points to an array of length n and the
function searches for largest and second largest.
#include<stdio.h>
#include<limits.h>
void find_two_largest(int *arr,int n,int *l,int *sl)
{
*l = arr[0];
int i,x=0;
for(i=1;i<n;i++)
{
if(*l<arr[i])
{
*l = arr[i];
x = i;
}

}
arr[x]=INT_MIN;
*sl=arr[0];
int j;
for(j=1;j<n;j++)
{
if(*sl<arr[j])
*sl=arr[j];
}
}
int main()
{
int n;
printf("Enter the size of array");
scanf("%d",&n);
int arr[n],i;
printf("Enter the elements of array");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
int largest=0,second_largest=0;
find_two_largest(arr,n,&largest,&second_largest);
printf("Largest number in array is %d\n",largest);
printf("Second largest number in array is
%d",second_largest);
return 0;
}

Q3. Write a program that shows which digit(if any)were


repeated:
I: Give a number: 939577
O: Repeated digit: 7,9
#include<stdio.h>
int main(){
int arr[10]={0};
int n=0;
printf("\n Enter any number: ");
scanf("%d",&n);
while(n>0){
arr[n%10]++;
n/=10;
}
for(int i=0;i<10;i++){
if(arr[i]>1)
printf("%d ",i);
}
return 0;
}
Q4. Write a program to perform the following: a. Find the
largest in the array. b. Find average of all elements in the
array. c. No. of positive element in the array.
#include<stdio.h>
void largest(int arr[],int n){
int m=arr[0];
for(int i=1;i<n;i++)
if(m<arr[i])
m=arr[i];
printf("\n Largest element in array: %d",m);
}
void avg(int arr[],double n){
double sum=0;

for(int i=0;i<n;i++)
sum+=arr[i];
double a = sum/n;
printf("\n Average of elements of array: %lf", a);
}
void count_pos(int arr[],int n){
int pos=0;
for(int i=0;i<n;i++)
if(arr[i]>0)
pos++;
printf("\n Count of positive elements in array: %d",pos);
}
int main(){
int arr[5]={0};
printf("\n Enter 5 elements of Array: ");
for(int i=0;i<5;i++)
scanf("%d",&arr[i]);
largest(arr,5);
avg(arr,5);
count_pos(arr,5);
return 0;
}
Q5. Write the following function: double innerProduct
(double a[],double b[]); [If a = [1,2,4,5] and b = [6,7,8,9],
then innerProduct is defined by: res = a[0] * b[0] + ... +
a[3]*b[3] ].
# include<stdio.h>
double inner_product(double a[],double b[]){
double res;
for(int i=0;i<5;i++)
res+=a[i]*b[i];
return res;
}

int main(){

double arr1[5]={0};
double arr2[5]={0};
printf("\n Enter 5 elements of First Array: ");
for(int i=0;i<5;i++)
scanf("%lf",&arr1[i]);
printf("\n Enter 5 elements of Second Array: ");
for(int i=0;i<5;i++)
scanf("%lf",&arr2[i]);
printf("\n result of inner multiplication:
%lf",inner_product(arr1,arr2));
return 0;
}

Q6. Write a program that asks the user to enter a series of


integers, which it stores in an array, and then sorts that
array. Input: 7 1 89 21. Output: 1 7 21 89. [Use Selection
Sort].
#include<stdio.h>
int main()
{
int i,temp;
printf("enter the size of array");
scanf("%d",&i);
int a[i];
printf("enter the element of array");
for(int j=0;j<i;j++)
scanf("%d",&a[j]);
for(int j=0;j<i;j++)
{
for(int k=0;k<i-j-1;k++)
if(a[k]>a[k+1])
{
temp=a[k];
a[k]=a[k+1];
a[k+1]=temp;
}

}
for(int j=0;j<i;j++)
printf("%d ",a[j]);
return 0;
}

Lab 6 questions
Q1. An identity matrix is a square matrix whose values
are all zero except for those on the main diagonal, which
are one.
ex. [1 0 0
010
0 0 1]
is a 3 * 3 identity matrix. Write a function called
identity Matrix that takes a 5 × 5 matrix of integers as
its only argument and returns a boolean value
indicating whether the matrix is an identity matrix.
#include<stdio.h>
#define N 3
void check(int a[N][N]);
int main()
{
int a[N][N],i,j;
printf("enter element in array");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
scanf("%d",&a[i][j]);
}
}
check(a);
return 0;
}
void check(int a[N][N])
{int i,j,count=0;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(i==j&&a[i][j]==1||i!=j&&a[i][j]==0)
count++;
}
}
if(count==N*N)
printf("identity matrix");
else
printf("not identity matrix");
}
Q2. Prompts for five quiz grades for each of five students,
then compute the total score and average score for each
student, and the average score, high score, and low score
for each quiz.
#include<stdio.h>
#define N 5
void func(int a[N][N]);
int main()
{
int a[N][N],i,j;
printf("enter marks of five quiz of five student");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
scanf("%d",&a[i][j]);
}
}
func(a);
return 0;
}
void func(int a[N][N]){
int i,j;
for(i=0;i<N;i++)
{
int total=0;
float avg;
for(j=0;j<N;j++)
{
total+=a[i][j];
}
avg=(float)total/N;
printf("sum of marks of %d student = %d\n",i+1,total);
printf("average of %d student = %g\n",i+1,avg);
}
//second part//
for(i=0;i<N;i++)
{ int sum_col=0;
float avg_quiz;
for(j=0;j<N;j++)
{
sum_col+=a[j][i];
}
avg_quiz=(float)sum_col/N;
printf("average of %d quiz = %g\n",i+1,avg_quiz);
}
for(i=0;i<N;i++)
{
int max,min;
max=a[0][i];
min=a[0][i];
for(j=0;j<N;j++)
{

if(a[j][i]>max)
max=a[j][i];
if(a[j][i]<min)
min=a[j][i];
}
printf("max of %d quiz= %d\n",i+1,max);
printf("min of %d quiz= %d\n",i+1,min);
}
}
Q3. Assume that the following array contains a week's
worth of hourly temperature readings, with each row
containing the reading for one day:
int temperature[7][24]; (or temperature[7][7];)
Write a function to search the entire temperature array for
value 32.
#include<stdio.h>
void check(int a[7][7], int k);
int main()
{
int a[7][7],i,j,k;
printf("enter temperature");
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the temp you want to search");
scanf("%d",&k);
check(a,k);
return 0;

}
void check(int a[7][7],int k)
{
int i,j,count=0;
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
if(k==a[i][j])
count++;
}
}
if(count>=1)
printf("entered temp is in the given matrix");
else
printf("not found");
}

Q4.Write a program that declares an 8 × 8 char array


named checkerBoard and then use a for loop to store the
following data into the array (one character per array
element).
[B R B R B R B R
RBRBRBRB
BRBRBRBR
RBRBRBRB
BRBRBRBR
RBRBRBRB
BRBRBRBR
R B R B R B R B]
#include<stdio.h>
#define N 8
int main()
{

int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if((i+j)%2==0)
{
printf("B");
printf(" ");
}
else
{
printf("R");
printf(" ");
}
}
printf("\n");
}
return 0;
}

Q5.The Hermite Polynomial is defined as:


Hn (x) = 1, n<=0
2x, n = 1
2xHn-1(x) – 2(n - 1)Hn-2 (x), n >= 2.
For ex.
H3 (2) = 40.
Write a recursive function to calculate the value of
Hn(x).
Prototype: int hermite(int n, int x);
#include<stdio.h>
int hermit(int,int);
int main()
{
int n,x,k;
printf("enter a value of n and x");
scanf("%d%d",&n,&x);
k=hermit(n,x);
printf("value of hermit function be %d",k);
return 0;
}
int hermit(int n,int x)
{
if(n<=0)
return 1;
else if(n==1)
return 2*x;
else
return(2*x*hermit(n-1,x)-2*(n-1)*hermit(n-2,x));
}
Lab 7 Question
1. Assume that the fraction structure contains two
members: numerator and denominator (both of type
int). Write functions that performs the following
operations on fractions:
a. Reduce the fraction f to lowest term.
b. Add the fractions f1 and f2.
c. Subtract the fraction f2 from the fraction f1.
d. Multiply the fractions f1 and f2.
e. Divide the fraction f1 by the fraction f2.
The fraction f, f1 and f2 will be arguments of type
struct fraction: each function will return a value of the
type struct fraction. The fractions returned by the
functions in part b, and c should be reduced to lowest
term.
#include<stdio.h>
struct fraction{
int n;
int d;
};
typedef struct fraction frac;
frac lowest(frac f);
frac add(frac f,frac ft);
frac subt(frac f,frac ft);
frac pro(frac f,frac ft);
frac divi(frac f,frac ft);
int main()
{
frac f,fa,f1,f2,sum,sub,mul,div;
printf("enter a fraction");
scanf("%d/%d",&f.n,&f.d);
fa=lowest(f);
printf("lowest is %d/%d\n",fa.n,fa.d);
//enter two other fraction
printf("enter two fraction");
scanf("%d/%d",&f1.n,&f1.d);
scanf("%d/%d",&f2.n,&f2.d);
sum=add(f1,f2);
printf("sum of given fraction is %d/%d\n",sum.n,sum.d);
sub=subt(f1,f2);
printf("subtraction of given fraction is
%d/%d\n",sub.n,sub.d);
mul=pro(f1,f2);
printf("product of given fraction is %d/%d\n",mul.n,mul.d);
div=divi(f1,f2);
printf("Division of given fraction is %d/%d\n",div.n,div.d);
}
frac lowest(frac f)
{int gcd;
for(int i=1;i<=f.n&&i<=f.d;i++)
if(f.n%i==0&&f.d%i==0)
gcd=i;
f.n=f.n/gcd;
f.d=f.d/gcd;
return f;
}
frac add(frac f,frac ft)
{
frac sum;
sum.n=(f.n*ft.d + ft.n*f.d);
sum.d=(f.d* ft.d);
sum=lowest(sum);
return sum;
}
frac subt(frac f,frac ft)
{
frac sub;
sub.n=(f.n*ft.d - ft.n*f.d);
sub.d=(f.d * ft.d);
sub=lowest(sub);
return sub;
}
frac pro(frac f,frac ft)
{
frac mul;
mul.n=f.n*ft.n;
mul.d=f.d*ft.d;
mul=lowest(mul);
return mul;
}
frac divi(frac f,frac ft)
{
frac div;
div.n=f.n*ft.d;
div.d=ft.n*f.d;
div=lowest(div);
return div;
}

Q2. The following structures are designed to store


information about objects on a graphics screen:
struct Point{
int x;
int y; };
typedef struct Point point;
struct Rectangle{
point upperLeft;
point lowerRight; };
typedef struct Rectangle rectangle;
A point structure stores the x and y coordinates of a
point on the screen. A rectangle structure stores the
coordinates of the upper left and lower right corners of
a rectangle. Write function that performs the following
operations on a rectangle structure r passed as an
argument:
a. Compute the area of r.
b. Compute the center of r, returning it as a point value. If
either the x or y coordinate of the center isn't an integer,
store its truncated value in the point structure.
#include<stdio.h>
#include<math.h>

typedef struct
{
int x;
int y;
}point;

typedef struct
{
point upper_left;
point lower_right;
}rectangle;

int main()
{
point p1, p2, mid;
rectangle rect;
int length, breadth;
scanf("%d%d", &p1.x, &p1.y);
scanf("%d%d", &p2.x, &p2.y);

rect.upper_left = p1;
rect.lower_right = p2;

length = abs(rect.upper_left.x - rect.lower_right.x);


breadth = abs(rect.upper_left.y - rect.lower_right.y);

printf("The area of the rectangle is %d units\n", length *


breadth);

mid.x = (rect.upper_left.x + rect.lower_right.x)/2;


mid.y = (rect.upper_left.y + rect.lower_right.y)/2;

printf("The mid point of the rectangle is (%d , %d)\n",


mid.x, mid.y);
return 0;
}
Q3. Write the following function:
int *createArray(int n, int initialValue);
The function should return a pointer to a dynamically
allocated integer array with n members, each of which
is initialized to initialValue. The return value should be
NULL if the array can't be allocated.
#include<stdio.h>
#include<stdlib.h>

int *create_array(int n , int initial_value)


{
int *ar;
ar = malloc(n * sizeof(int)); //dynamically allocating
memory from the heap
if(ar==NULL)
return NULL;
for(int i = 0l; i < n; i++)
*(ar + i) = initial_value; //initialising all the locations
return ar;
}

int main()
{
int *array;
int init, n;
printf("enter size of array and initial value");
scanf("%d%d", &n, &init);

array = create_array(n, init); //call to the function

for(int i = 0; i < n; i++)


printf("%d ", *(array + i));
printf("\n");

return 0;
}
Q4.Write a function named duplicate that uses dynamic
storage allocation to create a copy of a string
For ex., the call: p = duplicate(str);
would allocate space for a string of the same length as str.
copy the contents of the str into the new string, and return
a pointer to it. Have duplicate return a null pointer if the
memory allocation fails.
#include<stdio.h>
#include<stdlib.h>

char *duplicate(char *src)


{
char ch;
char *dest;
int i = 0, len = 0;
while(*(src + i) != '\0')
{
i++;
len++;
}
dest = malloc(len+1);
if(dest==NULL)
return NULL;
for(i = 0; i < len; i++)
*(dest + i) = *(src++);

return dest;
}

int main()
{
char *name = "Hello";
char *dup_name;
dup_name = duplicate(name);
printf("Dupliacted as : %s\n", dup_name);
return 0;
}
Q5. Write the following function:
int sum(int (*f)(int), int start, int end);
Call: int res = sum(g,2,5);
Definition: g(1) + g(2) + … + g(5)
#include<stdio.h>
int g(int n)
{
return (n*n);
}
int sum(int(*f)(int),int start,int end)
{
int sum=0;
for(int k=start;k<end;k++)
{
sum=sum+g(k);
}
return sum;
}
int main()
{
int i,j;
printf("Enter the startvalue and endvalue for function:");
scanf("%d %d",&i,&j);
int res=sum(g,i,j);
printf("%d",res);
return 0;
}

You might also like