Making code more efficient through functions

Write your question here.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  #include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

class Functions
{
    private:
        int input{};
        int numRows{};
        int numColumns{};
        int holder{};

    public:
        void rndArrayMaxNumber (int x, int y)
        {
            int tempArray [x][y]{};
            srand(time(0));

            for (int j=0;j<x;j++)
            {
                for (int i=0;i<y;i++)
                {
                    tempArray[j][i]= (rand()%99)+1;
                }
            }

            for (int j=0;j<x;j++)
            {
                for (int i=0;i<x-1;i++)
                {
                    for (int k=0;k<y-1;k++)
                    {
                        if (tempArray[i][k] < tempArray[i][k+1])
                        {
                            holder=tempArray[i][k];
                            tempArray[i][k]=tempArray[i][k+1];
                            tempArray[i][k+1]=holder;

                        }
                    }

                }
            }

            for (int j=0;j<y;j++)
            {
                for (int i=0;i<y-1;i++)
                {
                    for (int k=0;k<x-1;k++)
                    {
                        if (tempArray[k][i] < tempArray[k+1][i])
                        {
                            holder=tempArray[k][i];
                            tempArray[k][i]=tempArray[k+1][i];
                            tempArray[k+1][i]=holder;

                        }
                    }

                }
            }

            for (int i=0;i<y;i++)
            {
                for (int k=0;k<x;k++)
                {
                    cout << tempArray[i][k] << "\t";
                }
                cout << endl;
            }

            cout << endl << "The greatest number is " << tempArray[0][0];
        }

        void arrayChoice ()
        {
            cout << "Enter the number of rows: ";
            cin >> numRows;
            cout << "Enter the number of columns: ";
            cin >> numColumns;
            cout << endl;
        }

        void menu ()
        {
            while (input!=7)
            {
                cout << "1. 2D array random numbers and show highest number" << endl;
                cout << "2. Exit" << endl << endl;
                cout << "Enter the number of the menu option you want to proceed with: ";
                cin >> input;
                cout << endl;

                switch (input)
                {
                    case 1:
                        arrayChoice();
                        rndArrayMaxNumber(numRows, numColumns);
                        cout << endl << endl;
                        break;
                }


            }

        }
};

int main()
{
    Functions program;
    program.menu();

    return 0;
}


I would like to create separate functions that fill and sort the array. But I'm not sure how to put an array in the function parameters, and how to store the values in the original array that was sent to the function.
Hello Shezshade,

To send an array to a function all you need is the array name.

In the prototype and function definition you have to tell it about the array like:
FunctionName(int arrayName[][size]) where "size" is either a variable defined as a "constexpr" or "const", the first being a newer version of "const", for a single dimension array the size is not needed. Either way at the function the array degrades to a pointer to the first element.

A lot of times when working on a program I like to pass the array by reference. A little more involved, but it allow you to see the whole array in the function and that would be: FunctionName(int (&arrayName)[size][size]). When passing by reference the size of each dimension must be known even for a 1D array.

Work up some code and post it if you have any questions.

Hope that helps,

Andy
Thank you Andy! I am trying to pass by reference but I would like to keep it so that the user can still choose the number of rows and columns, but it doesn't seem to work. Is that still possible using pass by reference?
Shezshade wrote:
...user can still choose the number of rows and columns...

use vector<vector<int>>
Hello Shezshade,

With the line of code int tempArray [x][y]{}; I think this can be bone,bot this is not the way to do it. For what is generally accepted this can not be done because "x" and "y" need to be a constant value at compile time so the compiler knows how much memory to set aside for the array. If you want to use a variable like this you will need to create a dynamic array.

Another option it to create the array larger than you need and use "x" and "y" to keep track of how much of the array is used.

The vector of vector is an option as ick1 suggested if you are use to using them.

Hope that helps,

Andy
Topic archived. No new replies allowed.