Working with Arrays

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

48 Replies - 10434 Views - Last Post: 16 July 2011 - 04:58 AM Rate Topic: -----

#31 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,880
  • Joined: 27-December 08

Re: Working with Arrays

Posted 14 July 2011 - 12:28 PM

Try debugging based on what I told you in my previous post:

Quote

We're quite happy to help, but think about things a little. Most of your errors are from your naming conventions. You can't have two variables with the same name in the same scope. You also can't treat a variable as an array if you don't declare it as an array. So if x is an int, you cannot access x[0]. Also, you must declare and initialize variables before you can use them.

Regarding the sort, I provided a tutorial, and your instructor basically told you what to code. Put in a good faith effort into solving it.


Where is the array sorted declared? Perhaps this is a typo and you mean to use another array.
Was This Post Helpful? 0
  • +
  • -

#32 Lillypop   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 11-July 11

Re: Working with Arrays

Posted 14 July 2011 - 12:29 PM

OK....


At the beginning it is declared as what the qs was asking

make another array of integers called sorted and set its size to the same size
			int [] stored= new int [size];

Was This Post Helpful? 0
  • +
  • -

#33 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,880
  • Joined: 27-December 08

Re: Working with Arrays

Posted 14 July 2011 - 12:30 PM

See the problem now?
int [] stored= new int [size];

...
for ( int i = 0 ;  sorted.length-1 ; i++ ) {  



Also, at this point, this more than applies:
Posted Image
Was This Post Helpful? 2
  • +
  • -

#34 Lillypop   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 11-July 11

Re: Working with Arrays

Posted 14 July 2011 - 12:35 PM

oops !!
The spelling + all this is happening from time
but still i have an error in the for loop

for ( int i = 0 ;  sorted.length-1 ; i++ ) {


Its giving me:
Type mismatch: cannot convert from int to boolean
Was This Post Helpful? 0
  • +
  • -

#35 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,880
  • Joined: 27-December 08

Re: Working with Arrays

Posted 14 July 2011 - 12:49 PM

You need a boolean condition in the middle section of your loop. What do you want to compare sorted.length-1 to?
Was This Post Helpful? 0
  • +
  • -

#36 Lillypop   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 11-July 11

Re: Working with Arrays

Posted 14 July 2011 - 01:28 PM

TIME IS UP :( ^-^ ^-^
Was This Post Helpful? 0
  • +
  • -

#37 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,880
  • Joined: 27-December 08

Re: Working with Arrays

Posted 14 July 2011 - 01:32 PM

In the mean time, I highly recommend checking out the DIC Java Tutorials Section. NeoTifa has some excellent introductory tutorials, and TriggaMike has an excellent tutorial on arrays. You may find those helpful.
Was This Post Helpful? 1
  • +
  • -

#38 Lillypop   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 11-July 11

Re: Working with Arrays

Posted 14 July 2011 - 01:34 PM

MY other Program

I don't understand how to find the smallest integer:

import java.util.Scanner;


public class Q56 {
	public static void main(String[] args) {

		//a. copy and paste the first part of your code from Q1).. upto reading in the values of start array
		//a. make a scanner object
		Scanner Keyboard= new Scanner (System.in);

	//b. make an int variable called size
		int size;		

	//c. print out 'enter the size of array'
		System.out.println ("Enter the size of array");
		
	//d. read in the size as an integer
		size =Keyboard.nextInt();

	//e. make an array of integers called start and set its size to the variable you read in
		int [] start= new int [size];

	//f. make another array of integers called sorted and set its size to the same size
		int [] stored= new int [size];

	//g. make an array of booleans , also with the same size (default will be all false)
		boolean [] check =new boolean [size];
		  

	//h. print out please enter the numbers
		System.out.println ("Enter the numbers");

	//i. read in the integers and assign them to your array start
		start [0] = Keyboard.nextInt();

		//b. find the smallest integer in the start array

		
		//c. find the biggest integer in the start array


		//d. find the average of all the integers in the start array


		//e. print them out
		System.out.println (" ");


View Postmacosxnerd101, on 14 July 2011 - 01:32 PM, said:

In the mean time, I highly recommend checking out the DIC Java Tutorials Section. NeoTifa has some excellent introductory tutorials, and TriggaMike has an excellent tutorial on arrays. You may find those helpful.



THANKS :)
I saved them in my favourite list & i'll be checking them as soon as i finish the other project :(
I will be getting 0 at the first one because its wrong,, therefore i don't want the second and the third to be the same :(
Was This Post Helpful? 0
  • +
  • -

#39 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,880
  • Joined: 27-December 08

Re: Working with Arrays

Posted 14 July 2011 - 01:37 PM

View PostLillypop, on 14 July 2011 - 03:25 PM, said:

first problem :
   for ( int i = 0 ;  sorted.length-1 ; i++ ) {  

  if  ( smallest> stored [i])  

           smallest=stored [0];  
                
 }  



the problem is with the word sorted in the for loop .. it says "sorted cannot be resolved"

You've almost got it here. Logically, stored[0] should be stored[i]. So if the current element is less than the smallest element, assign it to smallest rather than the first element. Then you just have the syntax issues to deal with that you encountered in your last program.
Was This Post Helpful? 1
  • +
  • -

#40 Lillypop   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 11-July 11

Re: Working with Arrays

Posted 14 July 2011 - 01:45 PM

But i don't now why sorted.length still give me and error :whatsthat:
what does it mean by the boolean?!!! :online2long:
Was This Post Helpful? 0
  • +
  • -

#41 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12691
  • View blog
  • Posts: 45,880
  • Joined: 27-December 08

Re: Working with Arrays

Posted 14 July 2011 - 01:45 PM

View Postmacosxnerd101, on 14 July 2011 - 03:49 PM, said:

You need a boolean condition in the middle section of your loop. What do you want to compare sorted.length-1 to?

I'm sure you are familiar with the primitive boolean datatype. In fact, I've seen you use it before. Please re-read my previous post.
Was This Post Helpful? 0
  • +
  • -

#42 ipushmycar   User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: Working with Arrays

Posted 14 July 2011 - 01:46 PM

How to find the smallest:

Think, you have this data structure (an array) that holds all these numbers. You know how to compare things (if statements), you know how to go through an array (loops).

HINT:
Have a variable called smallest equal the first element in the array (array[0])
Go through the array using a loop
compare each element to smallest
if smallest > array [i]
smallest = array[i]
Was This Post Helpful? 3
  • +
  • -

#43 Lillypop   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 11-July 11

Re: Working with Arrays

Posted 14 July 2011 - 02:00 PM

I CAN'T THINK ANY MORE ,,, I'M REALLY SWITCHED OFF NOW
MORE THAN FIVE HOURS WORKING WITH THESE PROGRAM + THE PROFESSOR IS NOT ANSWERING MY EMAIL :( AND TIME IS RUNNING AND POINTS WILL BE LOOSED FROM LATE SUBMISSION ... I NEED TO DO WELL IN THIS PROJECT TO GET A PASS
CUZ ITS 50% OF MY GRADE


I HAVE TO 100% , ONE 93% AND ONE 80%


I THINK I'LL SLEEP NOW BECAUSE I'M USELESS NOW :( :blink: :pinch:

View Postipushmycar, on 14 July 2011 - 01:46 PM, said:

How to find the smallest:

Think, you have this data structure (an array) that holds all these numbers. You know how to compare things (if statements), you know how to go through an array (loops).

HINT:
Have a variable called smallest equal the first element in the array (array[0])
Go through the array using a loop
compare each element to smallest
if smallest > array [i]
smallest = array[i]



i GOT U :bananaman:
Ill try it now :dozingoff:
Was This Post Helpful? 0
  • +
  • -

#44 Lillypop   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 11-July 11

Re: Working with Arrays

Posted 14 July 2011 - 02:11 PM

Here what i did

	find the smallest integer in the start array
			for ( int i =0; start.length ; i++){
				int smallest;
				if (smallest > start [i])
				smallest = start [i];
}

By still i get the same problem with start.length


it saaays:
Type mismatch: cannot convert from int to boolean
Was This Post Helpful? 0
  • +
  • -

#45 Lillypop   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 11-July 11

Re: Working with Arrays

Posted 14 July 2011 - 02:19 PM

		//b. find the smallest integer in the start array
			for ( int i =0; start.length ; i++){
				int smallest;
				if (smallest > start [i])
				smallest = start [i];
			
		
		//c. find the biggest integer in the start array
			else{
				if ( smallest < start [i])
					smallest=start [i];
			}

		//d. find the average of all the integers in the start array
		double average= start.length - smallest;
		//e. print them out
		System.out.println ( average);


My error is at the for loop only !
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4