hello,
I am having trouble with a program. It seems easy, but for some reason I am just not getting it. I need to create an array of random numbers and call a bubblesort method that I create myself. I then need to print the sorted array. I created the random array but I cant seem to figure out the sort method, which needs to follow in descending order. This is what I have: - public static int[] sorting(int vals[])
-
{
-
int i,j;
-
int max[] = new int [vals.length];
-
-
System.arraycopy(vals,0,max,0,vals.length);
-
-
for (i = 1; i < vals.length; i++)
-
{
-
for (j = 1; j < max.length; j++)
-
{
-
if (vals[j-1] > vals[j])
-
{
-
max[j-1] = vals[j];
-
max[j] = vals[j-1];
-
}
-
}
-
}
-
return max;
-
}
-
}
The message I am getting is the random numbers (the number of numbers is defined by the user and i printed the numbers just to see if it worked) along with the message [I@1eed786.
Please help,
Thanks
16 2378
You have two problems:
1) You are trying to print an array and get this: [I@1eed786; that's Java's way of
printing arrays: [ == array, I == int ,1ee786 == the hash code of the array. If you
want to print the contents of an array read the API documentation of the Arrays class.
2) Your bubble sort algorithm is like a Swedish Nilfisk vacuum cleaner: it s*cks
big times: it starts off nice by making a copy of the array but then you should
use *only* the copy for the comparisons and swaps.
kind regards,
Jos
i woul prefer if you post the hole code so i can test it.
i am not sure, but i think there is something with the sort algorithm (not sure). the i loop is good but with the j loop why dont you start with the last index of the array and go backward. it would be ;better.
please post the hole code so i can test it.
P.S. PLEASE USE THE CODE TAGS
hsn
a tip to save time. in the i loop and the j loop you are calling the length of the array in every time it loops. it is better to store the size in a variable and use the variable in the loop. you wont feel the difference. but trust me it matters.
good luck
hsn
[HTML]
import java.io.*;
public class Bubble
{
public static void main(String[] args)
throws java.io.IOException
{
String s1, convert;
int i, num;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
do
{
System.out.print("How many random numbers? ");
s1 = br.readLine();
num = Integer.parseInt(s1);
if (num > 9999 || num < 0)
System.out.println("Not valid.\n");
}
while(num > 9999 || num < 0);
System.out.println();
int mkrand[] = new int [num];
for(i = 0; i < num; i++)
{
mkrand[i] = 1 + (int)(Math.random()* 101);
System.out.print(mkrand[i] + " ");
}
System.out.print(sort(mkrand));
}
public static int[] sort(int vals[])
{
int i,j;
int max[] = new int [vals.length];
System.arraycopy(vals,0,max,0,vals.length);
for (i = 1; i < vals.length; i++)
{
for (j = 1; j < max.length; j++)
{
if (vals[j-1] > vals[j])
{
max[j-1] = vals[j];
max[j] = vals[j-1];
}
}
}
return max;
}
}[/HTML]
Here it is
You have two problems:
1) You are trying to print an array and get this: [I@1eed786; that's Java's way of
printing arrays: [ == array, I == int ,1ee786 == the hash code of the array. If you
want to print the contents of an array read the API documentation of the Arrays class.
2) Your bubble sort algorithm is like a Swedish Nilfisk vacuum cleaner: it s*cks
big times: it starts off nice by making a copy of the array but then you should
use *only* the copy for the comparisons and swaps.
kind regards,
Jos
I am not sure what you mean by API documentation. As you may tell, I'm pretty new to this stuff.
I am not sure what you mean by API documentation. As you may tell, I'm pretty new to this stuff.
API == Application Program Interface; the docs describe all the classes and their
methods that come with your Java distribution. You should use them whereever
you can and not reinvent the wheel. Here's a link.
kind regards,
Jos
i am shocked that you don't know about the API. it is the first thing i have learned in java. sownload it, it will help you alot..
hsn
i am shocked that you don't know about the API. it is the first thing i have learned in java. sownload it, it will help you alot..
hsn
Ok, I'm on it. But did you see any thing else wrong with my code?
hello m8.
as Jos said your bubble sort algorithm ******. it is not correct for
88 23 70 6 it gave me
23 88 6 70
you should do some reading about this algorithm and understand it well. do some search and you will understand it it is one of the simplest algorithms.
and you can't say - System.out.print(sort(mkrand));
you have to go in a loop to print the array.
Good luck
hsn
you have to go in a loop to print the array.
No he doesn't; that's what me fIrst remark was about (see my first reply).
He could simply do this: -
System.out.println(Arrays.toString(hisArray));
-
kind regards,
Jos
No he doesn't; that's what me fIrst remark was about (see my first reply).
He could simply do this: -
System.out.println(Arrays.toString(hisArray));
-
kind regards,
Jos
sorry m8 i didn't see that one
sorry m8 i didn't see that one
I tried this:
System.out.println(Arrays.toString(sorting(mkrand) ))
but I got the error message saying that it cannot find symbol
symbol: variable Arrays
I tried this:
System.out.println(Arrays.toString(sorting(mkrand) ))
but I got the error message saying that it cannot find symbol
symbol: variable Arrays
Oh, I also created a new a new selection sort method:
[HTML]
public static int[] sorting(int vals[])
{
for (int i = 0; i < vals.length; i++)
{
int index = i;
for (int j = i+1; j < vals.length; j++)
if (vals[j] > vals[index])
index = j;
int largerNumber = vals[index];
vals[index] = vals[i];
vals[i] = largerNumber;
}
return vals;
}[/HTML]
Can you tell me if this will give me the desired results? I still can't figure out how to print the array. I tried what was suggested but it still did not work.
I still can't figure out how to print the array. I tried what was suggested but it still did not work.
You have to import that class, just like every class not in the package java.lang.
This is how you do that: before you define any class add the line:
How did I know what package this Arrrays class is in? By reading the API docs.
kind regards,
Jos
You have to import that class, just like every class not in the package java.lang.
This is how you do that: before you define any class add the line:
How did I know what package this Arrrays class is in? By reading the API docs.
kind regards,
Jos
Thanks.
I did read it but I never found the part about converting to a String.
Thanks.
I did read it but I never found the part about converting to a String.
But in reply #13 you wrote:
I tried this:
System.out.println(Arrays.toString(sorting(mkrand) ))
but I got the error message saying that it cannot find symbol
symbol: variable Arrays
That simply indicates that the compiler couldn't find the 'Arrays' symbol, i.e. it
couldn't check whether or not there even *was* a method named toString(). That
should've fired up your attention: when a class symbol can't be found you either
misspelt its name or you have forgotten to import it.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: kathy |
last post by:
I have posted my problem before and still not feel confused. Anyone
know what is the problem?
In my Win32 dll, I defined:
#ifdef DllExport
#define UNMANAGED_API __declspec(dllexport)
#else...
|
by: magic9baller |
last post by:
I have to use bubblesort to sort a singly linked list. (Yes I am a n00b.) The tricky part is that I have to move pointers rather than moving the contents of one node to the other. I think my problem...
|
by: JavaStudent07 |
last post by:
If you know anything about "Bubble Sorts" please leave it here, the teacher said, "Use bubble sort to sort numbers into decreasing numerical value." Then she started eating lunch and said we were on...
|
by: Trent |
last post by:
Running this I see that on first run, both bubble and selection sort have 9
sort counts while insertion sort has ZERO. With a sorted list, should this
be ZERO for all?
Also bsort and Ssort have...
|
by: Trent |
last post by:
Still have problems with this thing.
Seems my results are not matching the "correct" example given.
The three sets of numbers below the last 3 columns is suppose to be the
number of comparisons...
|
by: AZRebelCowgirl73 |
last post by:
Ok I think I have added the bubbleSort methods correctly I am not getting any errors and the program runs as if I had not added them yet! My question is this! how would I now in the main method...
|
by: beacon |
last post by:
I'm writing a program as an assignment that takes 5 sorting algorithms and and tests for the amount of time and the number of comparisons it takes to um, sort an array.
I have run into some...
|
by: MLH |
last post by:
Is there any built-in FN for returning largest value
in a group of say 2 - 10 values? Lets say I want
a FN something like MaxVal(5, 7, 9, 3, 12) or
perhaps MaxVal(79,34). I would wanna see
return...
|
by: natvarara |
last post by:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#define MAX 30
void getData (FILE*,int,char,int,int,float,float,int,int*);
void...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |