Chapter 9

I l @ ve RuBoard

Chapter 9

  1. A formal argument is a variable that is defined in the function being called. The actual argument is the value appearing in the function call; this value is assigned to the formal argument. You can think of the actual argument as being the value to which the formal argument is initialized when the function is called.

    1. void donut(int n)

    2. int gear(int t1, int t2)

    3. void stuff_it(double d, double *pd)

    1. char n_to_char(int n)

    2. int digits(double x, int n)

    3. int random(void)

  2.  int sum(int a, int b){     return a + b; } 
  3. Replace int with double throughout:

     double sum(double a, double b) {     return a + b; } 
  4. This function needs to use pointers:

     void alter(int * pa, int * pb) {     int temp;     temp = *pa + *pb;     *pb = *pa - *pb;     *pa = temp; } 

    or

     void alter(int * pa, int * pb) {     *pa += *pb;     *pb = *pa - 2 * *pb; } 
  5. Yes; num should be declared in the salami() argument list, not after the brace . Also, it should be count++ , not num++ .

  6. Here is one solution:

     int largest(int a, int b, int c) {     int max = a;     if (b > max)         max = b;     if (c > max)         max = c;     return max; } 
  7. Here is the minimal program; the showmenu() and getchoice() functions are possible solutions to parts a. and b.

     #include <stdio.h> void showmenu(void);     /* declare functions used */ int getchoice(int, int); main() {     int res;     showmenu();     while ((res = getchoice(1,4)) != 4)         printf("I like choice %d.\n", res);     printf("Bye!\n");     return 0; } void showmenu(void) {     printf("Please choose one of the following:\n");     printf("1) copy files          2) move files\n");     printf("3) remove files        4) quit\n");     printf("Enter the number of your choice:\n"); } int getchoice(int low, int high) {     int ans;     scanf("%d", &ans);     while (ans < low  ans  > high)     {         printf("%d is not a valid choice; try again\n", ans);         showmenu();         scanf("%d", &ans);     }     return ans; } 
I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net