Russell Manuel
Russell Manuel

Reputation: 11

How to compute sum, average and product of three numbers using different functions

I need to create a program that accepts 3 numbers and find the sum, average and product. I only need to use main(), get_ABC(), compute() and display() functions. I did it right but im not getting the right output about my mathematical operations.

#include<conio.h>
#include<iostream.h>

float get_A(float A)
{
    cout<<"Enter First Number: ";
    cin>>A;
    return(A);
}

float get_B(float B)
{
    cout<<"Enter Second Number: ";
    cin>>B;
    return(B);
}

float get_C(float C)
{
    cout<<"Enter Third Number: ";
    cin>>C;
    return(C);
}

 float compute_sum(float A,float B,float C)
{
 float sum;

 sum = A + B + C;
 return(sum);

}

float compute_ave(float A,float B,float C)
{
    float ave;
    ave = (A + B + C) / 3;
    return (ave);
}

float compute_prod(float A,float B,float C)
{
    float prod;
    prod = A * B * C;
    return(prod);
}

void display(float sum,float ave,float prod)
{
    cout<<"The sum of three numbers is "<<sum<<".\n";
    cout<<"The average of three numbers is "<<ave<<".\n";
    cout<<"The product of three numbers is "<<prod<<".";
}

float main()
{
    float A,B,C;
    float sum;
    float ave;
    float pro;
    clrscr();
    get_A(A);
    get_B(B);
    get_C(C);
    sum = compute_sum(A,B,C);
    ave = compute_ave(A,B,C);
    pro = compute_prod(A,B,C);
    display(sum,ave,pro);
    getch();
    return(0);
}

This is the output.

Enter First Number: 1
Enter Second Number: 2
Enter Third Number: 3
The sum of three numbers is 0.
The average of three numbers is 0.
The product of three numbers is 0.

I really need help. My prof give me this problem without teaching how to code, so i only come up with basics, i really gave up and end up here. You can change, add or replace the codes(with basic codes) if you want and i'll appreciate it.

Upvotes: 0

Views: 2176

Answers (1)

gsamaras
gsamaras

Reputation: 73394

Change this:

get_A(A);
get_B(B);
get_C(C);

to this:

A = get_A(A);
B = get_B(B);
C = get_C(C);

so that you use the return values of your functions.

Moreover, main() should return an int, not a float.

Furthermore, initialize your variables when you declare them, so that you avoid "is used uninitialized in this function" warnings.

Upvotes: 1

Related Questions