Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

program for generating four random integers and finding third highest int  RSS feed

 
akshat khandelwal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

its execution gives error "variable tmax might not have been initialized" .Please help.
 
John Joe
Ranch Hand
Posts: 370
3
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
variable tmax might not have been initialized

You need to initialize tmax
 
Siva Rayapati
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You declared them, but you didn't initialize them. Initializing them is setting them equal to a value:
int a,b,c,d,max,min,tmax;
        a=(int)(Math.random()*100);
        b=(int)(Math.random()*100);
        c=(int)(Math.random()*100);
        d=(int)(Math.random()*100);
        tmax=0;  ------ this is important

You get the error because you haven't initialized the variables, but you are assigning some value to it in for loop

Local variables do not get default values. Their initial values are undefined with out assigning values by some means. Before you can use local variables they must be initialized.

There is a big difference when you declare a variable at class level (as a member ie. as a field) and at method level.

If you declare a field at class level they get default values according to their type. If you declare a variable at method level or as a block (means anycode inside {}) do not get any values and remain undefined until somehow they get some starting values ie some values assigned to them.
 
akshat khandelwal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.I was stuck in it for around 2 hours. Your explanation made me understand it.
 
John Joe
Ranch Hand
Posts: 370
3
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read more on Default Values and Initialization in Java to get better understanding.
 
Liutauras Vilda
Marshal
Posts: 4638
316
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akshat khandelwal, welcome to the Ranch.

After you fix your problem, I'd suggest you look for a better solution - more generic, so it would work not only with bounded limit of numbers, but with any amount of numbers.
Your current solution is quite complicated, can you imagine the complexity if you'd add let's say another 10 random numbers? I can't and don't want - probably would be too complex for any human being.

Try to build a loop construct and populate let's say an array with 10 or 20 random numbers. Then get pencil and solve this problem on a piece of paper. Then try out with code.

Solution which came first to my mind is 16 lines long and very basic.

Yours current solution even if it is correct, it is too complicated to me, so I'm doubted I'd solve it myself.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
consider using one of the primary tools you have - methods.  You want methods that each do one thing, and that don't care about anything else.

For example, at minimum, i would have a methods that creates a collection of random integers.  Perhaps you could pass it parameters to specifiy the range of the values, and how many to create.

Then, you'd have a separate method that gets handed a collection of numbers, and finds the third highest.  Again, perhaps it could be parameterized. You could pass in a value to say the third highest, or the 12th highest, or whatever. Maybe it could accept another parameter that allows you to find the third lowest value instead...But the important thing is that this method doens't care how the values were created. It only cares about finding the highest (or lowest, or whatever) in the collection.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!