Working with Arrays

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

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

#1 Lillypop  Icon User is offline

  • D.I.C Head

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

Working with Arrays

Posted 14 July 2011 - 09:34 AM

Hello Everyone ...
Today I`m working with 3 programs all about arrays and I have few Qs... PLZ be updated with my topic because their are three programs and I`m working with number 1 now and so u see my other programs if i had any question or got stuck ;)

First my Program is to sort array of integers by reading the size of array and all numbers then sorting them in ascending order ((( HOWEVER I CANT SORT IT USING SORT METHOD AS WE DIDN`T STUDY IT BECAUSE WE DIDN`T STUDY IT ,, WE SHOULD USE ANOTHER WAY !))

HERE IS MY PROGRAM

import java.util.Scanner;


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

import java.util.Scanner;


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

		//a. make a scanner object
			Scanner Keyboard= new Scanner (System.in);

		
			int space;		

		
			System.out.println ("Enter the size of array");
			
		
			space =Keyboard.nextInt();

		
			int [] num= new int [space];

		
			int [] stored= new int [space];

		
			boolean [] check =new boolean [space];
			boolean check = false;  // my error is here that it says: Duplicate local variable check ((DO I CHANGE the name) i should set a boolean with 
			// the same size and to be defult

		
			System.out.println ("Enter the numbers");

		
			num [0] = Keyboard.nextInt();

		
			int p =0;

		while ( true ) {

	
			space[0]=smallest;  // <-- my error is The type of the expression must be an array type but it resolved to int





+ did i did it correct by setting the array space in the array num


Check my post because i have more than one program to write



:)
LILYPOP

Is This A Good Question/Topic? 0
  • +

Replies To: Working with Arrays

#2 ipushmycar  Icon 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 - 09:47 AM

Why do you have an array of booleans?

Why not simply have a boolean to see if it is sorted or not and an array to sort. You have so many variables and I really do not see the need.

How do you want to sort it? Search for the smallest swap it into the first spot? And continue?

What were your ideas?
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10784
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: Working with Arrays

Posted 14 July 2011 - 09:47 AM

Your errors are pretty self-explanatory. The first says duplicate local variables aren't allowed. You cannot have two variables with the same name in the same scope. You must change the name for one of them. For the second error, space is an int, not an array. So you cannot access space[0] b/c there is no space[0]. You have to assign an int to space.
Was This Post Helpful? 1
  • +
  • -

#4 Lillypop  Icon 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 - 10:10 AM

The Qs for the boolean says
//g. make an array of booleans , also with the same space (default will be all false)

and I'm going with what the professor is asking, all these variables the professor asked for them
Was This Post Helpful? 0
  • +
  • -

#5 Lillypop  Icon 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 - 10:20 AM

Their is a qs that i don't understand it
it says /make an integer smallest set to -1 ( it will be the position of the first number in start that has not been moved )

What I did is :

int smallest= -1;
size[0]=smallest;


but under the the array size it gave me an error which says :The type of the expression must be an array type but it resolved to int

Was This Post Helpful? 0
  • +
  • -

#6 ipushmycar  Icon 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 - 10:26 AM

What is the purpose of an array of booleans?

Your infinite while loop is missing a curly brace.

This is how I would lay it out.

Check a function to check to see if the array is sorted.

public boolean isSorted(int [] a)
{
	test to see if the array is sorted
	for 1 to length of a
		if a[i-1] > a[i]
			return false
	return true
}




from here I would use your boolean to see if it is sorted.

if !isSorted(array)
    sort the array
else done //print out the array?



View PostLillypop, on 14 July 2011 - 10:20 AM, said:

Their is a qs that i don't understand it
it says /make an integer smallest set to -1 ( it will be the position of the first number in start that has not been moved )

What I did is :

int smallest= -1;
size[0]=smallest;


but under the the array size it gave me an error which says :The type of the expression must be an array type but it resolved to int


I do not know what a qs is.

size is not an array so you cannot use subscripts([#]) to try to access data.
Was This Post Helpful? 1
  • +
  • -

#7 Lillypop  Icon 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 - 10:42 AM

But I can't use sorting in class because we didn't took it
Was This Post Helpful? 0
  • +
  • -

#8 ipushmycar  Icon 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 - 10:50 AM

View PostLillypop, on 14 July 2011 - 10:42 AM, said:

But I can't use sorting in class because we didn't took it


I am not telling you to use the built in functions I am telling you to think of a way to sort it.

Maybe find the smallest integer in the array put it to the beginning and continue this process until its sorted?
Maybe compare the first two see which is bigger if the first is better swap the first and the second and continue till you get to the end.

There are many ways to sort an array.
Was This Post Helpful? 1
  • +
  • -

#9 Lillypop  Icon 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 - 11:00 AM

I had fixed the boolean +

ipushmy car I'm stuck in a question that says:

Quote

make a for loop over the moved array, set smallest to the position of the first element that is false (use a break)


I don't get what he means moved array??


+ the program is to sort integers from ascending to largest without the sorting class

Was This Post Helpful? 0
  • +
  • -

#10 ipushmycar  Icon 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 - 11:08 AM

What I think it is asking you to do is this.

  • Find the smallest element and move it to the first spot in the array (array[0])
  • Move the element that previously was in the first element and move it to were the smallest was
    EX: array = 5 2 3 4 1 smallest is 1 so switch with five. array = 1 2 3 4 5 (write a swap function)
  • Set the first element in the boolean array to True since it is now in the correct location
  • Increment smallest since we now have 1 sorted element (array[0])
  • repeat these steps until that array is sorted.

Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10784
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: Working with Arrays

Posted 14 July 2011 - 11:09 AM

This is called Selection Sort. I have a basic tutorial on selection sort you might want to check out: http://www.dreaminco...selection-sort/
Was This Post Helpful? 0
  • +
  • -

#12 ipushmycar  Icon 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 - 11:11 AM

But again I do not now what a qs is...

This post has been edited by ipushmycar: 14 July 2011 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

#13 Lillypop  Icon 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 - 11:13 AM

View Postipushmycar, on 14 July 2011 - 11:08 AM, said:

What I think it is asking you to do is this.

  • Find the smallest element and move it to the first spot in the array (array[0])
  • Move the element that previously was in the first element and move it to were the smallest was
    EX: array = 5 2 3 4 1 smallest is 1 so switch with five. array = 1 2 3 4 5 (write a swap function)
  • Set the first element in the boolean array to True since it is now in the correct location
  • Increment smallest since we now have 1 sorted element (array[0])
  • repeat these steps until that array is sorted.



YES ,, the Exact qs is :

10) Write a program to sort an array of integers. You must read in from the user the following using Scanner: i) the size of the array and ii) all the numbers. Then you should print out the sorted array in ascending order (from the smallest number he gave you to the largest).

