• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Manipulations with arrays!

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone! Could you please help me with this queston:

(b) Suppose a is an array of ints such that 0<= a[i]<= 9.

(i) Write a code fragment to determine how many elements of the array have
value 5.

(ii) Write a code fragment to determine how many elements of a have been
assigned each decimal digit, i.e. how many elements have been set to 0,
how many have been set to 1, etc.


I m prepering to exam now))
Thanks a lot!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Show us what you have so far, please.
 
Ibragim Gapuraev
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:Show us what you have so far, please.


I tried this:
i)
int[] list = {1,2, 4, 5, 6,5, 7, -2,9};
for(int i = 0; i<list.length;i++)
if(i == 5){
System.out.println(i);

but this gives me just the number, not quantity.
about (ii) I have no idea.>
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, that's a good start. You need to declare an int variable "count" before your loop, and then in the loop, instead of printing something, add one to "count". Then after the loop, print the value of "count" -- that'll be the answer.

Part two is just the same, except you need one variable like "count" for each of the ten digits. How could you store all ten variables easily?
 
Ibragim Gapuraev
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it! Thank you!)
 
Ibragim Gapuraev
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I solved it like this:
(i)


(ii)


[ EFH: Added code tags ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks great!
 
Marshal
Posts: 79236
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good stuff , but always use the .length field in a for loop. That way you are protected against out of bounds Exceptions.
 
reply
    Bookmark Topic Watch Topic
  • New Topic