# Find Smallest Number In Array filled with Random Numbers

Page 1 of 1

## 2 Replies - 1127 Views - Last Post: 24 October 2014 - 12:12 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=356526&amp;s=bd210151e990193733a5c64cd3121eb5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Glaven

Reputation: 0
• Posts: 1
• Joined: 24-October 14

# Find Smallest Number In Array filled with Random Numbers

Posted 24 October 2014 - 09:28 AM

I need to find the smallest number in my 10x8 arraygrid with random numebr filled in it

I know im doing it wrong but i really do not know what to do

here my Code:

```//Assignment 18 Program 2
#include <iostream>
using namespace std;

int main()
{
int total,average,smallest;
int row=0;
int col=0;
int ArrayGrid [10][8];
srand(time(NULL));

//PrintArray

for(row=1; row<10; row++)
{
for(col=1; col<8; col++)
{
{
ArrayGrid[row][col]= rand() % 70+1;
}
cout << ArrayGrid[row][col] << "\t";

}

cout << ""<<endl<<endl;
}

//findSmallest

for ( row=0;row<0;row++ )
for ( col=0;col<0;col++ )

{
if ( smallest >= 1)
cout << "Smallest Number Is: "<< smallest <<endl<<endl;
}
{
if ( smallest >= 1)
cout << "Smallest Number Is: "<< smallest << endl << endl;
}

//findAverage
for( row = 1;row <10;row++)
for( col =1;col <8;col++)
{
total += ArrayGrid[row][col];
}
average = total / 80;

//Display Average
cout << "The Average Of The Random Numbers Is: ";
cout << average << endl<<endl;

system ("pause");
return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: Find Smallest Number In Array filled with Random Numbers

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12332
• Posts: 45,436
• Joined: 27-December 08

## Re: Find Smallest Number In Array filled with Random Numbers

Posted 24 October 2014 - 09:31 AM

The idea with a find-min algorithm is this:
```findMin(array arr)
min := arr[0]

for i = 1 to arr.length-1
if arr[i] < min
min = arr[i]

return min
end function

```

The logic is the same in the case of a 2D array.

### #3 vividexstance

• Tiocfaidh ár lá

Reputation: 792
• Posts: 2,873
• Joined: 31-December 10

## Re: Find Smallest Number In Array filled with Random Numbers

Posted 24 October 2014 - 12:12 PM

If you know about functions then it might be a good idea to write a few to perform the different operations that the program needs to perform. Each function should have one thing to do and nothing more. When a function starts getting long, you may need to break that function up into smaller functions.

Either way, whenever you see code being repeated, sometimes with slight variations in the code, it's usually a good idea to try and "abstract" the repeated code into a function, and if possible, make the variations in the code into function arguments.

If you can do this successfully, your code will be easier to read and maintain.

*EDIT*:
I also suggest working on your program indentation and wrapping loops and other such constructs with curly braces to make the layout of the program easier to visualize. This will also make any missing braces or other such errors extremely easy to spot.

This post has been edited by vividexstance: 24 October 2014 - 12:16 PM