I need to find the smallest number in my 10x8 array grid fileld with random numbers

I know im doing it wrong but I do not know what to do.

Heres my Code:

``````//ArrayGrid
#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;
}
``````

All 9 Replies

Hi Maven, welcome to DaniWeb :)
Set your variable `smallest` to an arbitrarily large number, bigger than any number in your data set.
Test `smallest` with every element of your array in your two loops on line 31. If you find an element smaller than `smallest` set smallest equal to that element.

Like Maven, I would just start with the highest number possible in your random array and start comparing at (0,0) and iterate through ithe array until you find a smaller number then keep going if the next smallest number is found then that becomes the smallest until you find a smaller or hit the end. With this method there is no need to start from (0,0) again with each discovery.

Good luck

This is what you might want to do...

``````int smallest = INT_MAX;
for (int i = 0; i < 10; i++)
{
for(int k = 0; k < 8; k++)
{
if (array[i][k] < smallest)
{
array[i][k] = smallest;
}
}
}
``````

Brian 14, perhaps u meant `smallest=array[i][k];` instead of `array[i][k] = smallest;` ?

ok , so i chaned my findsmallest function , but it still wont work.

``````//ArrayGrid
#include <iostream>
using namespace std;
int main()
{
int total,average;
int row=0;
int col=0;
int array [10][8];
srand(time(NULL));

//PrintArray

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

//findSmallest

int smallest = INT_MAX;

for (int i = 0; i > 10; i++)
{
for(int k = 0; k > 8; k++)
{
if (array[i][k] > smallest)
{
array[i][k]=smallest;
}
}
}

//Display Smallest

cout << "The Smallest Number Is: ";

cout << smallest << endl << endl;

//findAverage

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

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

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

The output for the smallest number would be like"The Smallest number is: 2191752821"

Why don't you change the line 34 `if (array[i][k] > smallest)` into `if (array[i][k] < smallest)` and line 36 into `smallest = array[i][k];`?

I did and its still the same output.. :(

Post the code that you have now.

never mind i fixed it thanks everyone for your help <3 !

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.