Results 1 to 7 of 7
Thread: Help!
- 04-28-2011, 03:56 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 12
- Rep Power
- 0
Help!
So, I have this program running almost perfectly. Requirement 8 is a little buggy tho. The unique values array is supposed to end up having all unique values in order without any empty elements between them. The empty elements should only be the last few elements. Any ideas what I need to change to make this run properly?
import java.util.Scanner;
public class ArrayPracticeKRC
{
public static void main (String [] args)
{
Scanner scan = new Scanner(System.in);
//1. Declare a constant named MAX_SIZE and set to 10
final int MAX_SIZE = 10;
//2. Declare an array of ints with capacity MAX_SIZE
int[] values = new int [MAX_SIZE];
//3. Prompt and read for 10 values and store in array
for (int i = 0; i < values.length; i++)
{
System.out.println("Enter and integer: ");
values[i] = scan.nextInt();
}
//4. Calculate and print the minimum values stored
int min = values[0];
for (int i = 0; i < values.length; i++)
{
if (values[i] < min)
min = values[i];
}
System.out.println("The minimum value is: " + min);
//5. Calculate and print the maximum values stored
int max = values[0];
for (int i = 0; i < values.length; i++)
{
if (values[i] > max)
max = values[i];
}
System.out.println("The maximum value is: " + max);
//6. Calculate and print the sum of all values stored
int sum = 0;
for (int i = 0; i < values.length; i++)
{
sum += values[i];
}
System.out.println("The sum of all the values is: " + sum);
//7. Calculate and print the average value
double avg = 0;
for (int i = 0; i < values.length; i++)
{
avg = (double) sum / values.length;
}
System.out.println("The average value is: " + avg);
//8. Calculate and print a list of all the values and the number of times each occurs
int[] UniqueValues = new int [MAX_SIZE];
int[] times = new int [MAX_SIZE];
int count = 0;
for (int i = 0; i < values.length; i++)
{
boolean found = false;
for (int j = 0; j < UniqueValues.length && found == false; j++)
{
if (values[i] == UniqueValues[j])
{
times[j]++;
found = true;
}
}
if (found != true)
{
UniqueValues[i] = values[i];
times[i] = 1;
count++;
}
}
System.out.println("UniqueValues\t" + "Times occured");
for (int i = 0; i < UniqueValues.length; i++)
{
System.out.println("\t" + UniqueValues[i] + "\t\t" + times[i]);
}
}
}
- 04-28-2011, 04:44 PM #2
Don't double post. Ask a moderator to move your original thread to a more appropriate forum.
db
- 04-28-2011, 04:50 PM #3
Don't double post. Ask a moderator to move your original thread to a more appropriate forum.
db
- 04-28-2011, 04:57 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Also, code tags are very helpful, I will post how to use them again for you.
[code ]<-omit space
YOUR CODE HERE
[/code]
Also, my suggestion to you is to split section 8 up. Instead of nesting and doing everything at once, it may be helpful to do one step at a time. Get the logic for one thing first, then worry about the next part.
Try this, first, generate all the unique values. Once you do that, you can count the occurrences in the original array. I am imagining sets are not allowed, however; using a set would greatly simplify generating of unique values(since sets do this automatically).
- 04-28-2011, 05:04 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 12
- Rep Power
- 0
I'm sorry for double posting. If I knew how to go about asking a moderator, i would. I don't even know how to access my posted threads from the home site (without searching through 'forums' for them). I figured there would be a link for 'my threads', but i can't find one. Again, I'm sorry.
- 04-28-2011, 05:06 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 12
- Rep Power
- 0
I want my output to look like this...
if my original array is:
8
7
10
15
15
10
5
2
10
8
I want my UniqueValues[] array and my times[] array to look like this...
UniqueValues---------times
8------------------2
7------------------1
10-----------------3
15-----------------2
5------------------1
2------------------1
0------------------0
0------------------0
0------------------0
0------------------0
- 04-28-2011, 05:09 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I suggest you read my post again, I gave you an approach which may help you with the logic.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks