Midterm 1 with solutions

1. (25 points) Write a program that reads a text from the keyboard up to a new line. The program will then count the number of punctuation marks (``.'', ``,'', ``;'', ``:'', ``!'', ``?'') found in the text.

Sample run:

```   It was a dark and stormy night, the wind was blowing and the children were sca
red... Bang ! What was that ? The loud noise was soon explained: a tree had fa
llen over.

Number of '.' : 4
Number of ',' : 1
Number of ';' : 0
Number of ':' : 1
Number of '!' : 1
Number of '?' : 1
```
Notes and hints:
• The input is terminated by a new line, so you have to read 1 character at a time up to '\n'.
• The marks you are looking for are 6 distinct and finite choices.

```   #include <stdio.h>

int main()
{
int numPer = 0, numCom = 0, numSem = 0, numCol = 0, numExc = 0, numQue = 0;
char letter;

/* Read one letter at a time and stop at new line */

while( (letter = getchar()) != '\n' )
{
switch( letter )
{
case '.':
numPer++;
break;

case ',':
numCom++;
break;

case ';':
numSem++;
break;

case ':':
numCol++;
break;

case '!':
numExc++;
break;

case '?':
numQue++;
break;
}
}

printf("\n");
printf("Number of '.' : %d\n", numPer);
printf("Number of ',' : %d\n", numCom);
printf("Number of ';' : %d\n", numSem);
printf("Number of ':' : %d\n", numCol);
printf("Number of '!' : %d\n", numExc);
printf("Number of '?' : %d\n", numQue);

return 0;
}
```

2. (25 points) We want to write a mortgage calculator. It will simply display month by month what is the outstanding balance on the mortgage at the end of each month for the first year. The inputs are:

1. The amount of the mortgage
2. The annual percentage rate (ex: 7.75)
3. The monthly payment
Sample run:
```   Mortage amount   \$ 75000
APR              % 7.75
Monthly payment  \$ 555.55

1: \$ 74928.82
2: \$ 74857.18
3: \$ 74785.08
4: \$ 74712.51
5: \$ 74639.47
6: \$ 74565.96
7: \$ 74491.98
8: \$ 74417.52
9: \$ 74342.58
10: \$ 74267.16
11: \$ 74191.24
12: \$ 74114.84
```
Notes and hints:
• The outstanding balance at the end of each month is given by
balance + (balance * monthlyrate) - payment
• Since the rate is given as the yearly percentage rate and you need the monthly fractional rate, you will have to convert the input rate (ex: APR of 12, means 0.01 per month).
• You are dealing with dollars and cents, so the output will have to be formatted to display only 2 digits of precision.

```   #include <stdio.h>

int main()
{
int month;
float balance, rate, payment;

printf("Mortage amount   \$ ");
scanf("%f", &balance);

/* Escape the percentage sign (it's reserved) */
printf("APR              %% ");

scanf("%f", &rate);
/* Change APR to fractional monthly rate */
rate = 1 + (rate / 1200);

printf("Monthly payment  \$ ");
scanf("%f", &payment);

/* Go through first 12 months */

for(month = 1; month <= 12; ++month)
{
/* Update balance and print */

balance = balance * rate - payment;
printf("%2d: %.2f\n", month, balance);
}

return 0;
}
```

3. (20 points) What does the following code produce ?

```   #include <stdio.h>
int main()
{
int i1 = 0, i2 = 0, i3 = 0;

while( i1 < 2 )
{
for(i2 = i1; i2 < 5; i2++)
for( i3 = 9; i3 > i2; i3--)
printf("%c", '0'+i3);
printf("\n");
++i1;
}
printf("All done !!!\n");
return 0;
}
```

```   98765432198765432987654398765498765
98765432987654398765498765
ALL done !!!
```

4. (15 points) The following code contains at least 5 syntax errors. Identify the line number and the error.

``` 1. #include <stdio.h>
2. int main();
3. {
4.     int row; col;
5.     char c;
6.     for(row = 0; row < limit; row++)
7.         for(col = 0, col < limit; col++)
8.             scanf("%c", c);
9.     return 0;
10. }
```
1. Line: 2__    Error: should not have (";") after "main()"
2. Line: 3__    Error: separate variable names with commas (",") not semicolons (";").
3. Line: 6__    Error: limit not declared.
4. Line: 7__    Error: semicolon (";") needed after initialization.
5. Line: 8__    Error: ampersand ("&") needed before variable parameters in scanf.

5. (15 points)

 i. What is the value of the following expression ? 4 (12 - 3) / (17 % 6) * (8 / 2) [ 9 / 5 * 4 = 1 * 4 ] ii. How many times does the following loop execute ? 4 for(i = 0; i < 10; i += 3) [ i = 0, 3, 6, 9 ] iii. How many times does the following loop execute ? Infinite ``` int count = 1, limit = 1; while( count = limit ) count++;``` [ "count = limit" resets count to 1 each time ] iV. What is the final value of index at the end of the following loop ? 1 for(index = 10; index > 2; index -= 3) ; [ index = 10, 7, 4, 1 ] v. What is the final value of total at the end of the following code fragment ? 48 ``` total = 0; for(i = 0; i < 4; i++) for(j = 0; j < 3; j++) for(k = 0; k < 2; k++) total += 2; ``` [ i 4 times, j 3 times, k twice = 4*3*2 * 2 = 48 ]