-1

Trying to fulfill the coding prompt Input a list of positive numbers, find the mean (average) of the numbers, and output the result. Use a subprogram to input the numbers, a function to find the mean, and a subprogram to output the result.

I have done smaller calls with passing arguments but this one requires 3 separate subprograms, 1 for input, 1 for calculations and one to display the result. So far my program is does not start the initial call for input

#include <iostream>

using namespace std;

//prototypes
int prompt(int sum, int count );
float average(int sum, int count);
void result(float avg);

int main()
{
    int num;
    cout << "Welcome to Keith's Averaging program";
    cout << endl;
    int prompt();
    int average (int sum, int count);
    void result (float avg);

    return 0;
}

//Prototype Definitions
//get numbers from users
int prompt()
{
    int num, sum, count;
    cout << "Enter numbers and I will Average them." << endl;
    cout << "Please enter a number: ";
    cin >> num;
    sum = sum + num;
    if(num == 0)
    {
        cout << "Guess you don't want an average";
        cout << endl;
        cout << "Goodbye";
    }
    for(count=0; num !=0; count++)
    {
        cout << "Please enter a positive number, enter zero to compute the avg: ";
        cin >> num;
        if(num < 0)
        {
            cout << "Enter a positive number:";
            cin >> num;
        }
        sum = sum + num;


    }

Displays my welcome message then exits

  • 2
    `prompt()` instead of `int prompt();`? Edit: there's many other errors such as not enough argument for prompt(). I suggest you learn how to use functions here: https://www.geeksforgeeks.org/functions-in-c/ – Arne Jul 12 '19 at 04:22
  • `int prompt();` declares a new function, and does not call it. – L. F. Jul 12 '19 at 04:28
  • 1
    Also, pick a [good book](https://stackoverflow.com/q/388242) to learn. – L. F. Jul 12 '19 at 04:30

1 Answers1

0

I have put some comments in your code as explanation.

#include <iostream>

using namespace std;

//prototypes                            // These are declarations, definitions should also contain
                                        // same function signatures
int prompt(int& sum, int& count);       // accept arguments as reference (Read about it)
float average(int& sum, int& count);
void result(float& avg);

int main()
{
    // int num;         // don't need num in this function, not used
    int sum = 0, count = 0;     // create variables sum and count and initialize them to 0
    float avg;
    cout << "Welcome to Keith's Averaging program";
    cout << endl;
    prompt(sum, count);     // don't need function return type and argument return type when calling
                            // a function
    cout << sum << " " << count << endl;    // print the values after prompt() call
                                            // prompt() call must have filled the values sum and count
    average(sum, count);
    result(avg);

    return 0;
}

//Prototype Definitions
//get numbers from users
int prompt(int& sum, int& count)
{
    int num;
    cout << "Enter numbers and I will Average them." << endl;
    cout << "Please enter a number: ";
    cin >> num;
    sum = sum + num;
    if(num == 0)
    {
        cout << "Guess you don't want an average";
        cout << endl;
        cout << "Goodbye";
    }
    for(count=0; num !=0; count++)
    {
        cout << "Please enter a positive number, enter zero to compute the avg: ";
        cin >> num;
        if(num < 0)
        {
            cout << "Enter a positive number:";
            cin >> num;
        }
        sum = sum + num;
    }
}
float average(int& sum, int& count){
    // TODO: implement this
}

void result(float& avg) {
    // TODO: implement this
}
  • I have changed various parts of your code. I changed the function prototypes so that they take arguments by reference.

  • In the int main() function, I created two variables sum and count and initialized them to 0 - we will use these variables when calling those functions.

  • In the int prompt() function, I changed the function signature so that it matches the declared definition (otherwise it would have been some other function). Also, I removed the local declarations sum and count since we now have them as function arguments.

  • I have also put the definition blocks for other two functions and you can implement them (I have marked them as // TODO).

kiner_shah
  • 3,939
  • 7
  • 23
  • 37