two dimensional arrays function call

*edit* I see the indentation is weird, hope you can make out what the code does.

Hi!

I'm new at programming so I thought I'd pop by for some questions regarding an assignment at my uni.

Just began with c++, have some experience in ada and python. I'm supposed to create a 'golf score tab' using two-dimensional arrays. In essence, I have to create the array that represents 20 golf courses, and the score for each course. As stated, there are 20 courses and each course can have 1-8 scores. So basically, I create the arrays, fill the 20 'slots' with random integers with a range from 1-20. This is what I've got so far (pardon the code, there are things that might not make any sense but I've tried several different things that I've gathered from different sites in order to make it work.):

#include <iostream>
#include <cstring>
#include <stdlib.h>

#define course 20
#define score 8

using namespace std;

void fillArray(int golf_array[20][9])
{
for (int i = 0; course < 20; i++)
{
for (int j = 1; score < 9; j++)
{
golf_array[i][j] = rand() % 8 + 1;
}
}
}

////////////////////////////////////

void displayArray(int golf_array[20][9])
{
for (int i = 0; i < 20; i++)
{
for (int j = 1; j < 9; j++)
{
cout << golf_array[i][j] << " ";
cout << endl;
}
}
}

int main ()
{
int f;
int golf_score[20][8];

int fillArray(golf_score[20][8]);
int displayArray(golf_score[20][8]);
cin >> f;
}

I get no output when I run this. Might be because I don't fully understand how to call functions yet. So help regarding that is also greatly appreciated. Also, the int f; cin >> f; is just some foul fix in order to prevent my program to immediately shut down once it's done. Don't really know how to properly fix this yet either.

Help is greatly appreciated. Thank you in advance!
Last edited on
Use [code] tags to preserve indentation.

The problem is you are not calling your functions in main(); those lines you have there are just definitions. If you do a quick read on the functions bit on the tutorial here, you should get a good answer. If you still aren't sure, please post again.
Hi!

Took a look at the tutorial and I have to say I didn't really gather that much from it. But with that in light, should I declare a variable and then call the functions?

int callArray = fillArray(golf_score[20][8]);

something like that?
In function main() these two lines simply declare the functions (that is, tell the compiler that the function exists). You need to actively call the functions.
1
2
    int fillArray(golf_score[20][8]);
    int displayArray(golf_score[20][8]);


Instead of having magic numbers such as 20 or 8 or 9 scattered throughout the code, it is far better to use defined constants. That way, if you need to change the value, it just needs to change in one place, rather than altering many lines throughout the program.

When passing a 2D array as a parameter to a function, all but the first dimension must be specified in the function definition. However the function call need only use the name of the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cstring>
#include <cstdlib>

const int course = 20;
const int score = 8;

using namespace std;

void fillArray(int golf_array[][score])
{
    for (int i = 0; i < course; i++)
    {
        for (int j = 0; j < score; j++)
        {
             golf_array[i][j] = rand() % 8 + 1;
        }
    }
}

void displayArray(int golf_array[][score])
{
    for (int i = 0; i < course; i++)
    {
        for (int j = 0; j < score; j++)
        {
            cout << golf_array[i][j] << " ";
        }
        cout << endl;
    }
}

int main ()
{
    int golf_score[course][score];

    fillArray(golf_score);
    displayArray(golf_score);

    cin.get();
}
Topic archived. No new replies allowed.