Thread: What's wrong with Requirement 8?!*

1. Member
Join Date
Feb 2011
Posts
12
Rep Power
0

What's wrong with Requirement 8?!*

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
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[10];
int[] times = new int[10];
int count = 0;
boolean flag = false;

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

for (int j = 0; j < count && flag == false; j++)
{
if (values[i] == UniqueValues[j])
{
times[j]++;
flag = true;
}

if (values[i] != UniqueValues[j])
{
UniqueValues[j] = values[i];
times[j] = 1;
count++;
}
}
}

System.out.println("UniqueValues\t" + "Times occured");

for (int i = 0; i < count; i++)
{
System.out.println(UniqueValues[i] + "\t\t" + times[i]);
}
}
}

It is not printing anything from the very last 'for' statement...???

2. On step one you declared a variable, not a constant. Constants are generally final.

What have you done to try and understand this? What is the value of count before going to the loop? Print it and see what it is, maybe your logic in the above loop is incorrect and count is still 0.

To perform the counting your nested loop says j < count && flag == false, but j starts at 0, so how can it ever enter that loop and change the value of count? There are a lot of other logic errors you need to work on here. Also, please wrap your code in code tags

[ code]//don't put the space.
[/code]

A good approach would be to start small and work your way up, say you have an array, how would you count how many 3's are in the array? You can figure this up and scale it to count every number.
Last edited by sunde887; 04-22-2011 at 01:18 AM.

3. Member
Join Date
Feb 2011
Posts
12
Rep Power
0

okay...

//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;
boolean flag = false;

for (int i = 0; i < values.length; i++)
{
for (int j = 0; j < values.length && flag == false; j++)
{
if (values[i] == UniqueValues[j])
{
times[j]++;
flag = true;
}

else
{
UniqueValues[j] = values[i];
times[j] = 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]);
}

now it's printing values out, but under Unique Values, it's printing 10 '15's....and under times occurred, it's printing a '2' and 9 '1's......i can't figure out what i'm doing wrong!!!

4. You are using the same i but j loops x times, since the array is initially empty it never passes the first test and simply fills the array with values[i]. There are some more logic errors here that I am still thinking through, I may edit this post to add more information. However; try to continue working on the logic.

A set may be very helpful for unique values. Not sure if you are up to sets yet, but they dont allow duplicates, each element is unique. This can be done with arrays but it will be more challenging. Perhaps you should work on first creating an array of only unique ints, then use the unique array to count the values.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•