EE150 - Fall 1996

Midterm #1 (10/2/96)
Solutions


Jump To Solution For Problem: [1 | 2 | 3 | 4]
------

Problem #1

Part A

The output is:

Main:
A 1 ....... 13
B 3 .......
C 7 .......
P call1=10

DoStuff/call1:
C 1
A 3 
B 7
returns 3 + (7 * 1) = 10

Output:
yy: 3,7,1
xx: 10
xx: 13
xx: 3

Here is the reason why.

dostuff is passed three values: 1, 3, and 7. 1 goes into its parameter c, 3 goes into its parameter a, and 7 goes into its parameter b. dostuff prints these values and computes its return value of 3 + 7 * 1, which is 10. That's returned to the main program, which stores it in p and then prints p, changes the variable a to equal b plus p (3 + 10) or 13, and then prints out a and b

The problem. The code.

Part B

The output is:

No way 48.00
Okay 24.0
Okay 12.0
O vay 1076363264      <- unknown or print double as integer is fine

Here's the reason why.

n is set to 96.0 (floating point) before the loop. The loop then tests whether n is greater than 13.0, which it is, so we go through the loop. This involves dividing n by 2.0, which sets it to 48.0. We then test whether that's less than 35., which it is not, so we write the No way and n's'value (48.0) in a floating point field of 4 with one decimal point or 48.0.

We now do the test again (which verifies that n is greater than 13), so we go through the loop again, which divides n by 2, setting it to 24.0, which is now less than 35, so we write Okay and 24.0 in a field of 5 with two decimal points or 24.00.

We do the test again, which is true again (since n is 24.0, which is greater than 13), so we divide n by 2 which gives us 12.0. Now the if will do the then part, printing Okay and 12.0.

Once again, we do the loop test, which is now false, and integer m is assigned to 12.0 + 1 + 0.51. This expression is evaluated in floating point as 13.51 and truncated to 13 for assignment to the integer. The final printf is now executed printing O vay and a double value 13.0 as an integer - it would be fine to say you didnt know what would happen.

The problem. The code.

Part C

The output is:

b=25.00, c=12, j=7
b=28.00, j=7
lp i=12
lp i=5
dp i=-2

Here is the reason why.

b starts out with an integer cast of 25 following the floating point expression evaluating to 25.94. Since b is a floating point number, it is given the value 25.0. c is assigned the value of 12, and j the value of 7. Next the values of b, c and j are printed. A function call to change with parameter 7 results in an assignment to b of 28.0. once again some values are printed. Finally we encounter the loop which commences with the counter i set to the value of c which is 12. The printf in the loo prints out the value of i the first time which is 12.

As we do the loop again, the value of the counter i is decremented by the value of j which is 7, giving 5. The body of the loop now prints i's value (5), and then we again subtract 7 from i, setting it to -2.

Now when we do the test, it's no longer true that i is non-negative, so we terminate the loop. Finally, the value of i is printed outside the loop as -2.

The problem. The code.

Part D

The output is:

0     <- an unknown value wuld be ok since the value is not initialized
10

q's value is originally set as the value of m but m has not been set yet - basically q is unknown, probably 0. m is assigned a value of 13 and then assigned again a value of 4. Recall that the operator == returns true (1) or false (0), both integers. Evaluating the expression (q == m) works out as 0 since q and m are not equivalent, and thus m is assigned 0 + 10 or 10. Finally, m is printed.

The problem. The code.

------

Problem #2

Part A

The changes are as follows:

/* 
   Sum the two times each of the even numbers between START and FINISH together
   with three times each of the odd numbers between START and FINISH
*/

#include <stdio.h>

#define START  5
#define FINISH 8
#define T1 thrice                          /* 1 defined T1 and T2 backwards */
#define T2 twice

