Results 1 to 4 of 4
Thread: Finding a Smallest Integer
- 10-25-2012, 11:30 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Finding a Smallest Integer
This is the code I have so far that I am working on:
What this code does is print out an array of 5 integers with locations and different numbers in the locations. For example when I ran it this is what is printed out.Java Code:public class MixerUpper{ public static void main(String[] args){ int[] myArray = new int[5]; printIntArray(myArray); } public static void printIntArray(int[] myParamVar) { int LOCATION = 0; while(LOCATION<myParamVar.length) { System.out.print("In LOCATION " + LOCATION + " "); System.out.print("Computer has written "); LOCATION = LOCATION + 1; System.out.println(( ( int) ( 4.9999 * Math.random() ) ) ); } return; } }
> run MixerUpper
In LOCATION 0 Computer has written 4
In LOCATION 1 Computer has written 0
In LOCATION 2 Computer has written 4
In LOCATION 3 Computer has written 0
In LOCATION 4 Computer has written 0
>
Now what I need to do is create code so that after it prints out this information. It prints out the smallest integer in the array by first printing its location and then the actual smallest number in the array.
I don't really know where to start on this one as I am very new to programming. Can anyone help out a little?
- 10-26-2012, 12:45 AM #2
Re: Finding a Smallest Integer
So, first off, use counting loops (for loop) for lists of a known size - quite a bit easier to read and the syntax is more concise.
Second, you aren't actually storing the random numbers you're generating anywhere. You're blindly looping through an array, and then just making up numbers and printing them. I would think that a method called 'printIntArray()' would print numbers, not generate them.
- 10-26-2012, 02:08 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Finding a Smallest Integer
Alright well my professor may not be the best. But he gave us framework which was this:
He then tells us to stop the infinite loop ( which I did ) then he gave us that equation to use to generate the numbers. This is actually what im supposed to do. Print random numbers out in 5 different locations.Java Code:public*static*void*printIntArray(int[]*myParamVar) { ***int*LOCATION*=*0; ***while(*LOCATION*<*myParamVar.length) ***{ ******System.out.print("In*LOCATION*"*+*LOCATION*+*"**"); ******System.out.print("Rosie*has*written*"); ******System.out.println(*myParamVar[LOCATION]); ***}//Whoops..I*really*forgot*something,*and*was*too*lazy*to test.*You*fix*my*infinite*loop.*You*got*what*you*paid*for. ***return;** ***//Make*the**method*return*AFTER*printing*the*entire*array. }
It may sound stupid but this is the assignment, haha.
- 10-26-2012, 04:20 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 44
- Rep Power
- 0
Re: Finding a Smallest Integer
Ok from what I can see, it looks like there should be more code.
The printIntArray looks like it already assumes that each element in the array has data in it.
Somewhere in the assignment should be some code that populates each element of the array.
you would have probaby covered methods and return types, so I wont go into that, but leave that exercise for you to go over your notes and look into returning values from methods.
So it looks like you are on the right track (If the myParamVar already has data in it.)
Similar Threads
-
Finding the Smallest Element in an Array
By Cod in forum New To JavaReplies: 1Last Post: 02-20-2011, 02:05 AM -
Problem getting numbers from user and finding smallest two numbers
By radhi16 in forum New To JavaReplies: 11Last Post: 01-14-2011, 06:36 PM -
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05-17-2010, 12:50 AM -
finding max and min with inheritance with two input integer
By drmarx in forum New To JavaReplies: 3Last Post: 12-04-2009, 05:21 PM -
Finding largest and smallest integer
By mlhazan in forum New To JavaReplies: 2Last Post: 01-12-2008, 10:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks