/*
 * funcint3B.c
 */
#include <stdio.h>

int getanswer(void);  // prototype: takes nothing, returns int
float intaverage(int, int); // prototype: takes 3 int's, returns a float
int intproduct(int,int); // prototype: takes 2 ints, returns 1 int
float intquotient(int,int);
int intpower(int,int);
int intremainder(int,int); 

int main(void) // header for function main: takes nothing, returns int
{
  int   aa, bb; // declare 2 ints
  float avrg, quot;  // declare 2 float
  int   prod, pow, rem; // declare 3 int

  do // execute block, then evalueat while befor second pass
  {
    puts("This program calculates the average, product, quotient, power, and remainder of two integers.\n");

    printf("Enter two integers separated by a space: ");

    scanf("%d%d", &aa, &bb); // read in 2 integers, 
                           // interpret as digits
                           // assign to memory location of aa and bb
    getchar(); // fluch input buffer

    avrg=intaverage(aa,bb); // call function intaverage with value at aa and bb
                            // assign result to avrg
 
    printf("\n\nThe average of %d and %d is %.2f.\n", aa, bb, avrg);

    prod=intproduct(aa,bb); // call function intproduct with value at aa and bb
                          // assign result to prod
                          
    printf("The product of %d and %d is %d.\n", aa, bb, prod);

    quot=intquotient(aa,bb);
    printf("The number %d divided by %d is %.2f.\n", aa, bb, quot);

    // The follwing will be used later.
    // pow = intpower(aa,bb);
    // printf("The number %d raised to the power of %d is %d.\n", aa, bb, pow);
    // rem = intremainder(aa,bb);
    // printf("The remainder of %d divided by %d is %d.\n", aa, bb, rem);

  } 
  while( getanswer() ); // call function getanswer, 
                        // if return value is 1, return to block

  return 0;
}

int getanswer(void) // header for function getanswer, takes nothing, returns int
{
  char cc; // declare char 
  printf("\nPress 'q' to quit, any other key to continue: "); // prompt for user input
  cc=getchar(); // read from stdin, assing one char to cc
  if ( cc=='q') // if char is q, 
  {
    return 0; // reutrn 0 to calling fucntion
  }
  else
  {
    return 1; // return 1 to calling fruntion
  }
}

float intaverage(int xx, int yy) // header for function intaverage:
                                 // takes 2 ints, assigns to xx and yy
                                 // returns a float
{
  return (xx+yy)/2.0; // add xx and yy,
                      // divide result by 2.0
                      // Return the result to calling function 
}

int intproduct(int xx, int yy) // header for function intproduct:
                               // takes 2 ints, assigns to xx and yy
                               // returns an int
{
  return (xx*yy); // multiply xx by yy
                  // return result to calling funxiton
}

float intquotient(int xx, int yy)
{
  return ( (float)xx/yy);
}

int intpower(int xx, int yy)
{
  int ii,pp=1; // declare int and pp, assign 1 to pp
  for (ii=0; ii<yy; ii=(ii+1) )
  {
    pp=pp*xx;
  }
  return pp;
}

int intremainder( int xx, int yy)
{
  if (xx < yy)
  {
    return xx;
  }
  else
  {
    while (xx>=yy)
    {
      xx=xx-yy;
    }
    return xx;
  }
}