Solution:: Faculty of Engineering
Solution:: Faculty of Engineering
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.
(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:
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);
}
#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()
{
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;
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. (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;
}