Jump to content

Hi, I'm doing an assignment and I have to create a program that generates a certain amount of random numbers (defined by the user) between 1-10, stores them in an array and then find out how many times each number showed up, which number showed up the most, and if more even numbers or more odd numbers showed up. This is the assignment that I have to do:

 

 

Col. Coin is holding his annual coin flipping contest, and to prepare for it he would like you to help him practice. Write Col. Coin a program that asks the user how many times he would like his coin to flip (he can flip up to 1000 times). Then randomly generate a number between 1 and ten, store all the numbers in an array. With your newly created array, supply Col. Coin with the following information:

  •  How many times each number out of 10 showed up.
  • What number showed up the most.
  • If even numbers are heads, and odd numbers are tails, what side of the coin came up the most?

Submit your program, make sure it is well commented.

***Bonus - Add another input for the Col. Asking him how many times he would like to perform his run through the flips (example, 3 flips of 30, 2 flips of 100) your output will now have a comparison between each trial. Try to incorporate as many arrays as possible!!! Submit a README.txt file along with your code to explain that you have completed the bonus.

 

This is what I've done so far:

import java.util.*;public class CoinFlipping {	public static void main(String[]args)	{		Scanner in = new Scanner(System.in);		int flips, i, num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0, num7 = 0, num8 = 0, num9 = 0, num10 = 0, popular;				System.out.println("How many times would you like to flip the coin?");		flips = in.nextInt();				int number[] = new int[flips];				for (i = 0; i < flips; i++)		{			number[i] = (int) ((Math.random() * 10) + 1);		}				for (i = 0; i < flips; i++)		{			switch (number[i]) {						case 1: num1++;					break;			case 2: num2++;					break;			case 3: num3++;					break;			case 4: num4++;					break;			case 5: num5++;					break;			case 6: num6++;					break;			case 7: num7++;					break;			case 8: num8++;					break;			case 9: num9++;					break;			case 10: num10++;					break;			}					}				System.out.println("\nNumber of 1(s): " + num1);		System.out.println("Number of 2(s): " + num2);		System.out.println("Number of 3(s): " + num3);		System.out.println("Number of 4(s): " + num4);		System.out.println("Number of 5(s): " + num5);		System.out.println("Number of 6(s): " + num6);		System.out.println("Number of 7(s): " + num7);		System.out.println("Number of 8(s): " + num8);		System.out.println("Number of 9(s): " + num9);		System.out.println("Number of 10(s): " + num10);

I'm not sure how to figure out which number showed up the most out of all of them, and also how to figure out if odd or even numbers showed up more. Any help would be greatly appreciated! Thanks.

Link to comment
https://linustechtips.com/topic/180529-java-help/
Share on other sites

Link to post
Share on other sites

I'm not sure how to figure out which number showed up the most out of all of them, and also how to figure out if odd or even numbers showed up more. Any help would be greatly appreciated! Thanks.

You can do multiple for loops. Make a for loop for each number 0-9 and iterate a count by one every time it appears in the array. Then just do multiple Math.max and output the greatest one. 

 

int zero=0; int one = 0;....int nine = 0; 

for (int i=0; i<array.length; i++){

if array==0 {zero++;}

if array==1 {one++;}

....

}

 

You can do the same thing for odd or even... 

 

int even=0; int odd=0; 

for (int i=0; i<array.length; i++){

if array % 2==0 {even++;}

else odd++;

}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/180529-java-help/#findComment-2424687
Share on other sites

Link to post
Share on other sites

You can do multiple for loops. Make a for loop for each number 0-9 and iterate a count by one every time it appears in the array. Then just do multiple Math.max and output the greatest one. 

 

int zero=0; int one = 0;....int nine = 0; 

for (int i=0; i<array.length; i++){

if array==0 {zero++;}

if array==1 {one++;}

....

}

 

You can do the same thing for odd or even... 

 

int even=0; int odd=0; 

for (int i=0; i<array.length; i++)

if array % 2==0 {even++;}

else odd++;

 

 

I'll leave you to figure out a more streamlined version of it. 

Alright thanks, that sounds good. I'll go try it out and see if I can get it to work.

Link to comment
https://linustechtips.com/topic/180529-java-help/#findComment-2424700
Share on other sites

Link to post
Share on other sites

pretty sure if you stuck them into an array, you could sort them by value. not sure how well that would work. maybe create an object and override toString. complicating things a bit but thats my style

Even if you sort the array you'd still have to go through each element and figure out how many times each appeared and how many were odd and even....i.e. sorting it doesn't really make things any easier. Maybe if it was hundreds of elements, you could sort it with mergesort and keep track of the current number; every time the number changed the counter would be stored in "previous max" and the new number would be iterated through and counted, then if it was greater than previous max, previous max would be overridden. 

 

As for turning it into a String....I can't see that being any easier to work with than an array--as iterating through a string is no easier than iterating through an array. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/180529-java-help/#findComment-2424742
Share on other sites

Link to post
Share on other sites

Even if you sort the array you'd still have to go through each element and figure out how many times each appeared and how many were odd and even....i.e. sorting it doesn't really make things any easier. Maybe if it was hundreds of elements, you could sort it with mergesort and keep track of the current number; every time the number changed the counter would be stored in "previous max" and the new number would be iterated through and counted, then if it was greater than previous max, previous max would be overridden. 

 

As for turning it into a String....I can't see that being any easier to work with than an array--as iterating through a string is no easier than iterating through an array. 

what i was thinking was more stuff the values into an array, sort, then iterate over the array and check if the value matches say num1 then if it does, print "Num 1 occured x times" etc

		int nums[] = new int[10];		int num1 = 0;				//assign				Arrays.sort(nums);				for(int i = nums.length-1; i>=0; i--)		{			if(nums[i] == num1)			{				System.out.println("Number 1 Occured " + String.valueOf(nums[i]) + " Times");				break;			}			//repeat for however many numbers needed		}
Link to comment
https://linustechtips.com/topic/180529-java-help/#findComment-2424748
Share on other sites

Link to post
Share on other sites

You can do multiple for loops. Make a for loop for each number 0-9 and iterate a count by one every time it appears in the array. Then just do multiple Math.max and output the greatest one. 

 

int zero=0; int one = 0;....int nine = 0; 

for (int i=0; i<array.length; i++){

if array==0 {zero++;}

if array==1 {one++;}

....

}

 

You can do the same thing for odd or even... 

 

int even=0; int odd=0; 

for (int i=0; i<array.length; i++){

if array % 2==0 {even++;}

else odd++;

}

This doesn't exactly work... it returns the highest amount but it doesn't return which number had that amount. So, if number 6 was the highest with 15 times, it would return 15 instead of 6. I need it to say 6.

Link to comment
https://linustechtips.com/topic/180529-java-help/#findComment-2424980
Share on other sites

Link to post
Share on other sites

			case 1: num1++;					break;			case 2: num2++;					break;			case 3: num3++;					break;			case 4: num4++;					break;			case 5: num5++;					break;			case 6: num6++;					break;			case 7: num7++;					break;			case 8: num8++;					break;			case 9: num9++;					break;			case 10: num10++;					break;			}					}				System.out.println("\nNumber of 1(s): " + num1);		System.out.println("Number of 2(s): " + num2);		System.out.println("Number of 3(s): " + num3);		System.out.println("Number of 4(s): " + num4);		System.out.println("Number of 5(s): " + num5);		System.out.println("Number of 6(s): " + num6);		System.out.println("Number of 7(s): " + num7);		System.out.println("Number of 8(s): " + num8);		System.out.println("Number of 9(s): " + num9);		System.out.println("Number of 10(s): " + num10);

