Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
checking numbers
Author Message
potatoes

Posted: Wed Apr 14, 2004 7:46 pm   Post subject: checking numbers

ok the following is *suppose* to check the if all teh values in one column are different. the first column starts from 1 to 15 the next is from 16 to 30 and soo on just like bingo. what it does right now is check finds the values that are alike and changes one of them... except it doesnt change it according to its proper range... well thats wat i think its doing.. can somemone look through it and offer some ideas??

for (int count = 0 ; count < 60 ; count += 15)
{
for (int check = 0 ; check < col ; check += 2)
{

if (board [row] [col] == board [check] [col])
{
board [row] [col] = (int) (Math.random () * 14) + 1 + count;
flag = true;
}
}

}

potatoes

Posted: Wed Apr 14, 2004 9:01 pm   Post subject: (No subject)

hello...
anyone out there......
potatoes

Posted: Wed Apr 14, 2004 9:49 pm   Post subject: (No subject)

ya sooo i have a question the Math.random command thing...is there a way to have expections like this is the range of numbers but dont choose this number from the range????
wtd

Posted: Wed Apr 14, 2004 10:17 pm   Post subject: (No subject)

The data you're dealing with is a board, which is composed of columns. So we have two classes. Column is an inner class of Board since we'll not need it outside of the context of a Board.

Column is a comparatively simply class. It holds an array of integers that is a certain number of rows long. Our constructor for that class merely initializes the array and sets all of its data to zero.

It has an allUnique() method which checks to see if all of the values in the column are unique. To accomplish this it loops over the entire array once for each element in the array. In this way it compares all elements to each other, taking care not to compare the same element to itself.

It has a populateWithRandom(int low, int high, boolean unique) method which populates the array with random numbers in a given range. Ifthe unique parameter is true (it's false by default) then the method will ensure that all values are unique.

The Board class merely has a constructor and a method for accessing individual columns.

In the main method, I create a new Board with 4 columns and 15 rows. I then loop over the columns and populate each with random numbers in increasing ranges.

This code is untested.

 code: import java.io.*; import java.lang.*; class Exercise {         public class Board {                 private class Column {                         private int rows;                         private int[] data;                         public Column(int initRows) {                                 rows = initRows;                                 data = new int[rows];                                 for (int i = 0; i < rows; i++)                                         data[i] = 0;                         }                         public boolean allUnique() {                                 for (int i = 0; i < rows; i++)                                         for (int j = 0; j < rows; j++)                                                 if (i != j && data[i] == data[j])                                                         return false;                                 return true;                         }                                                 public void populateWithRandom(int low, int high, boolean unique = false) {                                 int range = high - low;                                 for (int i = 0; i < rows; i++) {                                         int rand;                                         boolean alreadyUsed = false;                                                                                 do {                                                 rand = (int)(Math.random() * range) + low;                                                                                         if (unique)                                                         for (int j = 0; j < i && !alreadyUsed; j++)                                                                 if (data[j] == rand)                                                                         alreadyUsed = true;                                         } while (alreadyUsed);                                                                                                 data[i] = rand;                                 }                                                     }                 }                 private int cols, rows;                 private Column[] columns;                 public Board(int initCols, int initRows) {                         cols = initCols;                         rows = initRows;                         columns = new Columns[cols];                         for (int i = 0; i < cols; i++)                                 columns[i] = new Column(rows);                 }                                 public Board column(int index) {                         return columns[index];                 }         }         public static void main(String[] args) {                 Board b = new Board(4, 15);                                 for (int i = 0; i < 4; i++)                         column(i).populateWithRandom(i * 15, (i * 15) + 15);         } }
wtd

Posted: Wed Apr 14, 2004 10:19 pm   Post subject: (No subject)

potatoes wrote:
ya sooo i have a question the Math.random command thing...is there a way to have expections like this is the range of numbers but dont choose this number from the range????

The basic algorithm is going to be:

1. Chose a random number from the range.
2. Check to see if it matches any of the numbers already chosen.
3. If it does, go to step 1.
4. Otherwise, store the random number in the array and go on to the next in the array.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 5 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: