Site hosted by Angelfire.com: Build your free website today!

Chapter 6 - Making Decisions

List of Questions


Back to Home Page

Question 1

Type in and run the ten programs presented in this chapter. Compare the output produced by each program with the output presented after each program in the text. try experimenting with each program by keying in values other than those shown.

Answer 1

Basic instruction... Back to Top

Question 2

Write a program that asks the user to type in two integer values at the terminal. Test these two numbers to determine if the first is evenly dividible by the second, and then display an appropriate message at the terminal.

Answer 2


/*Chapter 6 Exercise 2 (6.Ex2)*/

#include <stdio.h>

int main (void)
{
    int i1, i2, total = 0;
    
    printf ("\nPlease enter the first integer: ");
    scanf  ("%i", &i1);
    
    printf ("\nPlease enter the second integer: ");
    scanf  ("%i", &i2);
       
    if (i1 % i2 == 0) {
       printf ("\n%i is evenly divisible by %i\n", i1, i2);
       total = i1 / i2;
       printf ("%i / %i = %i\n", i1, i2, total);
       }
       
    else
       printf ("\n%i is not evenly divisible by %i\n", i1, i2);

return 0;
}
Back to Top

Question 3

Write a program that accepts two integer values typed in by the user. Display the result of dividing the first integer by the second, to three-decimal-place accuracy. Remember to have the program check for division by zero.

Answer 3


/*Chapter 6 Exercise 3 (6.Ex3)*/

#include <stdio.h>

int main (void)
{
    float f1, f2, total;
    
    printf ("\nPlease enter the first integer: ");
    scanf  ("%f", &f1);
    printf ("\nPlease enter the second integer: ");
    scanf  ("%f", &f2);
     
    total = f1 / f2;
    
    if (f2 > 0)
       printf ("\n%.0f divided by %.0f gives %.3f\n", f1, f2, total);
    
    else   
       printf ("\nCannot divide by zero.\n");

return 0;
} 
Back to Top

Question 4

Write a program that acts as a simple "printing" calculator. The program should allow the user to type in expressions of the form:

number operator

The following operators should be recognised by the program:

+ - * / S E

The S operator tells the program to set the "accumulator" to the typed-in number. The E operator tells the program that execution is at an end. The arithmetic operations are performed on the contents of the accumulator with the number that was keyed in acting as the second operand.

Make certain that the program detects division by zero and also checks for unknown operators.

Answer 4

N.B. With this program I needed to change the exit operator to N because no matter what I tried the code would go into an endless loop if the exit operator was set to E.


/*Chapter 6 Exercise 4 (6.Ex4)*/

#include <stdio.h>

int main (void)
{
    float accum = 0, number = 0;
    char oper;
    
    printf ("\nHello. This is a simple 'printing' calculator. Simply enter the number followed");
    printf (" by the operator that you wish to use. ");
    printf ("It is possible to use the standard \noperators ( +, -, *, / ) as well as two extra "); 
    printf ("operators:\n");
    printf ("1) S, which sets the accumulator; and\n"); 
    printf ("2) N, that ends  the calculation (N.B. Must place a zero before N). \n");
    
      do {
         printf ("\nPlease enter a number and an operator: ");
         scanf  ("%f %c", &number, &oper);
           if (number == 0 && oper == 'N'){
              printf ("Total = %f", accum);
              printf ("\nEnd of calculations.");}
           else if (oper == '+', '-', '*', '/', 'S'){
                   switch (oper)
                   {
                     case 'S':
                      accum = number;
                      printf ("= %f", accum);
                      break;
                     case '+':
                      accum = accum + number;
                      printf ("= %f", accum);
                      break;
                     case '-':
                      accum = accum - number;
                      printf ("= %f", accum);
                      break;
                     case '*':
                      accum = accum * number;
                      printf ("= %f", accum);
                      break;
                     case '/':
                       if ( number != 0)
                        {accum = accum / number;
                         printf ("= %f", accum);}           
                       else
                        printf ("Cannot divide by zero.");
                        break;
                      default:
                       printf ("Error. Please ensure you enter a correct number and operator.");       
                       break;          
                    }     
                  }
                else
                 printf ("Error. Please ensure you enter a correct number and operator.");      
         }
             while (oper != 'N');      
          return 0;
}
Back to Top

