0% found this document useful (0 votes)
18 views7 pages

Solution:: Faculty of Engineering

This document outlines the end-term examination for B. Tech students in the Faculty of Engineering, covering the course CS1001 - Problem Solving using Computers. It includes instructions, a series of programming questions with solutions, and tasks related to C programming concepts such as data types, arrays, structures, and recursion. The exam is closed book, lasts for 2 hours, and consists of multiple questions that students must answer.

Uploaded by

nevaidhyasingh
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)
18 views7 pages

Solution:: Faculty of Engineering

This document outlines the end-term examination for B. Tech students in the Faculty of Engineering, covering the course CS1001 - Problem Solving using Computers. It includes instructions, a series of programming questions with solutions, and tasks related to C programming concepts such as data types, arrays, structures, and recursion. The exam is closed book, lasts for 2 hours, and consists of multiple questions that students must answer.

Uploaded by

nevaidhyasingh
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/ 7

Faculty of Engineering

B. Tech (All Branches)


II Semester [Jan 2023 – May 2023]
End Term Examination : 2022-23
CS1001- Problem Solving using Computers (Solution Set-B)
(CLOSED BOOK)

Duration: 2 Hour Max. Marks: 40

Instructions:
 Answer any Five out of Six Questions. All Questions Carry Equal Marks.
 Please write Name and Registration No. on Question Paper as well as on the first page of Answer Booklet.
 Missing data, if any, may be assumed suitably.
 Calculator NOT allowed.

1. (a) Convert the following numbers to the mentioned base: [1+1+1+1]


Solution:
(i) (523.64)8=(101010011.110100)2 || 0 / ½ /1
(ii) (322)5=(1010111)2 || 0/1
(iii) (203)4=(35)10 || 0/1
(iv) (345)8 + (453)8 = (1020)8 || 0/1

(b) Consider an 8-bit binary number as ‘11101100’. What decimal value it represents in (i) Signed-
Magnitude Form, (ii) 1’s Complement Form and (iii) 2’s Complement Form. [2+1+1]
Solution:

(i) Signed-magnitude Form = -108 || 0/2


(ii) 1’s Complement Form = -19 || 0/1
(iii) 2’s Complement Form = -20 || 0/1

2. (a) Read the following ‘C’ code carefully and predict the output of the questions asked at the end.
[1.5+1+1.5]
#include<stdio.h>
int fun1(int *,int *);
void main()
{
int a=10,b=20,sum,z=5;
int *p,*q;
p=&a,q=&b;
z=fun1(p,q);
printf("x=%d y=%d p=%d q=%d\n",a,b,*p,*q); // Line 1
printf("z=%d\n",z++); // Line 2

}
int fun1(int *a,int *b)
{
*a=*a+*b;
*b=*a+*b;
int sum=*a+*b;
printf("a=%d b=%d sum=%d\n",*a,*b,sum); // Line 3
return sum;
}
Solution:

(i) What will be the output of Line 1. || x=30 y=50 p=30 q=50 || 0.5 for any three answer total
1.5 marks
(ii) What will be the output of Line 2. || z= 80 || 0/1
(iii) What will be the output of Line 3. || a= 30 b=50 sum= 80 || 0.5+0.5+0.5

(b) Write a C program which performs the following tasks sequentially. [1+1+2]
 Declare a string variable named ‘city’ with a capacity of 100.
 Set the value of variable ‘city’ as "Manipal University Jaipur".
 Determine the frequency of the character 'a' in the string and display the count of it.
Solution:
#include <stdio.h>
int main()
{
char city[100]; || 0/1- For Declaration
city =” Manipal University Jaipur” || 0/1 For Initialization
int i, count=0;
for(i=0;city[i];i++) || 0/1- For applying Loop
{
if(city[i]=='a') || 0/1- For Rest of the Code
{
count++;
}
}
printf("character 'a' occurs %d times \n ",count);
}

3. (a) Complete the program as per description given below. [1+1.5+1.5]


