Jump to content

I need Help with a Program!!!!

xARACHN1D

Ok, I am in an AP computer science course. I have to write out a program soon and I am soooo lost on how to do so. Please forgive me as I am still a beginner programmer. I am coding with Java on a program called bluej.

Here is the prompt:

Write the Pick 5 Lottery program.  Your program should randomly generate 5 numbers from 1 and 50 and no two numbers should match for this set of numbers.  Then have your program randomly generate another set of 5 numbers from 1 to 50 and no two numbers should match from this set of numbers.  Once this is completed, determine how many matches you have between your 2 sets of numbers.  Print both sets of numbers along with the results of how many matches you have between the numbers.

 

ANYTHING HELPS!!!

Link to comment
Share on other sites

Link to post
Share on other sites

So.. What have you got so far?

If you get an assignment like this, I assume you know a bit about programming already, like:

- How to generate a random number

- How to get a list (hint hint, array) of numbers

- How to run through an array

- How to compare numbers

 

So... Personally I basically have this entire program ready in head, but I am not here to do other people's homework.. That kind of helps me learn some stuff, but that takes away the entire learning process from you.

The above list should help you get started.

Break down the program a little bit for yourself and tackle it in instances, instead of the whole thing at once. That should make it loads easier to complete.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, minibois said:

So.. What have you got so far?

If you get an assignment like this, I assume you know a bit about programming already, like:

- How to generate a random number

- How to get a list (hint hint, array) of numbers

- How to run through an array

- How to compare numbers

 

So... Personally I basically have this entire program ready in head, but I am not here to do other people's homework.. That kind of helps me learn some stuff, but that takes away the entire learning process from you.

The above list should help you get started.

Break down the program a little bit for yourself and tackle it in instances, instead of the whole thing at once. That should make it loads easier to complete.

I understand. I am not here for answers. I just need some help.

I have this so far

image.png.ef31e3b26dc5d662833d5073bc0d41e5.png

I have my random number picked. But how do I make sure they are not all the same? and also, should I put it in a while sequence or would that just mess things up?

 

Edited by xARACHN1D
Updated the picture
Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, xARACHN1D said:

I understand. I am not here for answers. I just need some help.

I have this so far

 

I have my random number picked. But how do I make sure they are not all the same? and also, should I put it in a while sequence or would that just mess things up?

 

How I would go about this is having a for loop where you create the numbers for a given array.

In this for loop, you will want to check your currently made random number against the already existing numbers in the array, which you might very well do via a while loop. Go play around with that idea!

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, minibois said:

How I would go about this is having a for loop where you create the numbers for a given array.

In this for loop, you will want to check your currently made random number against the already existing numbers in the array, which you might very well do via a while loop. Go play around with that idea!

So I think I have a set plan. I am going to code out a ton of if statements that if one randNum matches with another, it repicks that randNum. I tried doing this with the first one, and it didtn quite work out though.

image.png.38fd8373a2b604fe33a7e385df2e0d20.png

Link to comment
Share on other sites

Link to post
Share on other sites

26 minutes ago, xARACHN1D said:

ANYTHING HELPS!!!

First thing you should do is translate the description into psuedo code. From there it is easy.

 

