2 Replies - 1127 Views - Last Post: 24 October 2014 - 12:12 PM Rate Topic: -----

#1 Glaven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Please Help.

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   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12332
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 792
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1