int main()
{
  int i;  
  int sum_even;  
  int sum_odd;
  int total;                               /* 2 forgot to define total */

  int even(int x);
  int twice(int x);                       
  int thrice(int x);                       /* 3 forgot thrice prototype */

  sum_even = 0;   sum_odd = 0;             /* 4 forgot to initialize sums */

  for (i = START; i <= FINISH; i = i + 1)
    {
      if (even(i))
          sum_even = sum_even + T2(i);
      else
          sum_odd  = sum_odd  + T1(i);
    }
  total = sum_even + sum_odd;
  printf("%i\n", total);

  return 0;
}

int even(int x)
{
  double ck;
  ck =  (double) (x / 2.0) - (int) (x / 2);
  if (ck > 0.0) 
    return 0;
  else return 1;
} 

int twice ( int x )
{ 
  return 2 * x; 
}

int thrice ( int x )
{ 
  int twice(int x);
  return twice(x) + x;
}

The problem. The code.

Part B

The changes are as follows:

/* Print powers of 2 forwards from 0 through FINISH followed by the total. That is, for FINISH = 7, output should be: 1 2 4 8 16 32 64 128 = 255 */ #include <stdio.h> #define FINISH 7 main() { int count; /* 1 all variables should be ints */ int p; int total; total = 0; count = 0; p = 1; while (count <= FINISH) /* 2 actually we should check count */ { /* 3 also <<= should be <= */ printf("%i ", p); total = total + p; p = p * 2; /* 4 powers means mult by 2 not add */ count = count + 1; } printf(" = %i\n", total); /* 5 print out the total not count */ return 0; }

The problem. The code.

------

Problem #3

One possible solution is:

#include <stdio.h>

main()
{
  int count_a;
  int count_all;
  int percent_a;
  double score;
  int classify_got_A( double grade );    /* prototype */

  count_a = count_all = 0;
  while (scanf("%lf", &score) == 1)      /* input fl pt grade */
  {
    count_all = count_all + 1;
    if ( classify_got_A(score) )         /* function call */
      count_a = count_a + 1;
  }
  percent_a = (count_a * 100) / count_all; 
  printf("%i%% of the students (%i out of %i) got A's", 
         percent_a, count_a, count_all);

  return 0;
}

/*
  This function returns 0 if the grade is less than 90, 1 if greater than 90
*/
int classify_got_A( double grade )          /* function definition */
{
   if (grade >= 90)
     return 1;
   else return 0;
}

The problem. The code.

------

Problem #4

The first part of Problem 4 should have been straightforward - simply use the function given to do the hard math stuff. You should have put your input reading loop into a function since that is what the question asked for.

The second part of Problem 4 was a pretty tough question. Look up the recursive functions section of the Joy of C text. Does factorial look a lot like Fbig? It is quite possible to write Fbig using a loop (while or for) rather than by calling itself.

#include <stdio.h>

#include 

main()     /* you didn't really need a main program, just the function 
               inputValues for the first part, and just Fbig for the
               second part */
{
  void inputValues();     /* prototype */
  
  inputValues();          /* function call */

  return 0;               /* return something */
}

void inputValues()        /* function definition */
{
  int result; int i; int val;
  int Fbig(int);

  while (scanf("%i", &val) == 1)
  {
    result = Fbig(val);
    printf("Fbig of %i is %i.\n", val, result);
  }  
}

/* straightforward but longer solution */
int Fbig(int i)              /* function definition */
{ 
  int j;
  int sofar;

  sofar = 1; 
  for (j = i; j > 1; j = j - 1)    /* iterate from i, i-1, ... down to 1 
  {
    sofar = sofar * j;             /* keep a multiplying total
  }
 return sofar; 
}

/* ALTERNATE: simple to write but tricky to figure out solution */
int Fbig(int i)              /* function definition */
{
  if (i <= 1)
    return 1;
  else return i * Fbig(i - 1); 
}

The problem. The code.

------

[EE150 Home Page | E150 Exam Information Page | Top Of Page]