SRM Institute of Science and Technology College of Engineering and Technology School of Computing Department of Computing Technologies

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

SRM Institute of Science and Technology

College of Engineering and Technology


School of Computing
DEPARTMENT OF COMPUTING TECHNOLOGIES
SRM Nagar, Kattankulathur – 603203, Chengalpattu District, Tamilnadu
Academic Year: 2022-23 (ODD)

Test: CLAT- 2 Date:


Course Code & Title: 21CSS101J / Programming for Problem Solving Duration: 1 hr 40 mts
Year & Sem: I / I Max. Marks: 50

Course Learning Rationale (CLR):

CLR-2 : Utilize the appropriate operators and control statements to solve engineering problems
CLR-3 : Store and retrieve data in a single and multidimensional array
CLR-4 : Create custom designed functions to perform repetitive tasks in any application

Course Learning Outcomes (CLO):

CLO-2 : To use appropriate data types in simple data processing applications. To create programs using the concept
of arrays.
CLO-3 : To create string processing applications with single and multi-dimensional arrays.
CLO-4 : To create user-defined functions with required operations. To implement pointers in applications with
dynamic memory.

NOTE:
● For each 5 mark question, 2 marks logic (as seen in program irrespective of syntax errors) and 3
marks for program.
● Based on number of syntax errors in program, program marks (3) may be allotted.
● There is no need to write #inlcude<stdio.h> for all programs
● There are many ways of writing the same program!

Part - A
(1 x 25 = 25 Marks)
Instructions: This section has only ONE question with internal choice.
Q. Question Marks BL CO PO PI
No Code
1 National Highways Department announces a scheme for the
four-wheeler in the toll gate for 50th year celebration. If the 5 2 2 2 2.6.3
vehicle number is divisible by both 7 and 3 then they should
pay one-third of the fee. If the vehicle number is divisible by 7
or 3 then they should pay half of the fee. Otherwise, they have
to pay full fees. Write a c program for the same.

ANSWER:
#include <stdio.h>
int main()
{
int vehicleno;
int pay,tollfee=100;
printf("enter the vehicle number");
scanf("%d",&vehicleno);
if (vehicleno%7==0 && vehicleno%3==0)
pay=tollfee/3;
else if (vehicleno%7==0 || vehicleno%3==0)
pay=tollfee/2;
else
pay=tollfee;
printf("the payable amount is %d",pay);
return 0;
}
OUTPUT:
enter the vehicle number2304
the payable amount is 50
2.6.3
2 Mrs. Ninu wants to teach her five-year-old daughter on “ 5 2 2 2
seasons and months”. If Ninu tells her daughter the name of the
month, she must tell her the season for that month. Given the
month number M, the task is to print the season name of the
year based on the month number. Write a C program to
implement the same.
ANSWER:
#include <stdio.h>
void main()
{
// Checks out the season according
// to the month number entered by the user
int M;
printf("enter the number of month");
scanf("%d",&M);
switch (M)
{
case 12:
case 1:
case 2:
printf("\nWINTER");
break;
case 3:
case 4:
case 5:
printf("\nSPRING");
break;
case 6:
case 7:
case 8:
printf("\nSUMMER");
break;
case 9:
case 10:
case 11:
printf("\nAUTUMN");
break;
default:
// Handles the condition if number entered
// is not among the valid 12 months
printf("\nInvalid Month number");
break;
}
}
OUTPUT:
enter the number of month3
SPRING
7
3 Mr. Bob has been deputed as the Election officer for the Tamil
Nadu State Election. He wanted to perform an analysis to check
whether a candidate is eligible for voting when he/she enters
his/her age. Write a C program to read the age of a candidate
and determine whether it is eligible for casting his/her own
vote. 5 2 2 2.6.3
2
ANSWER:
#include <stdio.h>
void main()
{
int vote_age;
printf("Input the age of the candidate : ");
scanf("%d",&vote_age);
if (vote_age<18)
{
printf("Sorry, You are not eligible to caste your vote.\n");
printf("You would be able to caste your vote after %d
year.\n",18-vote_age);
}
else
printf("Congratulation! You are eligible for casting your
vote.\n");
}

OUTPUT:
Input the age of the candidate: 21
Congratulation! You are eligible for casting your vote.

4
Divya is interested to walk on the number line. She started from
1 and will go on walking along the number line in the positive
direction. She got a bag to collect those numbers from 1, 2, 3,
and so on up to N. Help her to find the sum of that numbers by
writing C code.

2.6.3
ANSWER: 2
5 3 2
#include <stdio.h>

int main()

int i, n, sum = 0;

/* Input upper limit from user */

printf("Enter any number: ");

scanf("%d", &n);

printf("Natural n0s from 1 to %d :\n", n);

/* * Start loop counter from 1 (i=1)


and go till n (i<=n)

* increment the loop count by 1 to get the


next value.

* For each repetition print the value of i.

*/

for(i=1; i<=n; i++)

sum += i
}

printf(“%d\n”, sum);

return 0;

Output

Enter any number: 10

Natural numbers from 1 to 10 :

10
5
Ms. Ahalya is a C programmer and she has been assigned work
on Student Grade Calculation. She is familiar with control flow
statements. Assist her in developing a C program to compute
student grades based on the following criteria:

Mark Grade
2.6.3
2
Mark<50 F
5 1 2
50<=marks<60 C

60<=marks<70 B

70<=marks<80 B+

80<=marks<90 A

90<=marks<=100 A+

ANSWER:
#include <stdio.h>
int main(void)
{
int num;
printf("Enter your mark ");
scanf("%d",&num);
printf(" You entered %d Marks \n", num); // printing outputs
if(num >= 90)
{
printf(" You got A+ grade \n"); // printing outputs
}
else if ( num >=80)
{
printf(" You got A grade \n");
}
else if ( num >=70)
{
printf(" You got B+ grade \n");
}
else if ( num >=60)
{
printf(" You got B grade \n");
}
else if ( num >=50)
{
printf(" You got C grade \n");
}
else if ( num < 50)
{
printf(" You Failed in this exam \n");
printf(" Better Luck Next Time \n");
}
return 0;
}

OUTPUT:
Enter your mark 56
You entered 56 Marks
You got C grade

(OR)
6 Mr. John is a trainee at XYZ Software company and he has
been assigned with the following tasks to find the solutions.
Help him to find the output from the following program.

#include<stdio.h>
void main ( )
{
int num[26], temp ; 3.5.1
num[0] = 100 ; 3
5 3 2
num[25] = 200 ;
temp = num[25] ;
num[25] = num[0] ;
num[0] = temp ;
printf ( "\n %d %d", num[0], num[25] ) ;
}

OUTPUT

200 100

7 A class of 10 students appeared 5 subjects in annual


examination. Write a program to read the marks obtained by
each student in various subjects and print the total marks scored
by each of them.
2.6.3
HINT:
5 1 2 2
Consider the inputs as an array of students and subjects.

ANSWER:
#include<stdio.h>
#include<conio.h>
#define FIRST 360
# define SECOND 240
void main()
{
int n, m, i, j, roll number, marks, total;
printf(“Enter number of students and subjects”);
scanf(“%d%d”, &n,&m);
printf(“\n”);
for(i=1; i<=n; ++i)
{
printf(“Enter roll number:”);
scanf(“%d”, &roll_number);
total=0;
printf(“Enter marks of %d subjects for ROLL NO”, m, roll
number);
for(j=1; j<=m; j++)
{
scanf(“%d”, &marks);
total=total+marks;
}
printf(“TOTAL MARKS =%d”, total);
}
}

Output:
======
Enter number of students and subjects
36
Enter roll_number: 8701
Enter marks of 6 subjects for ROLL NO 8701
81 75 83 45 61 59
TOTAL MARKS =404 (First division)
Enter roll_number:8702
Enter marks of 6 subjects for ROLL NO 8702
51 49 55 47 65 41
TOTAL MARKS =308(Second division)
Enter roll_number: 8704
40 19 31 47 39 25
8 TOTAL MARKS=201
Help John to point out the errors, if any, in the following
program segment:

/* mixed has some char and some int values */


int char mixed[100] ;
main( )
{
int a[10], i ;
for ( i = 1 ; i <= 10 ; i++ )
{
scanf ( "%d", a[i] ) ; 3.6.1
printf ( "%d", a[i] ) ; 3
} 5 3 2
}

OUTPUT:
Error:
1. int followed by char is illegal
2. No ‘&’ sign in scanf function
9
After summer vacation, the University is reopening the college
for UG and PG courses. In the first day and first hour, the B.
Tech students are having Computer Programming class. As
soon as the Professor entered the class, he saw that students
were sitting in random order. So, he decided to arrange the
student in his class in ascending order (Height). Assist the
Professor to arrange the student in ascending order based on the
height of the students. Write a program for the above given
scenario.

2.6.3
HINT:
2
INPUT ARRAY: {123,112,130,145,162}
5 2 2
ANSWER:
#include <stdio.h>
void main()
{
int arr1[10];
int n, i, j, tmp;
printf("\n\nsort elements of array in ascending order :\n");
printf("----------------------------------------------\n");
printf("Input the size of array : ");
scanf("%d", &n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr1[i] < arr1[j])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
printf("\nElements of array is sorted in descending order:\n");

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

printf("%d ", arr1[i]);

printf("\n\n");

}
10

Write a program in C to read ‘n’ number of values in an array


and display it in reverse order.

Test Data :
Input the number of elements to store in the array :3
Input 3 number of elements in the array :
element - 0 : 2
element - 1 : 5
element - 2 : 7 2.6.3
2
Expected Output :
The values store into the array are :
5 1 2
257
The values store into the array in reverse are :
752

ANSWER:
#include <stdio.h>
void main()
{
int i,n,a[100];

printf("\n\nRead n number of values in an array and display


it in reverse order:\n");
printf("--------------------------------------------------------------
----------\n");

printf("Input the number of elements to store in the array :");


scanf("%d",&n);

printf("Input %d number of elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}

printf("\nThe values store into the array are : \n");


for(i=0;i<n;i++)
{
printf("% 5d",a[i]);
}

printf("\n\nThe values store into the array in reverse are :\n");


for(i=n-1;i>=0;i--)
{
printf("% 5d",a[i]);
}
printf("\n\n");
}

Part – B
(1 x 25 = 25 Marks)
Instructions: This section has only ONE question with internal choice.
11 Mr. Adam is a software programmer at Orange Systems. He
wanted to assess the trainee's skillset in C language. Hence, he
has assigned some tasks to the trainees to complete in the
allotted time. Assist them in completing the tasks listed below.
3 3.6.1
Consider the fragment. 5 3 3
char str[] = “ I am Gr8DON”;
str[4] = ‘\0’;
printf(“%s”, str);
What is printed? Justify the answer.
ANSWER:
OUTPUT:
I am
12 Adam is willing to play with C strings. He would like to find
the length of a given string without using string functions. 5 2 3 2 2.6.3
Assist him to complete the task.

Input: “Programming for Problem Solving”

ANSWER:
#include <stdio.h>
#include <string.h>

void main()
{
char str1[50] = “Programming for Problem Solving”;
int i, l = 0;

for (i = 0; str1[i] != '\0'; i++)


{
l++;
}
printf("The string contains %d number of characters. \n",l);
printf("So, the length of the string %s is : %d\n\n", str1, l);
} 2.6.3
Justification: When null character ‘\0’ is reached, the printing 5 2 3 2
of the string is terminated.

13 Mr. Adam is interested in separating the individual characters


with a blank from the input string. Help him to complete the
task in the given time.

ANSWER:
#include <stdio.h>
#include <stdlib.h>

void main()
{
char str[100]; /* Declares a string of size 100 */
int l= 0;

printf("\n\nSeparate the individual characters from a string


:\n");
printf("------------------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);
printf("The characters of the string are : \n");
while(str[i]!='\0')
{
printf("%c ", str[l]);
i++;
}
printf("\n");
5 1 3 2.6.3
}
2
14 Mr. Bob is learning C. He wants to learn how strings are
declared and initialized. Help him by explaining any four string
manipulation functions with examples.

ANSWER:
STRINGS
A String in C is nothing but a collection of characters in a
linear sequence. ‘C’ always treats a string a single data even
though it contains whitespaces. A single character is defined
using single quote representation. A string is represented using
double quote marks.
Example, "Welcome to the world of programming!"

C String is a simple array with char as a data type. ‘C’


language does not directly support string as a data type. Hence,
to display a String in C, you need to make use of a character
array.

The general syntax for declaring a variable as a String in C is as


follows,
char string_variable_name [array_size];
The classic Declaration of strings can be done as follow:

char string_name[string_length] = "string";


The size of an array must be defined while declaring a C String
variable because it is used to calculate how many characters are
going to be stored inside the string variable in C. Some valid
examples of string declaration are as follows,
2.6.3
5 2 3
char first_name[15]; //declaration of a string variable 2
char last_name[15];
Any four string manipulation functions may be explained.

Sushan is working on strings. He has got the numbers as a


string which is given below as an input. His task is to convert
the numbers in the form of string and convert the same to
15
integer. Assist him to complete the given task.

Input: 155342

ANSWER:
#include<stdio.h>
#include <stdlib.h>
int main() {
// Converting a numeric string
char str[10] = "155342";
int x = atoi(str);
printf("Converting '155342': %d\n", x);

// Converting an alphanumeric string


char str2[10] = "Hello!";
x = atoi(str2);
printf("Converting 'Hello!': %d\n", x);

// Converting a partial string


char str3[10] = "99Hello!";
x = atoi(str3);
printf("Converting '99Hello!': %d\n", x);
return 0;
} 1.6.1
5 1 4 1
(OR)

Find the types of pointer used for the given cases and explain
a) Consider a scenario where a person living in country
16
A decides to move country B for his/her vacations,
where certain services like YouTube are not
accessible. Now, whenever he/she tries to hit
www.youtube.com, they would receive some HTTP
errors, which would mean that there is no pointer or
route available for www.youtube.com at that location.

b) Which type of pointer is the most conventional way of


storing raw addresses in C programming?

ANSWER:
Dangling Pointers
The pointers pointing to a deallocated memory block or
unallocated memory block are known as Dangling Pointers.
This condition generates an error known as Dangling Pointer
Problem. Dangling Pointer occurs when a pointer pointing to a
variable goes out of scope or when an object/variable's memory
contains unitialized values or point to deallocated memory.

Void Pointers

The void pointer in C is a pointer that is not associated with any


data types. It points to some data location in the storage. It is
also called the general-purpose pointer. It may be explicitly
type-casted as another type.

Syntax

void *name_of_pointer;
Here, the void keyword acts as the pointer type, and it is 2.6.3
followed by the pointer name- to which the pointer type points
and allocates the address location in the code. The declaration 2
5 2 4
of a pointer happens with the name and type of pointer that
supports any given data type. Let us take a look at an example
to understand this better.
void *ptr
Here, the pointer is expecting a void type- not int, float, etc. It
is pointed by the ptr pointer here that includes the * symbol. It
denotes that this pointer has been declared, and it will be used
for dereferencing in the future.

Mr. Charles is interested in C pointer concepts and he wanted


to count the number of vowels in a string using a pointer. Help
17 him to complete the task.

ANSWER:
#include <stdio.h>
int main()
{
char str[100];
char *ptr;
int cntV,cntC;

printf("Enter a string: ");


gets(str);

//assign address of str to ptr


ptr=str;

cntV=cntC=0;
while(*ptr!='\0')
{
if(*ptr=='A' ||*ptr=='E' ||*ptr=='I' ||*ptr=='O' ||*ptr=='U'
||*ptr=='a' ||*ptr=='e' ||*ptr=='i' ||*ptr=='o' ||*ptr=='u') 3.6.2
cntV++;
else 5 3 4 3
cntC++;
//increase the pointer, to point next character
ptr++;
}

printf("Total number of VOWELS: %d, CONSONANT:


%d\n",cntV,cntC);
return 0;
}

Mr. Charles wants to find the output of the following code 3.6.2
snippet. 3
1. int x = 5, y = 15; 5 3 4
18 2. int * p1, * p2;
3. p1 = &x;
4. p2 = &y;
5. *p1 = 10;
6. *p2 = *p1;
7. p1 = p2;
8. *p1 = 20;
9. printf("%d %d",x,y);

OUTPUT:
10 20
EXPLANATION:

In line 5, *p1 = 10; so the value of variable x is changed to 10.

Krishna is very fond of natural numbers. He wants to display


19 all the natural numbers in between 1 to 100 using a function.
Your task is to help him to display the natural number in 3.6.2
between 1 to 100 iteratively.
ANSWER: 3
5 1 4
#include<stdio.h>
int number(int val) {
if(val<=100) {
printf("%d\t",val);
number(val+1);
}
}
int main() {
int val = 1;
number(val);
return 0;
}

Mr. Charles is interested in C function concepts and he wanted


20
to find out the maximum and minimum of some values using a
function which will return an array.
Test Data :

Input 5 values

25 11 35 65 20

Expected Output :

Number of values you want to input: Input 5 values

Minimum value is: 11


Maximum value is: 65
ANSWER:
#include <stdio.h>
/* Function declarations */
int max(int num1, int num2);
int min(int num1, int num2);
int main()
{
int num1, num2, maximum, minimum;

/* Input two numbers from user */


printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);

maximum = max(num1, num2); // Call maximum function


minimum = min(num1, num2); // Call minimum
function

printf("\nMaximum = %d\n", maximum);


printf("Minimum = %d", minimum);

return 0;
}
/*** Find maximum between two numbers.*/
int max(int num1, int num2)
{
return (num1 > num2 ) ? num1 : num2;
}
/*** Find minimum between two numbers.*/
int min(int num1, int num2)
{
return (num1 > num2 ) ? num2 : num1;
}

*Performance Indicators are available separately for Computer Science and Engineering in AICTE
examination reforms policy.

Course Outcome (CO) and Bloom’s level (BL) Coverage in Questions

Approved by the Audit Professor/Course Coordinator

You might also like