Question 5

You developed Program 5.9 to reverse the digits of an integer typed in from the terminal. However, this program does not function well if you type in a negative number. Find out what happens in such a case then modify the program so that negative numbers are correctly handled. For example, if the number -8645 is typed in, the output of the program should be 5468-.

Answer 5


/*Chapter 6 Exercise 5 (6.Ex5)*/

#include <stdio.h>

int main()
{
  	int number, number2, right_digit = 0;
  	char minus = '-';
  	
  	printf ("Enter your number:\n");
  	scanf  ("%i", &number);
  	
  	if (number >= 0){
      do {
        right_digit = number % 10;
        printf ("%i", right_digit);
        number = number / 10;
    }
    while (number !=0);
      printf ("\n");
    }
      
    else{ 
      number2 = number * -1;
      do {
        right_digit = number2 % 10;
        printf ("%i", right_digit);
        number2 = number2 / 10;
      }
    
      while (number2 != 0);
      printf ("%c", minus);  
      printf ("\n");
      }
          
    return 0;
}
Back to Top

Question 6

Write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in english. So, if the user types in 932, the program should display:

nine three two

Remember to display "zero" if the user just types in just a 0. (Note:This exercise is a hard one!)

Answer 6


/*Program 6 Exercise 6 (6.Ex6)*/

#include <stdio.h>

int main (void)
{

    int number = 0, right_digit = 0, number2 = 0, mult = 1;
    
    printf ("This program will convert numbers as numerals into words.");
    printf ("\nPlease enter a number: ");
    scanf  ("%i", &number);
    
  while (number !=0) {
        right_digit = number % 10;    
        number = number / 10;
        number2 = number2 * mult + right_digit;
        while (mult != 10) {
              mult = mult * 10;}
  }
        
        right_digit = 0;
        
        do {
        right_digit = number2 % 10;           
        if (right_digit == 0)    
           {printf ("zero ");}
           else if (right_digit == 1)
           {printf ("one ");}
           else if (right_digit == 2)
           {printf ("two ");}
           else if (right_digit == 3)
           {printf ("three ");}
           else if (right_digit == 4)
           {printf ("four ");}
           else if (right_digit == 5)
           {printf ("five ");}                      
           else if (right_digit == 6)
           {printf ("six ");}
           else if (right_digit == 7)
           {printf ("seven ");}               
           else if (right_digit == 8)
           {printf ("eight ");}
           else
           printf ("nine "); 
        number2 = number2 / 10;
           }
        while (number2 != 0);     

    
    return 0;
}
Back to Top

Question 7

Program 6.10 has several inefficiencies. One inefficiency results from checking even numbers. Because it is obvious that any even number greater than two cannot be prime, the program could simply skip all even numbers as possible primes and as possible divisors. The inner for loop is also inefficient because the value of p is always divided by all values of d from two to p-1. This inefficiency could be avoided by addding a test for the value of isPrime in the conditions of the for loop. In this manner, the forloop could be set up to continue as long as no divisor was found and the value of d was less than p. Modify Program 6.10 to incorporate these changes. Then run the program to verify its operation.

Answer 7


/*Chapter 6 Exercise 7 (6.Ex7)*/

#include <stdio.h>
#include <stdbool.h>

int main (void)
{
    int   p, d;
    _Bool isPrime;
    
    for (p = 2; p % 2 != 0, p <= 50; ++p) {
        isPrime = 1;
        
        for (d = 2; d % 2 != 0, d < p; ++d)
            if (p % d == 0)
               isPrime = 0;
            
            if (isPrime != 0)
               printf ("%i   ", p);
   }           
               printf ("\n");
   
   return 0;
}
Back to Top

Back to Home Page