Vous êtes sur la page 1sur 7

Name: Sr. No.

Pakistan Institute of Engineering & Applied Sciences

BS ME
st
1 Sessional Exam.
Computer Fundamentals and Programming (CIS-101)
March 15, 2019
Instructions:
(1) Please write your Name and Sr. No. on the top of the question paper.
(2) The use of mobile for any purpose is not allowed.
(3) Solve Question No. 6 on the space provided on the question paper only.
(4) Exchange of any material (as calculator, eraser, ball points etc.) is not allowed.

Total Marks = 100 Time Duration: 90 minutes

Q. 1 Consider a wire of certain length and cross-sectional area. When this wire is stretched by [10]
applying a force, its length increases. Write a COMPLETE and an Efficient C Program
that should take the values of length (in meters) and cross-sectional area (in m2) of the
wire, the force (in N) and the resulting stretch (in mm) in the wire. Your code should
calculate and display in appropriate forms, the values of stress, tensile strain, and Young’s
Modulus of the wire.
Q. 2 Consider a quadratic equation ax2 + bx + c = 0, where a, b and c are real numbers and a ≠ [10]
0. Write a COMPLETE and an EFFICIENT C program that should accept the coefficients
a, b and c from the user. If the entered co-efficient a is 0, continue to ask the user to enter
a non-zero co-efficient till the correct entry. Your code then should calculate and display
the nature and the values of the roots (both real and complex roots depending upon the
discriminant).
Note: The term b2-4ac is known as the discriminant of a quadratic equation. The
discriminant tells the nature of the roots (i.e. distinct roots, or equal roots, or complex
roots). You can use sqrt function of math.h
Q. 3 Write a COMPLETE and an EFFICIENT C-program in which take the values of two [15]
matrices mat1 (of order 5x4) and mat2 (of order 4x7). These matrices may contain
decimal values. Your code should multiply these two matrices and store the result in mat3
and display it. Your code should also calculate the transpose of mat3 and store it in mat4
and display it.
Q. 4 Consider the series 95/(2*53) + 93/ (7*49) + 91/ (12*45) + … [10]
Write a COMPLETE and an EFFICIENT C program to print the above series up to N
terms, the value of N should be acquired from the user (maximum allowed value of N is
10). If the entered value is less than 1 or greater than 10, continue to ask the user to enter
the allowed value of N till the correct entry. On having the correct input, your code should
print the series upto N terms using only ONE for loop (or any other type). Your code
should also display the net value of the sum.
Sample output: Suppose the user enters the value of N as 3. Your code should display
outputs as:
95/(2*53) + 93/(7*49) + 91/(12*45)
Sum =1.335882
Q. 5 Consider two sets A and B as:
A = {2, 7, 4, 6, 5, 9, 8, 3} and B = {10, 5, 7, 13, 6,9} [10]
Write a COMPLETE and an EFFICIENT C program that should determine the
intersection of these two sets and place the result in a set named C. Finally display the
intersection set C in appropriate form.
Question No 6. (45 Marks)
In the following C programs are provided. They have been tested carefully and run successfully.
Please write the outputs in the “Outputs Column” that would be displayed on the screen.
//PART A: 30 Marks Outputs t = -5; Outputs
#include <stdio.h> do
int main() { t = t+2; } while (t<-3);
{int a = 7, t, x = -6, y, z; printf("out8= %d\n", t); out8=
int A[6] = {3,0,7,1,5,4}; x = 9;
int B[9] = a = x < 10 ? 5 : 8; out9=
{10,12,14,16,18,20,22,24,26}; out1= printf ("out9= %d\n",a);
printf("out1= %d\n", x-2);
/* It is a testing program// t = 9;
t+= t+1;
a = x; */ out2= printf("out10= %d\n", t); out10=
printf("out2= %d\n", a);
a = 13;
x = 4; while (1)
y = --x; {
z = x--; t = ++a;
x--; out3= if (a<15) continue;
printf("out3= %d\n", z); t = ++a;
if (t>15) break;
y = -8; }
x = 6 + (y = y - 7); out4= printf("out11= %d\n", t); out11=
printf("out4= %d\n", x++);
y = 12;
x = 7; a = y + 1;
y = 23; printf("out12= %d\n", a); out12=
z = 17;
if(z < 9) x = 15; y = 7; z = -6;
x = 17; if(x!=4 || y == 2 || z > -10)
y = 20; out5= z = z - x -y ;
printf("out5= %d\n", x); printf("out13= %d\n", z); out13=
out6= t = B[A[4]];
printf("out6= %d\n", y); printf("out14= %d\n", t); out14=
a = 4; t = A[ B[4]%4];
if (a!=a) printf("out15= %d\n", t); out15=
a = 200; out7= getchar(); return 0;
printf("out7= %d\n", a); }
// Continued on the next column.
//PART B: 15 Marks Outputs
#include<stdio.h>
int main()
{
int i = 4, j, k;

for(j = 1, k = 2; ;j*=2)
{
if((i+j % 7 == 0) || (j>18))
{ break; }
if((i+j) % 2 != 0)
{ i++; continue; }
k=i+j;
printf("%d\n",i);
printf("%d\n",j);
printf("%d\n",k);
}
printf("%d\n",(i+j+k)%13);
getchar(); return 0; }
Solution Q1:
Stress = Force /Area (N/msquare)
Tensile strain = Change in length / Original length (No unit)
Y = stress / strain ((N/msquare)

Also take care the units (SI Units)

Solution Q2: Quadratic Equation:


#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f",&a, &b, &c);

while(a ==0)
{
printf("The coefficient a can't be zero. Please enter non zero value for coefficient a: ");
scanf("%f",&a);
}

discriminant = b*b-4*a*c;

// condition for real and distinct roots


if (discriminant > 0)
{ root1 = (-b+sqrt(discriminant))/(2*a);
root2 = (-b-sqrt(discriminant))/(2*a);
printf("root1 = %f and root2 = %f",root1 , root2);
}
//condition for real and equal roots
else if (discriminant == 0)
{ root1 = root2 = -b/(2*a);
printf("root1 = root2 = %f;", root1);
}
// if roots are not real
else
{
realPart = -b/(2*a);
imaginaryPart = sqrt(-discriminant)/(2*a);
printf("root1 = %f + %fi and root2 = %f - %fi", realPart, imaginaryPart, realPart,
imaginaryPart);
}
getchar();
getchar(); return 0;
}
Solution Q3: Matrices
#include <stdio.h>
int main()
{ float mat1[5][4], mat2[4][7], mat3[5][7], mat4[7][5], sum = 0.0;
int row, col, k;
printf("Enter the elements of matrix 1: ");

for (row = 0; row < 5; row++)


for (col = 0; col < 4; col++)
{ scanf("%f", &mat1[row][col]); }

printf("Enter the elements of matrix 2: ");

for (row = 0; row < 4; row++)


for (col = 0; col < 7; col++)
{ scanf("%f", &mat2[row][col]); }

printf("Multiplication of two matrices is:\n\n");

for (row = 0; row < 5; row++)


{
for (col= 0; col < 7; col++)
{
for (k=0; k<4; k++)
{ sum = sum + (mat1[row][k]*mat2[k][col]);
}
mat3[row][col] = sum;
printf("%f\t", mat3[row][col]);
sum = 0.0;
}
printf("\n");
}
// Transpose here
for (row = 0; row < 5; row++)
{
for (col= 0; col < 7; col++)
mat4[col][row] = mat3[row][col];
}

// Printing transpose matrix m4


for (row = 0; row < 7 ; row++)
{
for (col= 0; col < 5; col++)
printf("%f\t", mat4[row][col]);
printf("\n");
}
getchar();
return 0;
}
Solution Q4: Series
Consider the series
95/(2x53) + 93/ (7x49) + 91/ (12x45) + …
Write a COMPLETE and an EFFICIENT C program to print the above series up to N terms,
the value of N should be acquired from the user (maximum allowed value of N is 10). If the
entered value is less than 1 or greater than 10, continue to ask the user to enter the allowed
value of N till the correct entry. On having the correct input, your code should print the series
upto N terms using only ONE for loop (or any other type). Your code should also display the
net value of the sum.
Sample output: Suppose the user enters the value of N as 3. Your code should display
outputs as:
95/(2*53) + 93/(7*49) + 91/(12*45)
Sum =1.335882

