0

How do I properly pass the paramaters / overloads needed in the average(); function thats inside my main function? As you can se i created a function that takes two parameters but how do I pass the paramaters in the main?

#include <iostream>

using namespace std;



float average(int v[], int n) {
    //n = number of elements
    //v = the vector

    int sum = 0;
    int avg = 0;

    for (int i = 0; i <= n; i++) {
        cout << "Enter a number: ";
        cin >> v[i];
        sum += v[i];

    }
    avg = (double)sum / n;

    cout << avg;

    return 0;

}

int main() {
    average();
}
Jess Chan
  • 391
  • 2
  • 14

3 Answers3

1

1st thing to do is to change the main method to take the parameters...

int main(int argc, char** argv)

then

you can do a loop over argV

for(auto x = 0; x < argc; x++)
{
  std::cout << "argv[" << x << "] = " << argv[x] << std::endl;
}

remember all those are strings and arg at 0 is the name of the executable...

now, you can loop and take the input from the user at run time...

if that is your desired alg. then do something like:

#include <iostream>
using namespace std;

int main() {
    const auto K = 10;
    int array[K];
    std::cout << "Please give then numbers..." << std::endl;
    int sum = 0;

    for (size_t i = 0; i < K; i++)
    {
        std::cin >> array[i];
    }


    for (int i = 0; i < K; i++) {
        sum = sum + array[i];
    }
    double avg = double(sum) / K;
    std::cout << "avg: " << avg << std::endl;

    return 0;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 1
    You made it look so much more complicated, would you mind doing a more "simple" version, I know that the method you just posted is "simple" but if you look at my code i broke it down a bit more :p – Jess Chan May 03 '17 at 12:09
  • 1
    Thats much more readable but I was trying to use parameters, so like, I wanted to create a function with parameters and then sue it in main :p – Jess Chan May 03 '17 at 12:22
1

You can do it more interactive by allowing the user to enter as many numbers as he/she wants and then end them with a special number or character(mind the conversion in case of character) for example 0. then you can calculate sum or average or ... so the code goes like this:

#include <iostream>
using namespace std;

int main() {

    int current = 0;
    int sum = 0;
    int count = 0;

    while(true)
    {
        cout << "Please enter next number(end by entering 0): ";
        cin << current;
        cout << endl;
        sum = sum + current;
        count++;
    }

    double avg = sum / count;
    cout << "sum: " << sum << endl;
    cout << "avg: " << avg << endl;

    return 0;
}

Edit: if you realy want to use arguments and function this is the way:

#include <iostream>
#include <stdlib.h>     /* atoi */
using namespace std;

double avg(int argc, char** argv){

        int i=1;
        double sum = 0;

        while(i<argc){
                try{
                        sum = sum + atoi(argv[i]);
                }
                catch(int ex){
                        cout << "Failed to calculate average" << endl;
                        exit;
                }
                i++;
        }
        sum = sum / (argc-1); /* note that we have one extra argument because of the name of program. */
        return sum;
}

int main(int argc, char** argv) {

    double result = avg(argc, argv);
    cout << "avg: " << result << endl;

    return 0;
}
Iman Kianrostami
  • 482
  • 3
  • 13
0

Maybe you want this (as per your last edit):

#include <iostream>

using namespace std;    

double Average(int a[], int size)
{
  int sum = 0;

  for (int i = 0; i < 10; i++) {
    sum = sum + a[i];
  }

  return (double)sum / size;
}

int main() {
  int a[10] = { 1,2,3,4,5,6,7,8,9,10 };

  cout << Average(a, sizeof(a)/sizeof(a[0])) << endl;
  system("pause");

  return 0;
}

sizeof(a)/sizeof(a[0]) is a way to get the number of elements of the a array.

Or a more C++ like solution using the vector template class:

#include <iostream>
#include <vector>

using namespace std;    

double Average(vector<int> a)
{
  int sum = 0;

  for (auto & v : a)
    sum = sum + v;

  return (double)sum / a.size();
}

int main() {
  vector<int> a = { 1,2,3,4,5,6,7,8,9,10 };

  cout << "Average = " << Average(a) << endl;
  system("pause");

  return 0;
}

And with interactive user input:

int main() {
  vector<int> a;

  for (int i = 0; i < 10; i++)
  {
    cout << "Enter value " << i + 1 << endl;
    int value;
    cin >> value;
    a.push_back(value);
  }

  cout << "Average = " << Average(a) << endl;
  system("pause");

  return 0;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115