is this considered an \"array\"? If not can you please help me fix my code so th
ID: 3634847 • Letter: I
Question
is this considered an "array"?If not can you please help me fix my code so that it is an array.
Thanks.
/*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.
*/
import java.util.Random;
public class array
{
public static void main( String args[] )
{
Random randomNumbers = new Random(); // random number generator
int frequency1 = 0, frequency2 = 0, frequency3 = 0, frequency4 =
0, frequency5 = 0, frequency6 = 0, frequency7 = 0, frequency8 = 0,
frequency9 = 0, frequency10 = 0;// stores indevidual frequencys
int number; // stores most recently generated value
// summarize results of 6000
for ( int col = 1; col <= 6000; col++ )
{
number = 1 + randomNumbers.nextInt( 10 ); // numbers from 1 to
10
// determine values 1-10 and increment appropriate counter
switch ( number )
{
case 1:
++frequency1; // increment the 1s counter
break;
case 2:
++frequency2; // increment the 2s counter
break;
case 3:
++frequency3; // increment the 3s counter
break;
case 4:
++frequency4; // increment the 4s counter
break;
case 5:
++frequency5; // increment the 5s counter
break;
case 6:
++frequency6; // increment the 6s counter
break;
case 7:
++frequency7; // increment the 7s counter
break;
case 8:
++frequency8; // increment the 8s counter
break;
case 9:
++frequency9; // increment the 9s counter
break;
case 10:
++frequency10; // increment the 10s counter
break;
}
}
System.out.println( "number Frequency" ); // output headers
System.out.printf( "1 %d 2 %d 3 %d 4 %d 5 %d 6 %d 7 %
d 8 %d 9 %d 10 %d ",
frequency1, frequency2, frequency3, frequency4,
frequency5, frequency6, frequency7, frequency8, frequency9,
frequency10 );
int even = 0, odd = 0;//stores even or odd totals
even = frequency2+frequency4+frequency6+frequency8+frequency10;//tally
of evens
odd = frequency1+frequency3+frequency5+frequency7+frequency9;//tally of
odds
System.out.println(" heads: "+even+" tails: "+odd);// prints out heads
and tails
System.out
}
}
Explanation / Answer
import java.util.Random;
public class array
{
public static void main( String args[] )
{
Random randomNumbers = new Random(); // random number generator
int frequency[10];
for(int i=0; i<10; i++)
frequency[i]=0;
int number; // stores most recently generated value
// summarize results of 6000
for ( int col = 1; col <= 6000; col++ )
{
number = 1 + randomNumbers.nextInt( 10 );
frequency[number]++;
}
System.out.println( "number Frequency" ); // output headers
for(int i=0; i<10; i++)
System.out.printf(" %d %d",(i+1),frequency[i]);
int even = 0, odd = 0;//stores even or odd totals
for(int i=0; i<10; i++)
{
if(i%2==0)
odd +=frequency[i];
else even+ = frequency[i];
}
System.out.println(" heads: "+even+" tails: "+odd);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.