Docsity

Computer Programming with Engineering Applications - Midterm Questions | CS 2073, Exams of Computer Science

Toggle sidebar
bg1
1
CS 2073 Computer Programming with Engineering Applications
Spring 2008 – Midterm — March 10, 2008
Name:……………………… Score: ……./20
Sequence number:………….
1. (4 point) Show the output line(s) generated by the following statements (also show how you
perform the computations):
int a = 3, b = 6, c = 4;
c = a + -b / 3 - b / sqrt(b + a) + a / 2 * b;
printf("New value of c = %d \n", c);
Step-by-
step show
how you
compute:
output: New value of c = 5
pf3
pf4
pf5

Partial preview of the text

Download Computer Programming with Engineering Applications - Midterm Questions | CS 2073 and more Exams Computer Science in PDF only on Docsity!

CS 2073 Computer Programming with Engineering Applications

Spring 2008 – Midterm — March 10, 2008

Name:……………………… Score: ……./ Sequence number:………….

  1. (4 point) Show the output line(s) generated by the following statements (also show how you perform the computations):

int a = 3, b = 6, c = 4;

c = a + -b / 3 - b / sqrt(b + a) + a / 2 * b;

printf("New value of c = %d \n", c);

Step-by- step show how you compute:

output: (^) New value of c = 5

Name:………………

  1. (4 points) Suppose a store XYZ is having a “BUY ONE, GET ONE 25% OFF” deal. Suppose every customer gets two items whose original prices are p1 and p2 dollars, respectively. The discount is applied to the lowest-cost item. Tax is 8% and applied after the discount. Complete the following C program that computes and prints the total amount to pay.

#include <stdio.h>

int main(void) { /* Declare variables. If needed, you can declare more*/ double p1, p2, total;

printf("Enter the original prices of item1 and item2:”); scanf("%lf %lf", &p1, &p2);

/* Compute the total amount including 8% tax Note: 25% off discount is applied to the lowest-cost item */

total = p1 + p2;

if( p1 > p2 ) { total = total - 0.25 * p2; } else { total = total - 0.25 * p1; }

total = total + total * 0.08;

printf("Total = %lf \n", total);

system("pause");

/* Exit program. */ return 0; }

if( p1 > p2 ) { total = p1 + 0.75 * p2; } else { total = p2 + 0.75 * p1; }

  1. (4 points) Complete the following C program that reads the value of x and n from the keyboard and then approximately computes the value of cos(x) using the following formula: 2 4 6 2

c o s 1 ( 1)

n

x x^ x^ x^ n x

n

= − + − L + −

#include <stdio.h> #include <math.h> int main(void) { /* Declare variables, If needed, you can declare more*/ int i, n; double x, cosx;

double powx, fact;

printf("Enter the value of x and n : "); scanf("%lf %d",&x, &n);

/* Write a loop to compute cosx using the above formula */

cosx=1.0; fact=1.0; powx=1.0;

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

powx = powx * x * x; / OR powx = pow(x, 2i); /*

fact = fact * (2i-1) * (2i);**

if (i%2==0) cosx = cosx + powx / fact; else cosx = cosx - powx / fact; }

printf("Approximate cosx is %lf when n=%d \n", cosx, n); printf("cos(x) in math lib returns %lf \n”, cos(x));

system("pause"); return 0; /* Exit program. */ }

Name:………………….

  1. (4 points) Suppose we have a data file that contains various input combinations for the below logical circuit. We would like to divide these input combinations into two files such that the combinations resulting in the same z1 and z2 are written into z1z2same.txt and the combinations resulting in different z1 and z2 are written into z1z2different.txt.

For instance, your program should process the following input.txt and generate the corresponding z1z2same.txt and z1z2different.txt as follows:

Here is the logical circuit.

Complete the program in the next page Æ

z

z

input.txt

z1z2same.txt prog

A

B

C

D

E

AND gate

OR gate

NOT gate

A B C D E

1 1 1 0 0 z1z2different.txt

A B C D E