#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
usingnamespace 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.
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.
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?
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.