All functions compile except the max function where i am trying to get it to find the maximum number in the matrix. The error message says overloaded function with no contextual type information Please tell me why.

#include <iomanip>
#include <iostream>
using namespace std;


      const int ROW = 3; //global const for # of rows
      const int COL = 3; //global const for # of columns
      const int LOW = 1; //lower limit
      const int UP = 10; // upper limit
      const int LOWer = 10; //global const for the lower limit
      const int UPer = 100; //global const for the upper limit

// Function prototypes:
void ZeroArray(int[][COL], int, int); //function prototype
void PrintArray(int[][COL], int, int); //function prototype
void GenerateArray(int[][COL], int, int, int, int); //function prototype
int SumArray(int[][COL], int, int); //function prototype
int FindMax(int[][COL], int, int); //function prototype



int main(void)
   
   {
   
  int table[ROW][COL]; //array declaration 
  int total;           //return from function 
  int min;             //return from function 
ZeroArray(table, ROW, COL); //function call
PrintArray(table, ROW, COL); //function call
srand(time(NULL)); 
GenerateArray(table, ROW, COL, LOWer, UPer); 
total = SumArray(table, ROW, COL); //function call


//problem with max findmax is here 

max = FindMax(table, ROW, COL); //function calll   

   return 0;
   }

//zero out

           void ZeroArray(int t[][COL], int row, int col){
          for (int r = 0; r < row; r++)
              for (int c = 0; c < col; c++)
                 t[r][c] = 0;
      }

    //random
    void GenerateArray(int t[][COL], int row, int col, int lower_limit, int upper_limit){
    for (int r = 0; r < row; r++)
        for (int c = 0; c < col; c++)
            t[r][c] = rand()%(upper_limit - lower_limit + 1) + lower_limit;
}

    //print

    void PrintArray(int t[][COL], int row, int col){
        for (int r = 0; r < row; r++){
            for (int c = 0; c < col; c++)
               cout << setw(5) << t[r][c];
            cout << endl;
        
}

}

//sum 

      int SumArray(int t[][COL], int row, int col){
          int sum = 0;
          for (int r = 0; r < row; r++)
              for (int c = 0; c < col; c++)
                 sum = sum + t[r][c];
          return sum;
      }
      



      //max
      
            int FindMax(int t[][COL], int row, int col){
          int max = t[0][0];
          for (int r = 0; r < row; r++)
              for (int c = 0; c < col; c++)
                 if(t[r][c] > max)
                     max = t[r][c];
          return max;
      }

Dani AI

Generated

The compile error reported was caused by using the identifier max without declaring it in main (as noted). In addition, max is a common name in the standard library or platform headers and can lead to confusing compiler errors or name clashes; prefer a clearer name such as maximum or maxValue.

Two practical bugs explain why output looked wrong even after compilation. First, PrintArray is called before GenerateArray, so the program prints the zeroed matrix instead of the generated one — call GenerateArray first, then print. Second, printing from inside utility functions mixes concerns and makes it harder to follow what’s printed; return values from SumArray and FindMax and do all user-facing output in main so the order is obvious (this echoes ’s suggestion to print after generation). Add simple spacing or a label before each printed matrix so multiple prints don’t appear “on top” of each other.

To count how many times the maximum occurs, compute the maximum first, then scan the array once more and increment a counter for equals. Example helper (fits your ROW/COL setup):

int CountOccurrences(int t[][COL], int row, int col, int value) {
    int count = 0;
    for (int r = 0; r < row; ++r)
        for (int c = 0; c < col; ++c)
            if (t[r][c] == value) ++count;
    return count;
}

/* usage in main, after GenerateArray and FindMax:
int maximum = FindMax(table, ROW, COL);
int times = CountOccurrences(table, ROW, COL, maximum);
std::cout << "Maximum: " << maximum << " (occurs " << times << " times)\n";
*/

Minor portability and style notes: include <cstdlib> and <ctime> when using srand(time(NULL)); remove unused variables (like min) or implement them; seed once at program start. To keep the console open portably (avoid system("pause")), use a standard input wait such as std::string dummy; std::getline(std::cin, dummy);. Finally, follow ’s formatting advice so posted code is easier to read (and ’s request for the latest code makes debugging simpler).

Recommended Answers

All 15 Replies

You forgot to declare max

thanks everything compiles now. I have another question I can not make system("PAUSE"); work. In other words how would I put system pause at the end of my code to make it show my results?

system("pause") isn't really a good method to use anyway, as it makes your code non-portable and you're using way more system resources than you need to.

Instead, use the get member function of cin:

cin.get();