public int[] getFiveNumbers(){ //return a list of 5 unique numbers}

 

public int compare(int[] set1, int[] set2){ //iterate through both sets to find & count matches, return count }

 

main{

int[] set1 = getFiveNumbers();

print( set1)

int[] set2 = getFiveNumbers();

print( set2)
int match_count = compare(set1, set2);

print( match_count)

}

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

23 minutes ago, Bacon soup said:

First thing you should do is translate the description into psuedo code. From there it is easy.

 

public int[] getFiveNumbers(){ //return a list of 5 unique numbers}

 

public int compare(int[] set1, int[] set2){ //iterate through both sets to find & count matches, return count }

 

main{

int[] set1 = getFiveNumbers();

print( set1)

int[] set2 = getFiveNumbers();

print( set2)
int match_count = compare(set1, set2);

print( match_count)

}

 

 

This has thoroughly confused me.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, xARACHN1D said:

So I think I have a set plan. I am going to code out a ton of if statements that if one randNum matches with another, it repicks that randNum. I tried doing this with the first one, and it didtn quite work out though.

 

I'm pretty sure I got the program here:

image.png.22f8da0e05c7e0d09cfd0d6aee23e5c3.png

(I just set the random number upper cap to 10, so get a more interesting result).

 

While I can obviously just give you the code outright, that would ruin the entire learning opportunity here..

I will post a bit of code here to just show you how to check if a number is unique:

 

// Add numbers to the array
for(int i = 0; i < amount; i++)
{
 int newNumber = random.nextInt(max);
 while(!uniqueNumber(newList, newNumber))
 {
  newNumber = random.nextInt(max);
 }
 newList[i] = newNumber;
}

This is how I kind of set up the main:

public static void main(String[] args) 
{
  // How many numbers in the lottery list?
  int amountOfNumbersInLottery = 5;
  int max = 10;
  
  // create array
  int[] lotteryNumbers = createRandomNumberArray(amountOfNumbersInLottery, max);
  // create second array
  int[] lotteryNumbers2 = createRandomNumberArray(amountOfNumbersInLottery, max);
  // check amount of matches against each other.
  int amountOfMatches = checkMatches(lotteryNumbers, lotteryNumbers2);
  
  // Print the lists and amountOfMatches
}

This should make it quite a bit easier to fill in the details.

 

To give you an idea, I only used a couple methods, couple for loops, 1 while loop and a couple if's and else's.

 

One bit of advice; place your ints into an array to easily compare them to each other.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, xARACHN1D said:

This has thoroughly confused me.

is your school not teaching you to create psuedo code?

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Bacon soup said:

is your school not teaching you to create psuedo code?

Ummm it kinda did. But not very well. I am doing homeschool. So pseudocode is kind of a lost concept to me. But I Do know about ifs elses and while loops and stuff

 

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, minibois said:

I'm pretty sure I got the program here:

image.png.22f8da0e05c7e0d09cfd0d6aee23e5c3.png

(I just set the random number upper cap to 10, so get a more interesting result).

 

While I can obviously just give you the code outright, that would ruin the entire learning opportunity here..

I will post a bit of code here to just show you how to check if a number is unique:

 


// Add numbers to the array
for(int i = 0; i < amount; i++)
{
 int newNumber = random.nextInt(max);
 while(!uniqueNumber(newList, newNumber))
 {
  newNumber = random.nextInt(max);
 }
 newList[i] = newNumber;
}

This is how I kind of set up the main:


public static void main(String[] args) 
{
  // How many numbers in the lottery list?
  int amountOfNumbersInLottery = 5;
  int max = 10;
  
  // create array
  int[] lotteryNumbers = createRandomNumberArray(amountOfNumbersInLottery, max);
  // create second array
  int[] lotteryNumbers2 = createRandomNumberArray(amountOfNumbersInLottery, max);
  // check amount of matches against each other.
  int amountOfMatches = checkMatches(lotteryNumbers, lotteryNumbers2);
  
  // Print the lists and amountOfMatches
}

This should make it quite a bit easier to fill in the details.

 

To give you an idea, I only used a couple methods, couple for loops, 1 while loop and a couple if's and else's.

 

One bit of advice; place your ints into an array to easily compare them to each other.

Ok this helps me insanely. Now I just need to set num caps and have a rearrange set. Thanks so much

 

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, xARACHN1D said:

Ummm it kinda did. But not very well. I am doing homeschool. So pseudocode is kind of a lost concept to me. But I Do know about ifs elses and while loops and stuff

 

its a paper concept. You break the problem down and draft the steps you want the program to take using descriptive words rather than actual code.

Link to comment
Share on other sites

Link to post
Share on other sites

Hey guys! All your help was so useful!!!

I will show you what I have so far. Tell me what you guys think...

image.png.77d220b2b6e252965e5b9d0cd3bf8a35.png

image.png.dfd42efa7dbbb3d8a9ad5f22e4610b72.png

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, xARACHN1D said:

Hey guys! All your help was so useful!!!

I will show you what I have so far. Tell me what you guys think...

Something a programmer should keep in mind in all times is:

1. Make functional programs

2. Make programs that can be expanded

 

Your solution probably works fine, but if you boss now says "great, now please make it work with 8 numbers per lottery list and with random numbers up to 80" you now have quite a task before you.

You first need to edit all 10 of these: 

image.png.cc101c780f37e7b8d62df8ea705fded1.png

You will also have to add a bunch of random number variables and add way more comparisons to your if-statement too.

That's a lot of work for 2 quite basic changes to the project.

 

For my project I would just have to edit 2 variables and it would work with those changes (those changes being the value of 2 variables that determine the amount of numbers per lottery and the max number limit). This not only makes it easier for me personally to edit, but also for any other programmer that will touch my code.

If you want to hear more about that and how companies view it, you could watch this video:

Quite an interesting video for any beginning aspiring programmer.

 

The sort of changes I would suggest to your code (if you want to improve it) are (in order from easiest to most difficult, although the last one is still quite do-able):

1. Create a variable for the max random number size, so instead of '+50' everywhere, you have '+SomeVariableYouMade' everywhere.

2. Place each set of random numbers in an array (or whatever sort of sorted list you have been taught to use)

3. Rework the if-statement to make use of the fact that you now have an array (hint hint: loop through the array with a for-loop).

4. Consider cutting your code into pieces, separate methods, that do the big changes. It's easier to read something like this:

public static void main()
{
  	int amountOfNumbersPerList = 5;
  	int highestValueForRandomNumber = 50;
  
	int[] LotteryNumberSetOne = CreateRandomNumberArray(amountOfNumbersPerList, highestValueForRandomNumber);
	int[] LotteryNumberSetTwo = CreateRandomNumberArray(amountOfNumbersPerList, highestValueForRandomNumber);
    
    	int amountOfMatches = CompareNumberSets(LotteryNumberSetOne, LotteryNumberSetTwo);
    
    	PrintResultOnScreen(amountOfMatches);
}

Than 100 lines of full on code..

This is mainly for easier reading, easier separation of code and easier adaptability to changes.

Making code this way does require a tiny bit of knowledge about return-methods, but it does make the code so much better IMO.

 

P.S. I see you have created a 'scanner' variable in your code. Is this perhaps something many other school assignments required to use? I seem to remember that from 1st year of school too.. using Scanner a lot..

Keep in mind you only need to use Scanner (and only need to import it at the top of your code) if you intend to use its feature; asking for user input. You aren't asking for user input in this piece of code, so there is no need to have it.

 

I would be happy to share some more code if you have any questions/uncertainties and wish you the best of fun in your further programming endeavors!

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Here's how I would do it:

 

Have two arrays, size 5 , these will hold the 5 randomly chosen numbers.

Have an array previouslyChosen that has 50 elements (or 51, if you start the array from 0) - you're gonna use this array as a temporary way to remember what random numbers were already picked and put in that collection of 5 numbers.

 

So basically this:

 

Set all those 50 elements in the array to 0 or false

 

for  counter from  1 to 5

  pick a random number between 1 and 50, put in variable randomNumber

  assume this number was already chosen so do a loop here and keep picking a random number until number was not chosen

  if the number is not already chosen, you don't even get inside the while, it jumps straight over it

  while previouslyChosen[randomNumber] = [ 1 or true ]

     pick another random number  : randomNumber = new random ( 1, 50)

  end while

  at this point, you know the number is for sure not already chosen, so put it in your array of 5 elements

  firstSet[counter] = randomNumber

  also, update the previouslyChosen array so that when you go to next number in the 5 elements you won't pick this number

  previouslyChosen[randomNumber] =  1 or true

end for

 

now repeat all this but with the second array of 5 elements. Make sure you reset all 50 values of the previouslyChosen to false or 0 before you start picking random numbers.

 

as for how many matches ... you do it like this ...

 

matchCount = 0

 

for firstCounter from 1 to 5

  for secondCounter from 1 to 5

     if  firstArray[firstCounter] = secondArray[secondCounter] then increase matchCount by 1

  end for secondCounter

end for firstCounter

 

 

Here's example code in PHP :

 

<?php

$set1 = array(0,0,0,0,0);
$set2 = array(0,0,0,0,0);

$picked = array(); // dynamic array, i'll use 0..50 and set 0 to true every time

$picked[0] = true; for ($i=1;$i<51;$i++) { $picked[$i] = false; }

$random = 0;
for ($i=0;$i<5;$i++) {
    // $random variable is 0 initially, and picked[0] was set to true, so while loop 
    // always happens at least once.
    while ($picked[$random] == true) {
        $random = mt_rand(1,50);
    }
    // I know for sure $random was not previously chosen, so put it in array
    $set1[$i] = $random;
    // now mark that number as already used so I won't pick it again for next numbers
    $picked[$random] = true;
    // I don't have to reset $random to 0, because picked[random] is now true, so the
    // while loop will happen at least once and another random number will be picked
}
// now clear the picked array (all but first 0 element) and repeat for next set
$picked[0] = true; for ($i=1;$i<51;$i++) { $picked[$i] = false; }
$random = 0;
for ($i=0;$i<5;$i++) {
    while ($picked[$random] == true) {
        $random = mt_rand(1,50);
    }
    $set2[$i] = $random;
    $picked[$random] = true;
}
// the two sets are now picked. 

// find the matches
$matchCount = 0;
for ($j=0;$j<5;$j++) {
    for ($i=0;$i<5;$i++) {
        if ($set1[$i]==$set2[$j]) $matchCount=$matchCount+1;
    }
}

echo $set1[0]." ".$set1[1]." ".$set1[2]." ".$set1[3]." ".$set1[4]."\n";
echo $set2[0]." ".$set2[1]." ".$set2[2]." ".$set2[3]." ".$set2[4]."\n";
echo "There are ".$matchCount." matches!\n";


?>

 

and this would output something like this :

 

d:\php\>php.exe program.php
1 30 3 22 5
23 6 37 27 14
There are 0 matches!

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×