# Midterm 2 with solutions

1. (25 points) Write a program that reads a sentence from the keyboard one character at a time. The sentence is terminated by a period (" ."). The program will print each character translating all lower case characters to upper case and it will count the number of words in the sentence. The end of a word is indicated by a space.

The main function will read each character and then use a helper function, called Process, to handle the character just read. The function Process does the conversion from lower case to upper case, identifies the end of word and prints the character. The function will return a 1 if it is an end of word, otherwise it will return a zero. At the end of the sentence, the main function will print out the total number of words encountered.

Sample run:

```   Enter sentence:
This is a sample run, just to see how it works.
THIS IS A SAMPLE RUN, JUST TO SEE HOW IT WORKS.

Total number of words: 11
```
Notes and hints:
• Which function, main or Process, determines when we've reached the end of the sentence ?
• Write first the prototype for Process, then write main, finally write the Process function.

```   #include <stdio.h>.h

int Process(char letter);

int main()
{
char letter = ' ';
int words = 0;

printf("Enter sentence:\n");

do
{
letter = getchar();

/* If we read at least one character that is not '.' or    */
/* ' ' and we haven't read any words yet, we have at least */
/* one word, so count it now.                              */

if( words == 0 )
if( (letter != '.') && (letter != ' ') )
words = 1;

if( Process( letter ) )
words += 1;

} while( letter != '.' );

printf("\nTotal number of words: %d\n", words);

return 0;
}

int Process(char letter)
{
/* If it's a lower-case letter, make it upper-case */

if( (letter >= 'a') && (letter <= 'z') )
letter -= 32;

printf("%c", letter);

/* If letter was a space, this is an end-of-word */

if( letter == ' ' )
return 1;

return 0;
}
```

2. (20 points) You are part of a team writing a database management program. You're in charge of writing the function that evaluates the commands. This function is called ParseCommand. The function takes one argument, command: a 1-character variable that identifies the current command (see below). The function returns to the caller the number of errors that have been encountered.

For each command below, there is an associated function that executes the command. All of these functions take no arguments and return the number of errors encountered while executing the command. All of these functions have already been written.

List of commands and associated execution function:

 Command Function A or a AddDB() D or d DeleteDB() U or u UpdateDB() L or l ListDB() P or p PrintDB()

Your function must be able to handle both uppercase and lowercase commands. The function must also detect invalid commands. You are writing only the function ParseCommand, no main or any of the other functions.

```   #include <stdio.h>

int DeleteDB();
int UpdateDB();
int ListDB();
int PrintDB();

int ParseCommand(char command)
{
int error = 0;

switch( command )
{
case 'a':
case 'A':
break;

case 'd':
case 'D':
error = DeleteDB();
break;

case 'u':
case 'U':
error = UpdateDB();
break;

case 'l':
case 'L':
error = ListDB();
break;

case 'p':
case 'P':
error = PrintDB();
break;

default:
printf("Error: invalid command %c\n", command);
error = 1;
}

return error;
}
```

3. (25 points) The integer 36 has a peculiar property: it is a perfect square and it is also the sum of the integers from 1 to 8. The next such number is 1225, which is 35^2 as well as the sum of 1 through 49. Write a program that finds the next number that is a perfect square, and is also the sum of the series 1 ... n. The program should output the number and the limits of the summation.

Sample run:

```   41616 is equal to the square of 204 and the sum from 1 to 288.
```
Notes and Hints:
• The function sqrt will return the square root of the argument.
• The function floor will return the integer part of a real number.
• A number is a perfect square iff it is equal to the square of its integer square root; i.e., if X is an integer, then X is a perfect square iff X == floor(sqrt(X)) * floor(sqrt(X)).
• Since you already know that 1225 is equal to the sum of 1 through 49, you should take this as your starting point; i.e, see if the sum of 1 through 50 is a perfect square, etc.

```   #include <stdio.h>
#include <math.h>

/**************************************************************/
/*                                                            */
/* Find the next integer, after 1225, which is both a perfect */
/* square and the sum of the series 1...n.                    */
/*                                                            */
/**************************************************************/

int main()
{

int summation = 1225;
int nextNumber = 0;

/* We know that 1225 = 1+2+...+49, so we start from 50 */

for(nextNumber = 50; nextNumber; nextNumber++)
{
/* Add the next number to the summation and verify */
/* if this is a perfect square */

summation += nextNumber;
if( summation == (int )(floor(sqrt(summation)) * floor(sqrt(summation))) )
{
printf("%d is equal to the square of %d and the sum from 1 to %d\n",
summation, (int )(sqrt(summation)), nextNumber);
break;
}
}

return 0;
}
```

4. (15 points) The following code contains at least 5 errors (syntax and logic). Comments, formatting or lack thereof are not any of the errors. Identify the line number and the error.

``` 1. #include <stdio.h>
2. void Print(int length, char letter);
3. int GetLength();

4. int main()
5. {
6.     int length = 0, index = 0, status = 0;
7.     length = GetLength();
8.     status = Print(length);
9.     for(index = 1; index < length; index++)
10.         if( index <= length/2 );
11.             Print(length-index, 'X');
12.         else
13.             Print(index, '*');
14.     return 0;
15. }

16. void Print(int length, char letter);
17. {
18.     int index = 0;
19.     for(index = 0; index < length; index++)
20.         printf("%c", letter);
21.     printf("\n");
22. }

23. int GetLength()
24. {
25.     printf("Enter length: ");
26.     scanf("%d", &length);
27.     return length;
28. }
```
1. Line 8:   __   Error: function Print is of type void and cannot be assigned to status.
2. Line 8:   __   Error: function Print takes two arguments.
3. Line 10: __   Error: semicolon at the end of line becomes body of if making the else on line 12 a dangling else
4. Line 16: __   Error: semicolon at the end of line should not be there.
5. Line 26: __   Error: lenght not decalred in function GetLength

5. (15 points) What is the output of the following program ?

```   #include

int Up(int n)
{
return 3*n + 1;
}

int Down(int n)
{
return n / 2;
}

int Disp(int n)
{
if( n % 2 )
n = Up(n);
else
n = Down(n);
return n;
}

int main()
{
int num = 3;
while( num != 1 )
{
printf("%d ", num);
num = Disp(num);
}
printf("1\n");
return 0;
}

```

```      3 10 5 16 8 4 2 1