• Post Reply Bookmark Topic Watch Topic
  • New Topic

comparing 2 arrays elements index against index  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, id just want to know if there is a need for a 3rd for loop in my program with variable index in order to compare 2 arrays elements index against index or do i need to change one of the loops ive already got. I kinda think i need a third loop but i just wanted to make sure. Im trying to create a lotteryprogram where the first array lotteryNumbers is supposed to randomly generate new numbers and the other array userNumbers is for user input. Then i need to compare if the numbers match in order for user to win, if not they loose. So i do know i need an if else statement later on but i just wanna know about this comparision. Id be thankful!

This is my code so far :


 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you don't need to assign elements of a newly created numeric array to zero; they already have that value by default.

In a single loop, you can assign the current index of lotteryNumbers to a random int and test the equality of that with an int obtained from user input, thus eliminating the need for the userNumbers array. You can break out of the loop when a mismatch is detected.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patricia Andersen wrote:Hi guys, id just want to know if there is a need for a 3rd for loop in my program with variable index in order to compare 2 arrays elements index against index or do i need to change one of the loops ive already got.

I'd say the first, because it's more flexible.

However, I really think you're hamstringing yourself by putting eveything in main(). It's really not a good habit to get into. For an alternative approach have a look at my MainIsAPain page.

This is the second or third thread that you've posted, and I don't get any sense that you've heeded previous advice to break problems down into small chunks and methods. Your main() method still looks like a stream of consciousness - and believe me, it's a bad way to program.

Even if you don't use the methodology suggested in the above link, at least read the StopCoding (←click→) and WhatNotHow pages.

HIH

Winston
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:First, you don't need to assign elements of a newly created numeric array to zero; they already have that value by default.

In a single loop, you can assign the current index of lotteryNumbers to a random int and test the equality of that with an int obtained from user input, thus eliminating the need for the userNumbers array. You can break out of the loop when a mismatch is detected.



Thanks a lot !!!
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Patricia Andersen wrote:Hi guys, id just want to know if there is a need for a 3rd for loop in my program with variable index in order to compare 2 arrays elements index against index or do i need to change one of the loops ive already got.

I'd say the first, because it's more flexible.

However, I really think you're hamstringing yourself by putting eveything in main(). It's really not a good habit to get into. For an alternative approach have a look at my MainIsAPain page.

This is the second or third thread that you've posted, and I don't get any sense that you've heeded previous advice to break problems down into small chunks and methods. Your main() method still looks like a stream of consciousness - and believe me, it's a bad way to program.

Even if you don't use the methodology suggested in the above link, at least read the StopCoding (←click→) and WhatNotHow pages.

HIH

Winston



All i asked for is if there is a need for a 3rd loop. And i am trying to break the problem down into small chunks which is why i wonder if there is a need for a 3rd loop.
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patricia Andersen wrote:
Winston Gutkowski wrote:
Patricia Andersen wrote:Hi guys, id just want to know if there is a need for a 3rd for loop in my program with variable index in order to compare 2 arrays elements index against index or do i need to change one of the loops ive already got.

I'd say the first, because it's more flexible.

However, I really think you're hamstringing yourself by putting eveything in main(). It's really not a good habit to get into. For an alternative approach have a look at my MainIsAPain page.

This is the second or third thread that you've posted, and I don't get any sense that you've heeded previous advice to break problems down into small chunks and methods. Your main() method still looks like a stream of consciousness - and believe me, it's a bad way to program.

Even if you don't use the methodology suggested in the above link, at least read the StopCoding (←click→) and WhatNotHow pages.

HIH

Winston





thanks for advice though
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patricia Andersen wrote:All i asked for is if there is a need for a 3rd loop. And i am trying to break the problem down into small chunks which is why i wonder if there is a need for a 3rd loop.

But:
  • You still have only one method, and it's static.
  • You're thinking about things like loops, which is the how, when what you should be concentrating on is understanding what needs to be done.

  • If you just want answers, you're probably better off with a site like Stackoverflow. Here, we try to help you.

    Winston
     
    Patricia Andersen
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    Patricia Andersen wrote:All i asked for is if there is a need for a 3rd loop. And i am trying to break the problem down into small chunks which is why i wonder if there is a need for a 3rd loop.

    But:
  • You still have only one method, and it's static.
  • You're thinking about things like loops, which is the how, when what you should be concentrating on is understanding what needs to be done.

  • If you just want answers, you're probably better off with a site like Stackoverflow. Here, we try to help you.

    Winston


    Ok you are right..but still even if i try to break it down into small pieces i still need to know the syntax and what should be used. Anyhow, i appriciate your help
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!