0% found this document useful (0 votes)
8 views25 pages

C Unit 5 Answer

fvdzgdgdgdg

Uploaded by

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

C Unit 5 Answer

fvdzgdgdgdg

Uploaded by

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

UNIT-5 C

Single File Programming Question

Problem stattement

Ivy is working on a program and needs help in creating a code that


allows the user to input integer values for an array. The program
should prompt the user for an integer input n and n values, use a
void pointer to capture the address, and then print the assigned array
values.

Input format :

The first line contains an integer n, representing the size of the array.

The next line contains n space-separated integers, representing the


elements of the array.

Output format :

Print the elements of the array in a single line separated by a space.


Refer to the sample output for the formatting specifications.

Code constraints :

In this scenario, the test cases fall under the following constraints:

1<= n <=100

1<=elements <=20

Sample test cases :


Input 1 :
5
1 2 3 45 6
Output 1 :
1 2 3 45 6
Input 2 :
10
2 3 1 4 5 7 8 9 10 11
Output 2 :
2 3 1 4 5 7 8 9 10 11
Note :
The program will be evaluated only after the “Submit Code” is
clicked.
Extra spaces and new line characters in the program output will
result in the failure of the test case.

// You are using GCC


#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
void * ptr=arr;
for(int i=0;i<n;i++){
printf("%d ",*((int*)ptr+i));
}
}

Problem Statement
Sharon wants to create a program that declares and initializes
pointers of various types (void, integer, character, and float) and
prints their sizes using the sizeof operator.

Assist Sharon in achieving the task.

Input format :

No console input.

Output format :

The first line of output displays "Void Pointer = " followed by the size
of the void pointer.

The second line displays "Integer Pointer = " followed by the size of
the integer pointer.

The third line displays "Character Pointer = " followed by the size of
the character pointer.
The fourth line displays "Float Pointer = " followed by the size of the
float pointer.

Refer to the sample output for formatting specifications.

Sample test cases :


Input 1 :
Output 1 :
Void Pointer = 8
Integer Pointer = 8
Character Pointer = 8
Float Pointer = 8
Note :
The program will be evaluated only after the “Submit Code” is
clicked.
Extra spaces and new line characters in the program output will
result in the failure of the test case.

// You are using GCC


#include <stdio.h>
int main(){
void*ptr;
int*iptr;
char*cptr;
float*fptr;
printf("Void Pointer = %d",sizeof(ptr));
printf("Integer Pointer = %d",sizeof(iptr));
printf("Character Pointer = %d",sizeof(cptr));
printf("Float Pointer = %d",sizeof(fptr));
}

Single File Programming Question

Problem Statement

Shalini wants you to create a program that calculates the average of


even numbers in an array. The program should input the size of the
array and then the array elements. Using pointers, calculate the
average of only the even elements and print the result with one
decimal point precision.

Input format :
The first line of input consists of an integer N, representing the array
size.

The second line consists of N space-separated integers, representing


the array elements.

Output format :

The output prints a double value, representing the average of the


even elements, rounded off to one decimal place.

Refer to the sample output for formatting specifications.

Code constraints :

In this scenario, the test cases fall under the following constraints:

1 ≤ N ≤ 10

1 ≤ array elements ≤ 20

Sample test cases :


Input 1 :
5
12345
Output 1 :
3.0
Input 2 :
6
12 15 16 17 19 20
Output 2 :
16.0
Input 3 :
3
11 13 19
Output 3 :
0.0
Note :
The program will be evaluated only after the “Submit Code” is
clicked.
Extra spaces and new line characters in the program output will
result in the failure of the test case.

#include <stdio.h>
int main(){
float avg=0;
int sum=0;
int count=0;
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}

int *ptr = arr;

// Calculate sum and count of even numbers


for (int i = 0; i < n; i++) {
if (*ptr % 2 == 0) {
sum += *ptr;
count++;
}
ptr++; // Move the pointer to the next element
}

if (count > 0) {
avg =(float)sum / count;
printf("%.1f\n", avg);
} else {
printf("0.0\n");
}

return 0;
}

Single File Programming Question

Problem Statement

Simba is the mayor of a town with three schools - School A, School B,


and School C. He wants to determine the total number of students in
the town based on the number of students in School B.

Note:
The number of students in School A is three times the number of
students in School B.
The number of students in School C is 120 more than the combined
number of students in School A and School B.

Create a program using pointers to help Simba that takes the number
of students in School B as input and calculates the total number of
students in each school and overall students in the town.

Input format :

The input consists of an integer value N, representing the number of


students in school B.

Output format :

The first line of output prints "Total Students in School A: ", followed
by the total number of students in School A.

The second line prints "Total Students in School B: ", followed by the
total number of students in School B.
The third line prints "Total Students in School C: " followed by the
total number of students in School C.

The fourth line prints "Overall Student Count: " followed by the total
number of students in all three schools.

Refer to the sample output for formatting specifications.

Code constraints :

In this scenario, the test cases fall under the following constraints:

20 ≤ N ≤ 250

Sample test cases :


Input 1 :
128
Output 1 :
Total Students in School A: 384
Total Students in School B: 128
Total Students in School C: 632
Overall Student Count: 1144
Input 2 :
235
Output 2 :
Total Students in School A: 705
Total Students in School B: 235
Total Students in School C: 1060
Overall Student Count: 2000
Note :
The program will be evaluated only after the “Submit Code” is
clicked.
Extra spaces and new line characters in the program output will
result in the failure of the test case.

