-2

I have been attempting this for hours to no avail, as you can see in my code I have separate functions, they were all together in main, but I am required to turn each into a separate function. However when I try anything I get errors, even when I try to pass parameters. Can someone point me in the right direction?

#include <iostream>
#include <cstdlib>
#include <ctime>
void printarray();
void average();
void largestnumber();
using namespace std;
int main()
{
    printarray();
    average();
    largestnumber();

    }


void printarray() {
    srand(time(0));
    int n[10], tot = 0;
    for (int i = 0; i < 10; i++)
    {
        n[i] = (1 + rand() % 100);
        cout << n[i] << endl;
    }

}

void average() {
    int j, tot = 0, n[10];
    for (j = 0; j < 10; j++)
    {
        tot += n[j];
    }
    cout << "The average of the numbers in the array are " << tot / j << endl;
}

void largestnumber() {
    int w = 1, int n[10];
    int temp = n[0];
    while (w < 10)
    {
        if (temp < n[w])
            temp = n[w];

        w++;
    }
    cout << "The largest number in the array is " << temp << endl;

}
WhiZTiM
  • 21,207
  • 4
  • 43
  • 68
Joe
  • 1
  • 1
  • Please write what you have tried and what error you get – NineBerry May 01 '16 at 23:40
  • [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329). – IInspectable May 01 '16 at 23:41
  • In printarray(), average(), and largestnumber() function you declare an array `n`. You do not initialize the contents of the array, then immediately proceed to use the values in the array. Since the array is not initialized, this becomes undefined behavior, and your results from this will be randomly-generated garbage. – Sam Varshavchik May 01 '16 at 23:53

1 Answers1

0

The array you are working with needs to be passed in to each function, so the same array is used everywhere. It is a good idea to pass the size as well, just for flexibility reasons.

Now your functions pretty much work as you wrote them.

#include <iostream>
#include <cstdlib>
#include <ctime>
void printarray(int n[], size_t size);
void average(int n[], size_t size);
void largestnumber(int n[], size_t size);
using namespace std;

int main()
{
    const size_t arr_size = 10;
    int n[arr_size];

    printarray(n, arr_size);
    average(n, arr_size);
    largestnumber(n, arr_size);

}


void printarray(int n[], size_t size) {
    srand((unsigned int)time(0));
    int tot = 0;
    for (size_t i = 0; i < size; i++)
    {
        n[i] = (1 + rand() % 100);
        cout << n[i] << endl;
    }

}

void average(int n[], size_t size) {
    size_t j;
    int tot = 0;
    for (j = 0; j < size; j++)
    {
        tot += n[j];
    }
    cout << "The average of the numbers in the array are " << tot / j << endl;
}

void largestnumber(int n[], size_t size) {
    size_t w = 1;
    int temp = n[0];
    while (w < size)
    {
        if (temp < n[w])
            temp = n[w];

        w++;
    }
    cout << "The largest number in the array is " << temp << endl;

}

One simple improvement is to break the printarray out into an initarray function that fills the array and printarray that prints the content.

It would also be a good idea to do some checking for things like an empty array (functions assume n[0] exists, for instance).

The next obvious step is to put all this in a class. Also, if you are allowed to, the c array should be replaced with a vector, as that does a great job of keeping all the resource information together.

Simon Parker
  • 1,656
  • 15
  • 37