I'm always just a little taken aback by things like getch(); and system( "pause" ); stuck in people's code. It is non-portable and in the case of getch() and cin.get(), leaves the input in an indeterminate state. Since the standard input is line bufferered, the most robust code would be something like: while (cin.get() != '\n'); ...which would read and discard anything typed until the ENTER key is pressed.

Even so, IMAO you shouldn't need to put such things in your code... The IDE should be able to do it for you or just run it from the command line as it was designed...

/me puts soapbox away

what? This is my code. It all compiles but I cant get it to show me a result. Does anyone see anything wrong with it. I wrote it myself and I think I did it right but dont know for sure and it is due tomorrow.

#include <iomanip>
#include <iostream>
using namespace std;


      const int ROW = 3; //global const for # of rows
      const int COL = 3; //global const for # of columns
      const int LOW = 1; //lower limit
      const int UP = 10; // upper limit
      const int LOWer = 10; //global const for the lower limit
      const int UPer = 100; //global const for the upper limit

// Function prototypes:
void ZeroArray(int[][COL], int, int); //function prototype
void PrintArray(int[][COL], int, int); //function prototype
void GenerateArray(int[][COL], int, int, int, int); //function prototype
int SumArray(int[][COL], int, int); //function prototype
int FindMax(int[][COL], int, int); //function prototype



int main(void)
   
   {
   
  int table[ROW][COL]; //array declaration 
  int total;           //return from function 
  int min;             //return from function 
  int max;
ZeroArray(table, ROW, COL); //function call
PrintArray(table, ROW, COL); //function call
srand(time(NULL)); 
GenerateArray(table, ROW, COL, LOWer, UPer); 
total = SumArray(table, ROW, COL); //function call
max = FindMax(table, ROW, COL); //function calll   

   return 0;
   }

//zero out

           void ZeroArray(int t[][COL], int row, int col){
          for (int r = 0; r < row; r++)
              for (int c = 0; c < col; c++)
                 t[r][c] = 0;
      }

    //random
    void GenerateArray(int t[][COL], int row, int col, int lower_limit, int upper_limit){
    for (int r = 0; r < row; r++)
        for (int c = 0; c < col; c++)
            t[r][c] = rand()%(upper_limit - lower_limit + 1) + lower_limit;
}

    //print

    void PrintArray(int t[][COL], int row, int col){
        for (int r = 0; r < row; r++){
            for (int c = 0; c < col; c++)
               cout << setw(5) << t[r][c];
            cout << endl;
        
}

}

//sum 

      int SumArray(int t[][COL], int row, int col){
          int sum = 0;
          for (int r = 0; r < row; r++)
              for (int c = 0; c < col; c++)
                 sum = sum + t[r][c];
                 cout << "The sum of the matrix is"  << sum;          
          return sum;
      }
      
      //max
      
            int FindMax(int t[][COL], int row, int col){
          int max = t[0][0];
          for (int r = 0; r < row; r++)
              for (int c = 0; c < col; c++)
                 if(t[r][c] > max)
                     max = t[r][c];
          cout << "The maximum number in the matrix is" << max;
          return max;
      }

Ehem...

Do one of the following:

1. Tell your IDE that you are compiling a console application --it should be smart enough to wait for you to read the console before the window disappears. If it can't do that:

2. Run your program from the command prompt. Or.

3. Use while (cin.get() != '\n'); instead of system().


[EDIT]
Yoinks...

I just tested your code and it works fine for me.

You might want to put a PrintArray() after you GenerateArray() just to see what the generated array was before printing out information.

Also, don't forget spacing and newlines in your output. For example, in sumArray() you should have: cout << "The sum of the matrix is " << sum << endl; And stick that while loop I gave you before the return statement in main().

Hope this helps.

how do i run from command prompt and what is command prompt what is system() and what does

Tell your IDE that you are compiling a console application --it should be smart enough to wait for you to read the console before the window disappears. If it can't do that:

mean

Are you doing this for school or just for your own learning?

You are the one who asked about system("pause"); in your second post. How is it you are writing a command-line application and not know what the command line is or how to access it? Go hit your teacher over the head.

Are you doing this for school or just for your own learning?

You are the one who asked about system("pause"); in your second post. How is it you are writing a command-line application and not know what the command line is or how to access it? Go hit your teacher over the head.

I am doing it for school and yes I would like to hit my teacher over the head. He doesn't teach very well.

thanks for the help

:icon_lol: Yeah, a lot of beginning-programming teachers are like that.

Glad to be of help. :icon_cheesygrin:

i cant figure out how to find out how many times the maximum number occurred and my matrices are on top of each other

please post your latest code

can i delete this whole thread to protect my code from being viewed by teacher

Please format your code. It's really hard to follow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.