// You are using GCC


#include <stdio.h>
int main(){
int a=0,b,c=0;
scanf("%d",&b);
a=b*3;
c=a+b+120;
int overall=a+b+c;
printf("Total Students in School A: %d",a);
printf("Total Students in School B: %d",b);
printf("Total Students in School C: %d",c);
printf("Overall Student Count: %d",overall);
}

Single File Programming Question

Problem Statement

Simran is engaged in a game with her friend, tackling logical


expressions utilizing pointers.

Her friend has presented Simran with a specific expression to


evaluate:

exp = (−−m × −−n) × (n−− × m−−)


Assist Simran in successfully deciphering and calculating the result of
this expression using pointers.

Input format :

The input consists of two space-separated integers m and n.

Output format :

The output prints "Result of the expression: ", followed by the result.

Refer to the sample output for the formatting specifications

Code constraints :

In this scenario, the test cases fall under the following constraints:

1 ≤ m, n ≤ 10

Sample test cases :


Input 1 :
23
Output 1 :
Result of the expression: 4

Input 2 :
34
Output 2 :
Result of the expression: 36

Note :
The program will be evaluated only after the “Submit Code” is
clicked.
Extra spaces and new line characters in the program output will
result in the failure of the test case.

// You are using GCC


#include <stdio.h>
int main(){
int m,n;
scanf("%d %d",&m,&n);
int result=(--m * --n)*(n-- * m--);
printf("Result of the expression: %d",result);
}
Single File Programming Question

Problem Statement

Jai is creating a program to find the maximum number from the given
two integers using pointers.

Help him with the task.

Input format :

The input consists of two space-separated integers.

Output format :

The output prints the maximum number.

Code constraints :
In this scenario, the test cases fall under the following constraints:

1 ≤ input integers ≤ 100

Sample test cases :


Input 1 :
23 56
Output 1 :
56
Input 2 :
45 23
Output 2 :
45
Note :
The program will be evaluated only after the “Submit Code” is
clicked.
Extra spaces and new line characters in the program output will
result in the failure of the test case.

// You are using GCC


#include <stdio.h>
int main(){
int a,b;
int *ptr;
scanf("%d %d",&a,&b);
if(a>b){
printf("%d",a);
}else{
printf("%d",b);
}
}

Single File Programming Question

Problem Statement

Sarah and Tom love pizza! They decided to share a pizza, but they
were curious about how many pizza slices they had eaten. Help them
by creating a program using pointers that calculate the fraction of the
pizza slices they have eaten.

Input format :

The first line of input consists of an integer N, representing the total


number of pizza slices.
The second line consists of two space-separated integers,
representing the number of slices Sarah and Tom ate, respectively.

Output format :

The output prints "Sarah and Tom, together ate X/Y of the pizza."
where X is the number of slices Sarah and Tom ate and Y is the total
slices.

Refer to the sample output for formatting specifications.

Code constraints :

1 ≤ N ≤ 100

Sample test cases :


Input 1 :
8
32
Output 1 :
Sarah and Tom, together ate 5/8 of the pizza.
Input 2 :
16
10 6
Output 2 :
Sarah and Tom, together ate 16/16 of the pizza.
Note :
The program will be evaluated only after the “Submit Code” is
clicked.
Extra spaces and new line characters in the program output will
result in the failure of the test case.

// You are using GCC


#include <stdio.h>
int main(){
int ns,s,t;
scanf("%d",&ns);
int *ptr;
scanf("%d %d",&s,&t);
printf("Sarah and Tom, together ate %d/%d of the pizza.",s+t,ns);
}

Single File Programming Question

Problem Statement
Alex has a credit card debt of a certain amount with a specified
annual interest rate. He makes regular monthly payments to pay off
the debt.

Write a program to determine how many months it will take for Alex
to pay off the debt, given the debt amount, annual interest rate, and
monthly payment. Utilize pointers to calculate the result.

Input format :

The first line of input consists of a floating-point number,


representing the initial debt amount.

The second line consists of a floating-point number, representing the


interest rate in percentage.

The third line consists of a floating-point number, representing the


monthly payment amount.
Output format :

The output prints the number of months it will take for Alex to pay
off the debt.

Refer to the sample output for formatting specifications.

Code constraints :

In this scenario, the test cases fall under the following constraints:

1.00 ≤ debt amount ≤ 1,00,000.00

1.0 ≤ interest rate ≤ 100.0

1.0 ≤ monthly payment amount ≤ 1200.0

Sample test cases :


Input 1 :
5000.00
5.00
200.00

Output 1 :
27 months
Input 2 :
10000.00
8.5
300.00

Output 2 :
39 months
Note :
The program will be evaluated only after the “Submit Code” is
clicked.
Extra spaces and new line characters in the program output will
result in the failure of the test case.

#include <stdio.h>

int main() {
float debt, interest_rate, monthly;
int months = 0;
scanf("%f", &debt);
scanf("%f", &interest_rate);
scanf("%f", &monthly);

// Convert annual interest rate to monthly rate


float monthly_interest_rate = interest_rate / 12.0 / 100.0;

while (debt > 0) {


float interest = debt * monthly_interest_rate;
float payment_towards_principal = monthly - interest;

if (payment_towards_principal >= debt) {


debt = 0;
} else {
debt -= payment_towards_principal;
}
months += 1;
}

printf("%d months", months);

return 0;
}

You might also like