1. (10 points)
 i. Given: int a = 5, b = 8; What is the value of the following expression ? false (((a == b) || (b <= 8)) && (a > b)) (a > b) is false, therefore the whole expression is false ii. Given: int a = 10, b = 1; How many times does the following loop execute ? while( a > 4 ) { a -= b; b++; 3 } The value of a will be: 10, 9, 7, 4 iii. What error, if any, is present in the following code segment ? int answer; it should be scanf("%d", &answer); scanf('%d', &answer); Double quotes, not single. iv. What is the output of the following program ? #include int value = 1; void Dumm(); int main() { Dumm(); printf("%d \n", value); } void Dumm() { 2 1 int value = 2; The value printed in main is the value printf("%d ", value); decalred in global scope } The value printed in Dumm has local scope. v. Given the function: int Double(int value) { return value * 2; } What is the value of the following expression ? 6 15 / Double(2) + 15 % Double(3) This is equivalent to 15 / 4 + 15 % 6 = 3 + 3 = 6

2. (5 points) What does this code produce?

```   #include <stdio.h>

int main()
{
int index = 0, index2 = 0;

for(index = 0; index < 3; index++)
{
index2 = index * 3;
while( index2 > index )
{
printf("%d ", index2);
index2 -= 2;
}
printf("%d\n", index);
}

return 0;
}
```

```   0
3 1
6 4 2
```
Create a table for the two indices and the resulting boolean expression:
indexindex2index2 > index
00false
13true
11false
26true
24true
22false

3. (15 points) Write a program that will read two integers, first and second, from the keyboard and then will compute the sum of all the numbers included in the range [ first second] which are multiples of \$5\$. first and second are included in the range.

Sample run:

```   Enter first number: 10
Enter second number: 44

```
Explanation: 10+15+20+25+30+35+40 = 175

Sample run:

```   Enter first number: 16
Enter second number: -8

```
Explanation: -5+0+5+10+15 = 25

Notes:

• The Explanations above are not part of the output of the program.
• You don't know in advance in what order the two numbers are given (you don't know if the first value is smaller than the second or not).

```   #include <stdio.h>

int main()
{
int first = 0, second = 0, start = 0, finish = 0, loop = 0, sum = 0;

printf("Enter first number: ");
scanf("%d", &first);

printf("Enter second number: ");
scanf("%d", &second);

if( first < second )
{
start = first;
finish = second;
}
else
{
start = second;
finish = first;
}

for(loop = start; loop <= finish; loop++)
{
if( loop % 5 == 0 )
{
sum += loop;
}
}

return 0;
}
```

4. (15 points) In a game of poker you decide to use the following strategy: you start off with a certain amount of money, in the first round you place a bet and in each round after that you bet \$1 more than in the previous round. Write a program which will ask the user for the amount of money he starts out with and the bet for the first round. The program should calculate and display how many rounds the player can play before he spends all his money assuming he loses in every round.

Sample run:

```   Initial amount:  13
Initial bet: 2

You will have to stop after 3 rounds.
```
Explanation: 2 + 3 + 4 <= 13 and 2 + 3 + 4 + 5 > 13.

Note: The Explanation above is not part of the output of the program.

```   #include <stdio.h>

int main()
{
int amount = 0, bet = 0, rounds = 0;

printf("Initial amount: ");
scanf("%d", &amount);

printf("Initial bet: ");
scanf("%d", &bet);

while( bet <= amount )
{
amount -= bet;
bet++;
rounds++;
}

printf("\nYou will have to stop after %d rounds.\n", rounds);

return 0;
}
```

5. (15 points) A common method used to "encrypt" messages is to change each letter in the message with the letter that comes 13 places after it in the alphabet. The alphabet is organized in a circular way: the 'z' is followed by the 'a' so that if the resulting letter is bigger than 'z' we resume the count from 'a'.

For example: the letter 'a' will be changed to 'n' and 't' will be changed to 'g'.

Write a function called rot13 which has as it's only argument a character and, if the character is a letter ( upper case or lower case), rot13 will return the letter that comes 13 places after it in the alphabet. If the character is not a letter, rot13 returns the original character.

Notes and Hints:

• You are writing a function which does not include any input or output.
• How do you determine if the new character is bigger than 'z' ? How do you determine by how many characters ? How do you make the count resume from 'a' ? You don't need the ASCII table for this.

```   char rot13(char symb)
{
if( (symb >= 'A') && (symb <= 'Z') )
{
symb += 13;
if( symb > 'Z' )
{
symb = symb - 'Z' + 'A' - 1;
}
}
else if( (symb >= 'a') && (symb <= 'z') )
{
symb += 13;
if( symb > 'z' )
{
symb = symb - 'z' + 'a' - 1;
}
}

return symb;
}
```

6. (20 points) Write a program that reads characters from the keyboard up to, but not including, the new line character (the new line character in C is indicated by '\n'). The program will count the number of times each different character has been entered and then display the results.

Sample run:

```   This is a sample run produced by program 6.

Symbol       Occurences

T            1
h            1
i            2
s            3
8
a            3
m            2
p            3
l            1
e            2
r            4
u            2
n            1
o            2
d            2
c            1
b            1
y            1
g            1
6            1
.            1
```

In order to do this, you will have to set up two arrays: the first, called symbols, is a character array where you will store each new character that you have encountered; the second, called count, is an integer array where you will store the number of occurrences for the corresponding character. Both arrays are of size 128 (the number of entries in the ASCII table). After reading each character, you will have to loop through the characters you have stored so far in the symbols array and if you have already encountered that character, you update the corresponding item in count; otherwise you add a new entry to the symbols array and initialize the corresponding entry in count to 1. Of course this means that you have to keep track of how many items you have stored in the symbols array so far.

```   #include <stdio.h>

main()
{
int count[128], numSymbols = 0, index, found;
char symbols[128], letter;

while( (letter = getchar()) != '\n' )
{
found = 0;
for(index = 0; index < numSymbols; index++)
{
if( symbols[index] == letter )
{
found = 1;
count[index]++;
break;
}
}

if( ! found )
{
symbols[numSymbols] = letter;
count[numSymbols] = 1;
numSymbols++;
}
}

printf("\n\nSymbol\tOccurences\n\n");

for(index = 0; index < numSymbols; index++)
{
printf("%c\t%d\n", symbols[index], count[index]);
}
}
```

7. (20 points) Write a function called SwapEntries which takes three arguments:

1. char data[]: an array of characters of unknown size terminated by the NULL character.
2. char firstItem: a character indicating the first item to be swapped.
3. char secondItem: a character indicating the second item to be swapped.
The function will search data for the two items. If both items are found in the array, their positions are swapped and the function returns a 1, otherwise the array remains as it was and the function returns a 0.

Notes:

• You can assume that the two items are unique within the array, that is if the items exist, they occur only once. For example: if the first item is 'a' and the second item is 'b', the data will contain at most one 'a' and at most one 'b'.
• You are writing a function which does not include any input or output.

```   int SwapEntries(char data[], char firstLetter, char secondLetter)
{
int success = 0, index = 0, firstIndex = -1, secondIndex = -1;
char temp;

while( data[index] != '\n' )
{
if( data[index] == firstLetter )
firstIndex = index;
else if( data[index] == secondLetter )
secondIndex = index;

index++;
}

if( (firstIndex != -1) && (secondIndex != -1) )
{
success = 1;
temp = data[firstIndex];
data[firstIndex] = data[secondIndex];
data[secondIndex] = temp;
}
else
{
success = 0;
}

return success;
}
```