We did not do sorting in class, but you know how to find the largest/smallest value in an array and you can repeatedly find the smallest and move it to another array – this will eventually sort it.

Use 3 arrays to answer this question: 1) the orginal array to hold all the individual numbers 2) an array of booleans the same size as your original array, initially all set to false (it will indicate whether its corresponding number in the original array has been moved or not – if you move a number you can set its corresponding boolean value to true (so you don’t move it more than once) 3) a sorted array the same size as the orginal array and initially empty (or set to all zeros)


View Postmacosxnerd101, on 14 July 2011 - 11:09 AM, said:

This is called Selection Sort. I have a basic tutorial on selection sort you might want to check out: http://www.dreaminco...selection-sort/



Thanks I'm checking it + i post the qs in my previous post :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#14 ipushmycar  Icon 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 - 11:13 AM

Okay, so try to do it and post your questions and new code.
Was This Post Helpful? 1
  • +
  • -

#15 Lillypop  Icon 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 - 11:17 AM

What I have remaining to do in this qs is to :

 make a for loop over the moved array, set smallest to the position of the first element that is false (use a break)
			
for ( )

if smallest remains -1 , that means there are no more to move - end the while loop with a break


 make a for loop over the space array.. skip the elements that have moved set to true
 at the end of the loop, smallest should point to the position of the smallest one


set the boolean value of smallest to true in the moved array


put the smallest element you found into the sorted array at position pos, increment pos

		}

 print out the numbers in sorted (they should be sorted)


View Postipushmycar, on 14 July 2011 - 11:13 AM, said:

Okay, so try to do it and post your questions and new code.



Ok .. i'll be writing the last part of this qs cuz i have to more to go of programs:(
everyday 3 programs to do :(
Was This Post Helpful? 0
  • +
  • -

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