Introduction

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 902 Views - Last Post: 07 October 2010 - 09:43 PM Rate Topic: -----

#1 xekret  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-October 10

Introduction

Posted 07 October 2010 - 04:36 PM

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]]++;

Is This A Good Question/Topic? 0
  • +

Replies To: Introduction

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Introduction

Posted 07 October 2010 - 04:42 PM

Please post your code in :code:

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 :)
Was This Post Helpful? 0
  • +
  • -

#3 xekret  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-October 10

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
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

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

Was This Post Helpful? 1
  • +
  • -

#5 xekret  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-October 10

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]);
	}
  }
}


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

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) {
Was This Post Helpful? 0
  • +
  • -

#7 xekret  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-October 10

Re: Introduction

Posted 07 October 2010 - 07:37 PM

I changed it an I'm still getting the same message from line 52.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Introduction

Posted 07 October 2010 - 07:39 PM

from

min = counts

to

min = counts[i]; // and don't forget the ;
Was This Post Helpful? 1
  • +
  • -

#9 xekret  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-October 10

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

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

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++;){
Was This Post Helpful? 1
  • +
  • -

#11 xekret  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-October 10

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.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

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 :)
Was This Post Helpful? 1
  • +
  • -

#13 xekret  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-October 10

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.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Introduction

Posted 07 October 2010 - 08:20 PM

View Postxekret, on 07 October 2010 - 09:16 PM, said:

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.

Repost revised version 5.6.7.8 of your code
Was This Post Helpful? 0
  • +
  • -

#15 xekret  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-October 10

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]);
	}
  }
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2