Hello I am a student and I am not very good with programming. I have a problem with my java assignment that I don't know what I am doing wrong. The instructor asked for the program to search the array for the lowest count. This is the code that I have that just counts the occurrences in the array.
// counting the occuranses of each number
public static int[] countNumbers(int[] numbers) {
// declaring and creating an array of 10 int
int[] counts = new int[10];
//for every number in the array count it
for (int i = 0; i < numbers.length; i++)
counts [numbers [i]]++;
18 Replies - 808 Views - Last Post: 07 October 2010 - 09:43 PM
Replies To: Introduction
#2
Re: Introduction
Posted 07 October 2010 - 04:42 PM
Please post your code in 
You will want a method that returns an integer, not an integer array. You want one variable that can keep up with the lowest number. Probably good to set it to the highest integer number possible. Then simply loop through the array and if the value is lower than your tracker variable, assign that value. Return the value at the end
You will want a method that returns an integer, not an integer array. You want one variable that can keep up with the lowest number. Probably good to set it to the highest integer number possible. Then simply loop through the array and if the value is lower than your tracker variable, assign that value. Return the value at the end
#3
Re: Introduction
Posted 07 October 2010 - 04:49 PM
I would appreciate a little demo because I am still a little lost after what you said =/ sorry
#4
Re: Introduction
Posted 07 October 2010 - 05:05 PM
// counting the occuranses of each number
public static int countNumbers(int[] numbers) {
// declaring and creating an array of 10 int
int[] counts = new int[10];
int min = 9999999;
//for every number in the array count it
for (int i = 0; i < numbers.length; i++) {
counts [numbers [i]]++;
}
for(int i = 0; i < 10; i++) {
if(counts[i] < min)
min = counts[i];
}
return min;
This post has been edited by pbl: 07 October 2010 - 05:08 PM
#5
Re: Introduction
Posted 07 October 2010 - 07:18 PM
Here is my code. It is telling me that there is an illegal start of expression where the min is declared. Any help is appreciated.
public class Program6 {
public static void main(String[] args) {
//declaring array and creating array
int[] numbers = createArray();
//array display
System.out.println("The 100 random numbers are ");
displayArray(numbers);
//count the occuranses of the numbers
int[] counts = countNumbers(numbers);
//display counts
System.out.println( " ");
System.out.println( "The occurranses of the least numbers are");
displayCounts(counts);
}
//creating array of numbers
public static int[] createArray() {
//declaring and creating an array of 100 random numbers
int[] numbers = new int[100];
//creating random numbers from 1 to 10 and assigning them to the array
for (int i = 0; i < numbers.length; i++)
numbers[i] = (int)(Math.random() * 10);
// returning the array
return numbers;
}
//displaying the array of numbers
public static void displayArray(int[] numbers) {
// displaying 10 numbers per line
for (int i = 0; i < numbers.length; i++) {
if ((i + 1) % 10 == 0)
System.out.println(numbers[i] + " ");
else
System.out.print(numbers[i] + " ");
}
}
// counting the occurranses of each number
public static int[] countNumbers(int[] numbers) {
// declaring and creating an array of 10 int
int[] counts = new int[10];
int min = 999999;
//for every number in the array count it
for (int i = 0; i < numbers.length; i++){
counts [numbers [i]]++;
}
for(int i=0; i < 10; i++;)/>{
if(counts[i] < min)
min = counts
}
return min;
}
// display counts
public static void displayCounts(int[] counts) {
for (int i = 0; i <= 9; i++) {
System.out.println( i + " | " + counts[i]);
}
}
}
#6
Re: Introduction
Posted 07 October 2010 - 07:21 PM
This is your method declaration
public static int[] countNumbers(int[] numbers) {
it says it will return an array of int: int[]
but min is int not an int[]
change to
public static int countNumbers(int[] numbers) {
public static int[] countNumbers(int[] numbers) {
it says it will return an array of int: int[]
but min is int not an int[]
change to
public static int countNumbers(int[] numbers) {
#7
Re: Introduction
Posted 07 October 2010 - 07:37 PM
I changed it an I'm still getting the same message from line 52.
#8
Re: Introduction
Posted 07 October 2010 - 07:39 PM
from
min = counts
to
min = counts[i]; // and don't forget the ;
min = counts
to
min = counts[i]; // and don't forget the ;
#9
Re: Introduction
Posted 07 October 2010 - 07:41 PM
Yea i noticed that before my last message and I changed it. I still got the same error message. This is how my code looks like now.
public class Program6 {
public static void main(String[] args) {
//declaring array and creating array
int[] numbers = createArray();
//array display
System.out.println("The 100 random numbers are ");
displayArray(numbers);
//count the occuranses of the numbers
int[] counts = countNumbers(numbers);
//display counts
System.out.println( " ");
System.out.println( "The occurranses of the least numbers are");
displayCounts(counts);
}
//creating array of numbers
public static int[] createArray() {
//declaring and creating an array of 100 random numbers
int[] numbers = new int[100];
//creating random numbers from 1 to 10 and assigning them to the array
for (int i = 0; i < numbers.length; i++)
numbers[i] = (int)(Math.random() * 10);
// returning the array
return numbers;
}
//displaying the array of numbers
public static void displayArray(int[] numbers) {
// displaying 10 numbers per line
for (int i = 0; i < numbers.length; i++) {
if ((i + 1) % 10 == 0)
System.out.println(numbers[i] + " ");
else
System.out.print(numbers[i] + " ");
}
}
// counting the occurranses of each number
public static int countNumbers(int numbers) {
// declaring and creating an array of 10 int
int[] counts = new int[10];
int min = 999999;
//for every number in the array count it
for (int i = 0; i < numbers.length; i++){
counts [numbers [i]]++;
}
for(int i=0; i < 10; i++;)/>{
if(counts[i] < min)
min = counts[i];
}
return min;
}
// display counts
public static void displayCounts(int[] counts) {
for (int i = 0; i <= 9; i++) {
System.out.println( i + " | " + counts[i]);
}
}
}
This post has been edited by xekret: 07 October 2010 - 07:43 PM
#10
Re: Introduction
Posted 07 October 2010 - 07:48 PM
You obviously have a problem identifying when to use array and when not using them 
from
public static int countNumbers(int numbers)
to
public static int countNumbers(int[] numbers)
also so ; after the i++ here
for(int i=0; i < 10; i++
{
from
public static int countNumbers(int numbers)
to
public static int countNumbers(int[] numbers)
also so ; after the i++ here
for(int i=0; i < 10; i++
#11
Re: Introduction
Posted 07 October 2010 - 07:57 PM
So that means to disregard the semicolon on for(int i=0; i < 10; i++
{ ? I have it in my code and when i take it off another error appears of incompatible types. Sorry for being a pain I'm trying to learn what to do and not to do. I appreciate your help.
#12
Re: Introduction
Posted 07 October 2010 - 08:02 PM
Another array/single problem ?
Probably here
int[] counts = countNumbers(numbers);
should be
int counts = countNumbers(numbers);
as countNumbers() returns an int not a int[]
then this one will probably generate an error if you fix the previous one
displayCounts(counts);
If you appreciate that much, click the "This post was helpful" some times
Probably here
int[] counts = countNumbers(numbers);
should be
int counts = countNumbers(numbers);
as countNumbers() returns an int not a int[]
then this one will probably generate an error if you fix the previous one
displayCounts(counts);
If you appreciate that much, click the "This post was helpful" some times
#13
Re: Introduction
Posted 07 October 2010 - 08:16 PM
You are right it returns an error that displayCounts(int[]) cannot be used with the program. I tried taking the [] from the line in the bottom and no luck.
#14
Re: Introduction
Posted 07 October 2010 - 08:20 PM
#15
Re: Introduction
Posted 07 October 2010 - 08:23 PM
public class Program6 {
public static void main(String[] args) {
//declaring array and creating array
int[] numbers = createArray();
//array display
System.out.println("The 100 random numbers are ");
displayArray(numbers);
//count the occuranses of the numbers
int counts = countNumbers(numbers);
//display counts
System.out.println( " ");
System.out.println( "The occurranses of the least numbers are");
displayCounts(counts);
}
//creating array of numbers
public static int[] createArray() {
//declaring and creating an array of 100 random numbers
int[] numbers = new int[100];
//creating random numbers from 1 to 10 and assigning them to the array
for (int i = 0; i < numbers.length; i++)
numbers[i] = (int)(Math.random() * 10);
// returning the array
return numbers;
}
//displaying the array of numbers
public static void displayArray(int[] numbers) {
// displaying 10 numbers per line
for (int i = 0; i < numbers.length; i++) {
if ((i + 1) % 10 == 0)
System.out.println(numbers[i] + " ");
else
System.out.print(numbers[i] + " ");
}
}
// counting the occurranses of each number
public static int countNumbers(int[] numbers) {
// declaring and creating an array of 10 int
int[] counts = new int[10];
int min = 999999;
//for every number in the array count it
for (int i = 0; i < numbers.length; i++){
counts [numbers [i]]++;
}
for (int i=0; i < 10; i++){
if(counts[i] < min)
min = counts[i];
}
return min;
}
// display counts
public static void displayCounts(int[] counts) {
for (int i = 0; i <= 9; i++) {
System.out.println( i + " | " + counts[i]);
}
}
}
|
|

New Topic/Question
Reply




MultiQuote




|