• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Need help with my array

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have following task:
The method static int [] drawNumbers () generates an array of length 71, in whose cells the 20 randomly drawn numbers are stored, that on the corresponding index of the array is 1 and in the other places a 0. This array is returned

This is what I did:


I am a beginner and don't really know how to continue. Can someone help me?
 
Rancher
Posts: 4893
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

array of length 71, in whose cells the 20 randomly drawn numbers are stored,


Where are those 20 numbers stored in the array?

the corresponding index  


What is  the corresponding index?  For an array of 71 elements, the valid indexes would be 0 to 70.
 
Marshal
Posts: 76817
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hams Petr wrote:. . . I am a beginner and don't really know how to continue. . . .

Please explain what you are trying to do, and what the program is supposed to do. Is it a lottery program or something?
 
Sheriff
Posts: 17282
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my interpretation:  Create an array of int capable of holding 71 elements. Let's call this int[] picked. Randomly pick 20 numbers (or more probably, randomly pick numbers until you get 20 unique numbers. Anyway...) For each number picked, set its corresponding element in the int[] to 1. That is, if the number is 15 then picked[15] is set to 1. If the number is 27, then picked[27] is set to one. Any element that doesn't correspond to a picked number will be 0. Then return picked.

This is a goofy requirement. Why use int[] when a more appropriate type would be boolean[]? Better yet, just use a Set<Integer> and keep adding to it until its size is 20.
 
Junilu Lacar
Sheriff
Posts: 17282
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hams Petr wrote:I am a beginner and don't really know how to continue. Can someone help me?


Make sure you can articulate in plain everyday language exactly what it is you want to do. Pretend you're giving instructions to a 7-year-old child. How would you instruct them to do this? Until you can clearly lay out what the procedure is, it will be virtually impossible for you to lay out that procedure in Java.

To pick random numbers, browse through the java.util.Random class API documentation and see what an appropriate method might be for your purposes.
 
Let me tell you a story about a man named Jed. He made this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic