Final V22.0002

1. (10 points) Reminder: in C the value 0 is false, everything else is true.

 i. How many times will the following for loop execute ? for(ind = -1; ind < 7; ind *= 2); Infinite ind starts at -1, then becomes -2, -3, etc. ii. How many times will the following while loop execute ? int ind = 32; while( ind % 3 ) ind /= 3; 2 Execute as long as (ind % 3) is not 0. ind will be: 32, 10, 3. iii. What is the value of the following expression (true/false) ? ((0 && 0) || (1 && (0 || 1))) True iv. Given: int t = 4; What is the value of the following expression (true/false) ? ((t = 5) || (t = 6)) True The single = sign is assignment, left side is 5 and right side is 6 v. Given: int f = 43, g = 5; What will be the output of the following printf statement ? printf("%d\n", (f % g) + (f / g)); 11 (43 % 5) is 3, (43 / 5) is 8

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

```   int main()
{
int i = 0, j = 0 ;
for ( i = 4  ; i >= j ; i-- )
for ( j = 0;  j <= 3; j++ )
printf("%d", i + j );
printf("\n");
return 0;
}
```

```   4567

At the end of the first time through the inner loop, "j" will be 4,
therfore the outer loop stops.
```

3. (15 points) Write a program that reads an integer N from the keyboard and then computes and displays the sum of the numbers from N to (2 * N) if N is nonnegative, or the sum of the numbers from (2 * N) to N if N is negative. The starting and ending points are included in the sum.

Sample run:

```   Enter number : 5
Sum : 45
```
Sample run:
```   Enter number : -4
Sum : -30
```
Notes and hints:
• You should have only 1 loop to compute the sum; use the sign of N to determine the start point and the end point.

```   #include <stdio.h>

int main()
{
int N = 0, start = 0, end = 0, sum = 0, loop = 0;

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

if( N < 0 )
{
start = 2 * N;
end = N;
}
else
{
start = N;
end = 2 * N;
}

for(loop = start; loop <= end; loop++)
{
sum += loop;
}
printf("Sum : %d\n", loop);

return 0;
}
```

4. (15 points) A palindrome is a word, verse, or sentence that reads the same backward or forward. For example, "able was I ere I saw elba" is a palindrome, as are "abba", and "a".

Write a function called IsPalindrome that takes one string parameter called str and returns a 1 if str is a palindrome or 0 if it's not a palindrome. The parameter str represents a string ending in a NULL termination character and you do not know in advance the string's length.

• Do not to write a whole program.
• Do not read in a string from the keyboard; the string is passed to the function.
• Do not use any standard library function.
Hints:
• Since the string passed to the function can be any length, you should count the characters in the string (excluding the NULL termination character) before doing any comparisons.
• With a palindrome, if you look at the beginning and end of the string and work your way one at a time from both ends towards the middle, the character from either end is the same at each step.

```   int IsPalindrome(char str[])
{
int len = 0, front = 0, back = 0;

/* Loop through the string 'till you hit the end */

while( str[len] != '\0' )
{
len++;
}

/* Proceed to compare the letters from the front */
/* with the letters from the back. We stop as soon */
/* as we find to letters that differ or the front */
/* passes the back */

back = len-1;

while( (str[front] == str[back]) && (front <= back) )
{
front++;
back--;
}

if( front > back )
{
return 1;
}
else
{
return 0;
}
}
```

5. (15 points) Write a program that reads a positive integer N from the keyboard and outputs all of the prime numbers from 1 up to N. Write and use the function:
int IsPrime(int num);
which returns 1 if num is prime, 0 if it is not.

The main function will prompt for the number N and then it will test all of the numbers from 1 to N inclusive calling IsPrime for each number. If the number is prime it will print it.

Note: A prime number is a number divisible only by itself and 1.

Sample run:

```   Enter number : 9
Prime numbers: 1 2 3 5 7
```

```   #include <stdio.h>

int IsPalindrome(int num);

int main()
{
int N = 0, loop = 0;

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

/* All of them will have 1 ... */

printf("Prime numbers: 1");

for(loop = 2; loop <= N; loop++)
{
if( IsPrime(loop) )
{
printf(" %d", loop);
}
}
printf("\n");

return 0;
}

int IsPrime(int num)
{
int loop;
for(loop = 2; loop < num; loop++)
{
if( (num % loop) == 0 )
{
return 0;
}
}

/* We got this far, must be prime */

return 1;
}
```

6. (20 points) Write a program that reads a date as a long integer in the format MMDDYYYY. The program will then print out the date in the format <Month Name> <day>, <year>.

Sample run:

```   Enter date: 12192000
December 19, 2000.
```
In the input the month and the day will always be given as two digits, including leading zero. So the 20th of May, 2000 will be entered as 05202000 and the 3rd of December, 2000 will be entered as 12032000.
Don't worry about writing down all of the months, write code that can handle just the first couple and you can put a comment like /* Remaining months the same way */.

```   #include <stdio.h>

int main()
{
long date;
int day = 0, year = 0;

printf("Enter date: ");
scanf("%ld", &date);

day = (date % 100000) / 10000;
year = date % 10000;

switch( date / 1000000 )
{
case 1:
printf("January ");
break;
case 2:
printf("February ");
break;
case 3:
printf("March ");
break;
case 4:
printf("April ");
break;
case 5:
printf("May ");
break;
case 6:
printf("June ");
break;
case 7:
printf("July ");
break;
case 8:
printf("August ");
break;
case 9:
printf("September ");
break;
case 10:
printf("October ");
break;
case 11:
printf("November ");
break;
case 12:
printf("December ");
break;
}

printf("%d, %d\n", day, year);

return 0;
}
```

7. (20 points) Write a program that reads an integer N greater than 0 from the keyboard and then produces a series of numbers according to the follwing rules:

• If the current value of N is even, the next value of N will be equal to (N/2)
• If the current value of N is odd, the next value of N will be equal to (3 * N + 1)
• The series stops when N becomes equal to 1.

Write and use a function called Next which takes one integer parameter and returns the next value in the series.

Sample run:

```   Enter starting number : 3
Series : 3 10 5 16 8 4 2 1
```
Notes and hints:
• In the output you can display the values one next to each other with a space between them. Don't worry about "neat" format on the screen (but don't forget a newline at the end of the output !).
• You can assume that the input from the user will be correct (that is, an integer > 0).
• The main function will "drive" the whole program with a loop. What kind of loop will you need ? What will determine when this loop ends ?
• How do you determine if a number is even or odd ?

```   #include <stdio.h>

int Next(int num);

int main()
{
int N = 0;

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

printf("Series : ");

do
{
printf("%d ", N);
N = Next(N);
}while( N != 1 );

printf("1\n");

return 0;
}

int Next(int num)
{
/* If even, return num/2 */

if( (num % 2) == 0 )
{
return (num / 2);
}
else
{
return (3 * num + 1);
}
}
```