sls1981
sls1981

Reputation: 53

Functions and separating a program

Basically my homework this week is:

Write three functions: One which when supplied with two integers, returns the lower. Another which returns the higher of two supplied integers. And a third which returns the average of two supplied integers To test your functions, write a program which prompts for two integers and then prints out the lower, then the higher and then the average. Your program should include the function prototypes.

Anyway, as I mentioned before, I seem to have real difficulty with this module - even though it is my favourite.

So here is my code so far (embarrassing). I cant work out if my logic should be above or below the int main line. Just really confused overall. I have been trying so hard with this module (far more than all of the others) but still feel like a lost child with it.

#include <stdio.h>
#include <stdlib.h>

int getLow(int num1 int num2);

int getHigh(int num1 int num2);

int getAvg(int num1 int num2);

int main()
{
    int lores, hires, avres;


    printf("Enter two integers:\n");
    scanf("%i %i", &num1, &num2);

    int getLow(int num1, int num2){

    if (num1 < num2){
        printf("%i is the lowest\n", num1);}
      if (num1 > num2){
        printf("%i is the lowest\n", num2);}}

   int getLow(int num1, int num2){

    if (num1 > num2){
        printf("%i is the highest\n", num1);}
      if (num1 < num2){
        printf("%i is the highest\n", num2);}}

    int getAvg(int num1, int num2)

    getAvg = (num1 + num2) / 2;

     printf("%i is the average of the two integers\n", getAvg);

    return 0;
}

Upvotes: 0

Views: 124

Answers (3)

John Bode
John Bode

Reputation: 123598

C doesn't allow nested functions - you can't define getLow, getHigh, or getAvg within the body of main.

A function definition may appear before (getHigh) or after (getLow) its caller:

int getHigh( int num1, int num2 )      // function definition, also serves
{                                      // as a function declaration
  return num1 > num2 ? num1 : num2;
}

int getLow( int num1, int num2 );      // function declaration

int main( void )
{
  double getAvg( int num1, int num2 ); // function declaration
  ...
  high = getHigh( num1, num2 );        // function call
  low = getLow( num1, num2 );          // function call
  avg = getAvg( num1, num2 );          // function call
  ...
}

int getLow( int num1, int num2 )       // function definition
{
  return num1 < num2 ? num1 : num2;
}

A function declaration may appear within its caller (getAvg) or before it (getLow).

A function declaration must be visible before the function is called. Since a function definition also serves as a declaration, I will typically define a function before its caller (if they're both in the same source file), so my code tends to read from the bottom up.

Upvotes: 1

Rode093
Rode093

Reputation: 406

#include <stdio.h>

int getLow(int num1, int num2);
int getHigh(int num1, int num2);
float getAvg(int num1, int num2);

int main()
{
    float avg;
    int num1, num2, low, high;

    printf("Enter two integers:\n");
    scanf("%i %i", &num1, &num2);

    low=getLow(num1, num2);
    high=getHigh(num1, num2);
    avg=getAvg(num1, num2);

    printf("Low: %d, High: %d, Average: %f", low, high, avg);

    return 0;
}  

int getLow(int num1, int num2){
    if (num1 < num2)
        return num1;
    return num2;
}

int getHigh(int num1, int num2){
    if (num1 > num2)
        return num1;
    return num2;
}

float getAvg(int num1, int num2){
    return (float)(num1 + num2) / 2;
}

Output:

Enter two integers:
5 6
Low: 5, High: 6, Average: 5.500000

Upvotes: 1

Mangu Singh Rajpurohit
Mangu Singh Rajpurohit

Reputation: 11420

#include <stdio.h>
#include <stdlib.h>

int getLow(int num1, int num2);
int getHigh(int num1, int num2);
float getAvg(int num1, int num2);

int main()
{
    int num1, num2, avres;

    printf("Enter two integers:\n");
    scanf("%i %i", &num1, &num2);

    float avg = getAvg(num1, num2);
    printf("The smallest number is %d", getLow(num1, num2));
    printf("The largest number is %d", getHigh(num1, num2));
    printf("%f is the average of the two integers\n", avg);

    return 0;

}

float getAvg(int num1, int num2)
{
    return (num1 + num2) / 2.;
}

int getLow(int num1, int num2)
{
    if (num1 < num2)
    {
        return num1;
    }
    else
    {
        return num2;
    }
}

int getHigh(int num1, int num2)
{
    if (num1 > num2)
    {
        return num1;
    }
    else
    {
        return num2;
    }

}

Upvotes: 2

Related Questions