Find Smallest Number in 2d Array

I need to generate a grid of 10x8 and fill it with random numbers (max 70)

then i need to find the smallest number within the random numbers generated

and my "findSmallest" function does not seem to work and i do not know how to make it work

could I get some help please and thank you.

Here my Code:

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
//Assignment 18 Program 2
#include <iostream>
using namespace std;

int main()
{
 
    int row=0;
    int col=0;
    int ArrayGrid [9][11];
    srand(time(NULL));
    

    for(row=1; row<9; row++)
    {          
        for(col=1; col<11; col++)
        {       
            ArrayGrid  [row][col]= rand() % (1 + 70) ;
        }
         
    }
    
//PrintArray

    for(row=1; row<9; row++)
    {          
        for(col=1; col<11; col++)
        {       
            cout << ArrayGrid[row][col] << "\t";
                
        }     
              
        cout << ""<<endl<<endl;               
    }

//findSmallest  
int small;
  
    for(row=0;row<50;row++);
    for(col=0;col<50;col++);
    {
        if(ArrayGrid[row][col]<small)
        small=ArrayGrid[row][col];
    }
    cout << "The Smallest Number Is: " << small << endl<<endl;

system ("pause");
return 0;
}
Your grid is the dimension 9x11. Your findSmallest function checks from... 0 to 49? No wonder it doesn't work- you are checking more positions that don't exist than do.
I changed the "findsmallest" function to:

1
2
3
4
5
6
7
    for(row=0;row<9;row++);
    for(col=0;col<11;col++);
    {
        if(ArrayGrid[row][col]<small)
        small=ArrayGrid[row][col];
    }
    cout << "The Smallest Number Is: " << small << endl<<endl;


and now it only outputs the number 2.

The Smallest Number Is: 2
What is the initial value of `small`? To find out, print the value before you do any searches. Also you don't have a findsmallest function, you have a section of your code that finds smallest and this is all done inside the function `main`
Last edited on
You nee help understanding the dimensions of your array. Read this tutorial page to help you understand what you need to do: http://www.cplusplus.com/doc/tutorial/arrays/

The problem that I see is that you say you are creating an array that is 10x8, then you create an array that is 9x11.

In your first first nested for loop, you populate rows 1 through 8 and columns 1 through 10, completely ignoring anything in row 0 or column 0. Then in the new code snippet, you try to read from rows 0 through 8 and columns 0 through 10.

As discussed in the tutorial page, if you declare an array with dimension n, you get n elements numbered 0 through (n-1). You are not handling that properly.

Additionally, in this new code snippe, you have ';' after each of the for statements. The ';' become the statement that is looped over rather than the intended code block below it. Remove the ';' to get your code closer to running properly.
Topic archived. No new replies allowed.