#include <stdio.h>
int main()
{ int i, j, k, N, count;
float term, sum = 0;

printf("Please enter any value of N from 1 to 10: ");


scanf("%d", &N);

while(N<1 || N>10)
{ printf("\nYou have entered invalid number.\nPlease enter the correct value: ");
scanf("%d", &N);
}

i = 95;
j = 2;
k = 53;
for (count = 1; count <= N; count++)
{
if (count < N)
printf("%d/(%d*%d) + ",i,j,k);
else
printf("%d/(%d*%d)",i,j,k);

term = float(i)/(j*k);
sum = sum + term;
i= i-2;
j = j+5;
k = k-4;
}
printf("\nSum = %f",sum);
getchar(); getchar(); return 0;
}
Solution Q5: Intersection of two sets
#include<stdio.h>
int main()
{

int A[8] = {2, 7, 4, 6, 5, 9, 8, 3};


int B[6] = {10, 5, 7, 13, 6,9};
int C[8];

int i, j, k = 0;

//A = {2, 7, 4, 6, 5, 9, 8, 3} and B = {10, 5, 7, 13, 6,9}

for(i=0;i<7;i++)
{
for(j=0; j<6; j++)
{
if(A[i]==B[j])
{
C[k] = A[i];
k++;
break;
}
}
}

printf("{");

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


{
if (i<(k-1) )
printf("%d, ",C[i]);
else
printf("%d",C[i]);
}

printf("}");

getchar();
return 0;
}