looking at your code, you can see that there is quite a pattern: it's the same instruction copypasted 10 times, with just a number increasing in every line

 

the solution is indeed to use an array: you're using 10 counters, and every counter refers to a number between 1 and 10. how about you have an array of 10 counters?

int counters[] = new int[10]; // beware: indexes range from 0 to 9, so the counter for the number 1 will have index 0counters[0] = 5;      // the number 1 was counted 5 timescounters[6] = 200;    // the number 7 was counted 200 timescounters[3]++;        // count the number 4

this way, your whole switch statement can be swapped out for 1 line of code, and those 10 output instructions become 1 loop with 1 instruction

 

if you get how this logic works, the assignment becomes very simple

Link to comment
https://linustechtips.com/topic/180529-java-help/#findComment-2426579
Share on other sites

Link to post
Share on other sites

We can also use a HasMap to count occurrences:

    public static void main(String[] args)    {        int someArray[] = new int[] { 1, 1, 2, 2, 4, 1, 6, 1, 8, 1, 9, 2, 4, 4, 5 };        Map<Integer, Integer> occurrenceMap = new HashMap<Integer, Integer>();        for (int i = 0; i < someArray.length; i++)        {            int key = someArray[i];            if (occurrenceMap.containsKey(key))            {                int occurrence = occurrenceMap.get(key);                occurrence++;                occurrenceMap.put(key, occurrence);            }            else            {                occurrenceMap.put(key, 1);            }        }        Iterator iterator = occurrenceMap.keySet().iterator();        while (iterator.hasNext())        {            int key = (Integer)iterator.next();            System.out.println(key + " seen " + occurrenceMap.get(key) + " time(s).");        }    }

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
https://linustechtips.com/topic/180529-java-help/#findComment-2427262
Share on other sites

Link to post
Share on other sites

This is my code, without crucial parts of the algorithm. I cannot test the code at the moment, but I'm 90% sure this will run, even without the missing steps. Ask me any questions. Cheers.

import java.util.*;public class Count{    public static void main(String[] args){		int freq[] = new int[11];		int flip[];		int count;		int most=0, numOfMost=0;		int side[] = new int[2];		Scanner input = new Scanner(System.in);		System.out.println("How many times would you like to flip the coin?");		count = input.nextInt();		flip = new int[count];		for(int i=0; i<count; i++){			flip[i] = (int) ((Math.random() * 10) + 1);			//fill in logic for frequency of each number (1 line)			//fill in logic for evens and odds (1 line)			//fill in logic for number that showed up the most (1-4 lines)		}		//freebies, I guess		for(int i=1; i<freq.length; i++) System.out.printf("\nNumber of %d(s): %d", i, freq[i]);		System.out.printf("\nNumber that showed up most: %d", numOfMost);		System.out.printf("\nNumber of evens: %d, Number of odds: %d", side[0], side[1]);	}}
Link to comment
https://linustechtips.com/topic/180529-java-help/#findComment-2436749
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

×