-1

These are global variables:

const int MAXROW = 5;
const int MAXCOL = 5;
int A[MAXROW][MAXCOL] = { 0 };
int newarr[MAXROW + 1][MAXCOL + 1] = { 0 };

How to declare them in main and to use them in these functions:

void case1()
{
for (int r = 0; r < MAXROW; ++r)
    for (int c = 0; c < MAXCOL; ++c) {
        cout << "\n A[" << r << "][" << c << "]= ";
        cin >> A[r][c];

    }

}

void case2()
{

    int max[MAXCOL] = { 0 };

    for (int r = 0; r < MAXROW; ++r) {
        int minr = A[r][0];
    ..............................

    }
void case3()
{
    int negNumber = 0;
    double average = 0;

    for (int r = 0; r < 6; ++r) {
        for (int c = 0; c < 6; ++c) {
            ..............
if (newarr[r][c] < 0) {
                ++negNumber;
                average += newarr[r][c];

I am using do while menu. How can I do it most easily.

aspaar321
  • 75
  • 1
  • 1
  • 9
  • If they are global variables, you don't "declare" them in main - you have already declared them when you said `const int MAXROW = 5; `, etc. And how do you use them? Well you just -um - use them. Llike you did in case1(). No need to pass them into the methods - they are globally available anywhere in the compilation unit. – FredK Jan 27 '16 at 20:23
  • The problem is that i don't want to use them as global. – aspaar321 Jan 27 '16 at 20:29

1 Answers1

0

You can declare local variables in main() in the same way you did in case3() with negNumber and average.

Passing variables to another functions is pretty easy. You just need to define input parameters for your function. Like this: case1( int var1 ). Now you can use var1 in your function case1 and you have to call case1 with arguments passed in. Like this: case1( 2 ).

You can find solution exactly for your case (passsing 2D arrays into funtions) here: Passing a 2D array to a C++ function

Read more about funtions in C++ here: https://msdn.microsoft.com/en-us/library/c4d5ssht.aspx

Community
  • 1
  • 1
RedRidingHood
  • 568
  • 3
  • 16