#include <stdio.h>
int main ()
{
int A [50],N;
/*
1.Read ‘N’ elements of array ‘A’ from user.
2. Print the Sum of odd numbers of Array ‘A’ and store it as A_odd.
3. Print the sum of even numbers of Array ‘A’. store it as A_even.
*/
}
Solution:

#include <stdio.h>
int main()
{
int A[50],i,N,A_odd=0,A_even=0;
printf("Enter size of the array : ");
scanf("%d",&N); || 0/.5 -Reading Value of N
printf("Enter elements in array : ");
for(i=0; i<N; i++) || 0/.5- Reading Array Elements
{
scanf("%d", &A[i]);
}
for(i=0; i<N; i++)
{
if(A[i]%2==0) || 0/.5/1/1.5 – For Odd Logic
{
A_even=A_even+A[i];
}
else{
A_odd=A_odd+A[i]; || 0/.5/1/1.5 – For Even Logic
}
}
printf(“The sum of odd numbers are: %d”,A_odd);
printf(“\nThe sum of even numbers are: %d”,A_even);
return 0;
3. (b) Define a structure named ‘Employee’ with members as (i) ‘emp_ID’ of type ‘int’ , (ii) ‘emp_Name’
of type ‘String’ with length 20 and (iii) ‘emp_Salary’ of type double. Create two variables named
‘E1’, ‘E2’ of type ‘struct Employee’. (iv) Read the values of all members of ‘E1’ and ‘E2’ from
user and display it on the screen. [.5+.5+.5+2.5]
Solution:

include <stdio.h>

/*structure declaration*/
1. 1/2 for each Structure Member Total 1.5
struct employee{ Marks
char emp_name[20];
int emp_Id; 2. 1.5 marks for Reading Value
double emp_salary;
3. 1 Marks for displaying the Values
};
int main()
{

/*declare structure variable*/


struct employee E1,E2;
/*read employee details*/
printf("\nEnter details :\n");
printf("Name ?:");
gets(E1. emp_name);
gets(E2. emp_name);
printf("ID ?:");
scanf("%d",&E1.emp_Id);
scanf("%d",&E2.emp_Id);
printf("Salary ?:");
scanf("%f",&E1.emp_salary);
scanf("%f",&E2.emp_salary);
/*print employee details*/
printf("\nEntered detail is of Employee-1:");
printf("Name: %s" ,E1.emp_name);
printf("Id: %d" ,E1.emp_Id);
printf("Salary: %f\n",E1.emp_salary);
printf("\nEntered detail is of Employee-2:");
printf("Name: %s" ,E2.emp_name);
printf("Id: %d" ,E2.emp_Id);
printf("Salary: %f\n",E2.emp_salary);
return 0;
}

4. (a) Write a C program that performs the following tasks sequentially. [1+1+2]

#include <stdio.h>
int main()
{
/*
 Declare a variable named ‘temperature’ of type ‘double’. =>0/1
 Read the value of ‘temperature’ from user. => 0/1
 Prints one the following messages based upon the value of ‘temperature’ => .5 for Each Part
(i) Prints “Freezing Weather” if ‘temperature is less than 0
(ii) Prints “Very Cold Weather” if 0 <= temperature <10
(iii) Prints “Cold Weather” if 10 <= temperature <20
(iv) prints “Normal Weather” if temperature >= 20
*/
}// End of Main

Solution:
#include <stdio.h>
void main()
{
double temprature;
printf("Input days temperature : ");
scanf("%f",&temprature);
if(temprature <0)
printf("Freezing weather.\n");
else if(temprature <10)
printf("Very cold weather.\n");
else if(temprature <20)
printf("Cold weather.\n");
else if(temprature <30)
printf("Normal weather\n");
else
printf("Invalid Input\n");
}
4. (b) You have to complete the following function named ‘getSecondSmallest(int Arr[], int N)’. This
function receives two parameters as (i) 1-D array named ‘Arr’ of type int[] and (ii) an ‘int’ variable
named ‘N’ that represents the number of elements of ‘Data’. This function returns the second smallest
element from array ‘Arr’. [2+2]
int getSecondSmallest(int Arr[], int N)
{
/*
This function returns the second smallest element from array ‘Arr’.
Complete the Code of This Function.
*/
}// End of Function
Solution:
#include <stdio.h>
int getSecondSmallest(int Arr[],int N);
void main()
{
int n=10, second_large;
int array[10] = {101, 11, 3, 4, 50, 69, 7, 8, 9, 0};
second_large=getSecondSmallest(array,n);
printf("Second - %d",second_large);
}
Note: Ignore the main function, please mark on the basis of logic of program
int getSecondSmallest(int Arr[], int N)
{
int loop, largest, second; || Declaring variables 0/1
if(Arr[0] > Arr[1]) {
largest = Arr[0];
second = Arr[1];
} else {
largest = Arr[1];
second = Arr[0];
}
for(loop = 2; loop < N; loop++) { ||applying logic 0/1/2
if( largest < Arr[loop] ) {
second = largest;
largest = Arr[loop];
} else if( second < Arr[loop] ) {
second = Arr[loop];
}
}
return second; || Returning Result: 0/1
}

5. (a) Write a recursive function to compute the sum of digits of an integer number “N”. Read value of N from
the user. [2+2]
For example: if N=234 then output should be 2+3+4=9.
Solution:
#include <stdio.h>
int sum (int a);
int main()
{
int num, result;

printf("Enter the number: ");


scanf("%d", &num);
result = sum(num);
printf("Sum of digits in %d is %d\n", num, result);
return 0;
}

int sum (int num)


{
if (num != 0) || Base Condition: 0/1 Marks
{
return (num % 10 + sum (num / 10)); || Writing a recursive statement: 0/1/2
}
else
{
return 0; || Returning 0 with base condition: 0/1
}
}
5. (b) Illustrate the difference between array and structure with suitable example. [1+1+1+1]
|| 1 mark for each difference, any four Difference

Array Structure
Array is the collection of the Homogeneous Structure is the collection of the Heterogeneous
elements elements.
Elements are stored contiguously, and elements are Elements are stored non-contiguously and elements
accessed using index are accessed using dot operator.
i.e Struct emp
{
int Arr[10]; char name[50];
Int emp_id;
}

6. (a) Implement the following function in C [2+2]


int isPalindrome(char name[])
{
/* This method returns 1 if name is a Palindrome string. A palindrome string has equal characters from both
sides. For Example, naman, madam, etc. are all palindrome strings.
}
Solution:
#include <string.h>
int isPalindrome(char name[])
{
int i,c=0,n;
n=strlen(name); || Getting length: 0/1
for(i=0;i<n/2;i++) || Loop: 0/1
{
if(name[i]==name[n-i-1]) || Condition: 0/1
c++;
}
if(c==i)
return 1; || Return 1: 0/.5
else
return 0; || Return 0: 0/.5
}

6. (b) Write a C program that performs the following tasks sequentially. [1+1+2]
(i) Declare a 2-D array named as “Data” of type “int” with size 5x5. || 0/1
(ii) Read the values of 2-D array “Data”. || 0/1
(iii) Display all the elements of 2-D array “Data” which are divisible by 5. || 0/1/2
Solution:
#include<stdio.h>
int main(){
/* 2D array declaration*/
int Data[5][5];
/*Counter variables for the loop*/
int i, j;
for(i=0; i<5; i++) {
for(j=0;j<5;j++) {
printf("Enter value for Data[%d][%d]:", i, j);
scanf("%d", &Data[i][j]);
}
}
//Displaying array elements which are divisible by 5
printf("Two Dimensional array elements:\n");
for(i=0; i<5; i++) {
for(j=0;j<5;j++) {
if(Data[i][j]%5==0)
printf("%d ", Data[i][j]);
}
}
return 0;
}

You might also like