13 Replies - 759 Views - Last Post: 21 October 2013 - 08:27 PM Rate Topic: -----

#1 MrDoobs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 21-September 13

Seeking help with arrays

Posted 21 October 2013 - 01:12 PM

I am currently experiences difficulties with this program I am trying to write. The program is a voting system that tallies votes for candidates #0-9. Candidates 0-9 are stored in the first array while the second array is instantiated to user input. The program output of candidates 0-9 prints accordingly but the second array set to user input is what I am having trouble with. The second array needs to calculate the votes entered and tally them through the counter so they are lined up next to their respective candidate. This is also my first time using arrays so my code may be totally wrong!

*I am not looking for the answer but to be guided down the right path*

Thank you.

import java.util.*;
public class VoteTally{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        
        int[]cands=new int[]{0,1,2,3,4,5,6,7,8,9};
        int[]votes=new int[40]; 
        final int FLAG = -999;
        int count = 0;
        
        System.out.println("Enter votes #0-9 for the Candidates running for mayor... Enter "+FLAG+" to stop voiting");
        for(int i=0; i<9; i++){ // Array set to the candidates 0-9.
            for(int j=0; j<40; j++){ // Array to store user input to be calculated to a total of number 1's entered etc. 
                votes[j]=input.nextInt();
                if(votes[j]==1){ // Not sure if this is right, but this is how I am trying to count how many 1's were entered etc.
                   count+=cands[i];
                }
            }
        }
        for(int i=0; i<10; i++)
        System.out.println(cands[i]+ " " +count);
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Seeking help with arrays

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7996
  • View blog
  • Posts: 13,694
  • Joined: 19-March 11

Re: Seeking help with arrays

Posted 21 October 2013 - 01:24 PM

I don't see why you have two arrays. One array of 10 elements should be enough: just register each candidate's votes in the slot corresponding to their ID number. If I vote for 6, increment slot # 6, and so forth.

If you wanted to keep track of other data about the candidates, such as their names or party affiliations perhaps, you might think of having an array of Strings, so now I can find out that candidate # 6's name is Jones, but you don't seem to be very concerned about that at this point.
Was This Post Helpful? 1
  • +
  • -

#3 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,539
  • Joined: 08-April 09

Re: Seeking help with arrays

Posted 21 October 2013 - 01:28 PM

Yes, kinda like this?
cands[5] = 10; //sixth candidate has 10 votes(has been voted for 10 times)


Quote

If you wanted to keep track of other data about the candidates, such as their names or party affiliations perhaps, you might think of having an array of Strings, so now I can find out that candidate # 6's name is Jones

Wouldn't HashMap be better at this
Map<Int, String> dictionary = new HashMap<Int, String>();
dictionary.put(6, "Jones");

This post has been edited by Michael26: 21 October 2013 - 01:33 PM

Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7996
  • View blog
  • Posts: 13,694
  • Joined: 19-March 11

Re: Seeking help with arrays

Posted 21 October 2013 - 01:30 PM

more like
int voter_choice = getInputBySomeMeans();
cands[voter_choice] ++;  // register this vote

Was This Post Helpful? 1
  • +
  • -

#5 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,539
  • Joined: 08-April 09

Re: Seeking help with arrays

Posted 21 October 2013 - 01:38 PM

Yes, that is what i meant cands[5], 5 comes as result from getInputBySomeMeans(); that will be voters choice, then cands[voter_choice] ++; will increment vote once for that candidate, in my example it incremented 10 times. 10 voters voted for candidate number 5.

This post has been edited by Michael26: 21 October 2013 - 01:39 PM

Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7996
  • View blog
  • Posts: 13,694
  • Joined: 19-March 11

Re: Seeking help with arrays

Posted 21 October 2013 - 01:44 PM

View PostMichael26, on 21 October 2013 - 03:28 PM, said:

Yes, kinda like this?
cands[5] = 10; //sixth candidate has 10 votes(has been voted for 10 times)


Quote

If you wanted to keep track of other data about the candidates, such as their names or party affiliations perhaps, you might think of having an array of Strings, so now I can find out that candidate # 6's name is Jones

Wouldn't HashMap be better at this
Map<Int, String> dictionary = new HashMap<Int, String>();
dictionary.put(6, "Jones");



If the key is an integer, and the integers are closely-packed, then no, I'd use an array (perhaps using an offset, if the id numbers are, say 1000-1010) to avoid the auto-boxing penalty.

Of course, the question is sort of moot, since I'd actually create an object to represent a candidate, and do this in a more OO style all around, but since I'm talking to someone who's just learning arrays, I'm going to help them understand arrays before I try to shove them on to more complex structures.

Quote

Yes, that is what i meant cands[5], 5 comes as result from getInputBySomeMeans(); that will be voters choice, then cands[voter_choice] ++; will increment vote once for that candidate, in my example it incremented 10 times. 10 voters voted for candidate number 5.


Well, in your example, cand[5] gets mistakenly set to 10, no matter how many votes were cast. We're counting here: you update by one at a time. Also, since the candidates are numbered 0-9, candidate #5 isn't even germane to the issue. The voter voted for candidate #6, so you want to increment candidate # 6's tally.

This post has been edited by jon.kiparsky: 21 October 2013 - 01:45 PM

Was This Post Helpful? 1
  • +
  • -

#7 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,539
  • Joined: 08-April 09

Re: Seeking help with arrays

Posted 21 October 2013 - 01:54 PM

Yes, i'm off by one, also i know we are counting by one, my example at #3 was bit rushed decision, i was trying to depict that all candidates started at 0 and cand[4] has been voted for 10 times already, it has been updated one at a time 10 times. It should be cand[4] for 5th candidate.
Was This Post Helpful? 1
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7996
  • View blog
  • Posts: 13,694
  • Joined: 19-March 11

Re: Seeking help with arrays

Posted 21 October 2013 - 02:11 PM

No, it's cand[5] for the candidate identified by ID number "5".

Quote

The program is a voting system that tallies votes for candidates #0-9


Even if you didn't have a candidate #0, it would still make sense to leave 0 empty and avoid the extra arithmetic step. But here, you have a candidate #0, so there's no offset at all.


As for the setting to 10, just leave it. It was a mistake, let it go.
Was This Post Helpful? 2
  • +
  • -

#9 MrDoobs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 21-September 13

Re: Seeking help with arrays

Posted 21 October 2013 - 02:17 PM

I see what your saying to a point Mr.Jon but I am not fluent in Java yet. The printout I get at the moment with what I can understand so far is... Where I am confused is what would be good to take user input as I set it to each index of the candidate array.
Thanks again for your time...

Enter votes #0-9 for the Candidates running for mayor... Enter -999 to stop voiting
1
1
1
1
1
1
1
1
1
1
1 10
1 10
1 10
1 10
1 10
1 10
1 10
1 10
1 10
1 10
[\code]

[code]
import java.util.*;
public class VoteTally{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        
        int[]cands=new int[10];
        int[]votes=new int[40]; 
        final int FLAG = -999;
        int count = 0;
        
        System.out.println("Enter votes #0-9 for the Candidates running for mayor... Enter "+FLAG+" to stop voiting");
        for(int i=0; i<10; i++){ // Array set to the candidates 0-9.
            //for(int j=0; j<40; j++){ // Array to store user input to be calculated to a total of number 1's entered etc. 
                cands[i]=input.nextInt();
            if(cands[i]==1){ // Not sure if this is right, but this is how I am trying to count how many 1's were entered etc.
                count+=cands[i];
                   
            }
        }
        for(int i=0; i<10; i++)
        System.out.println(cands[i]+ " " +count);
        }
      
    }


Was This Post Helpful? 0
  • +
  • -

#10 MrDoobs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 21-September 13

Re: Seeking help with arrays

Posted 21 October 2013 - 02:34 PM

My apologies for littering my last post. Here is the readable code I was referring to from your first reply. Also I understand a bit of the different methods that can be used instead of trying to do this in just the main method. But unfortunately my professor wants it done in the main method. Where I am confused is what I use to read the input and then compare it to the array indexes(not sure if this makes sense). Also there is not a static number of votes to cast; that all depends on when the user uses the FLAG(which I have not implemented yet).

import java.util.*;
public class VoteTally{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        
        int[]cands=new int[10]; 
        final int FLAG = -999;
        int count = 0;
        
        System.out.println("Enter votes #0-9 for the Candidates running for mayor... Enter "+FLAG+" to stop voiting");
        for(int i=0; i<10; i++){ // Array set to the candidates 0-9. 
            cands[i]=input.nextInt();
            if(cands[i]==1){ // Not sure if this is right, but this is how I am trying to count how many 1's were entered etc.
                count+=cands[i];
            }
        }
        for(int i=0; i<10; i++)
        System.out.println(cands[i]+ " " +count);
    }
}



Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7996
  • View blog
  • Posts: 13,694
  • Joined: 19-March 11

Re: Seeking help with arrays

Posted 21 October 2013 - 04:30 PM

Okay, step away from the keyboard and imagine you're actually tallying votes. How do you do it? I don't know about you, but I'd have a ballot box, and it's full of ballots. Each one has, let's say, one number on it, from 0 to 9. I take a ballot, I read the number, and I make a mark by that candidate's name.
I then put that ballot aside, and take another one, and repeat until there's no more votes.

That's tallying votes.

Now think about how you model that. Start by looking at your cands[] array. What do the ints in that array stand for?
Was This Post Helpful? 1
  • +
  • -

#12 MrDoobs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 21-September 13

Re: Seeking help with arrays

Posted 21 October 2013 - 05:27 PM

The ints stand for each candidates on the ballot that is 0-9. I will delve deeper into this when I get home. Thank you Mr.Jon I will post back when I get back to coding.
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7996
  • View blog
  • Posts: 13,694
  • Joined: 19-March 11

Re: Seeking help with arrays

Posted 21 October 2013 - 05:33 PM

View PostMrDoobs, on 21 October 2013 - 07:27 PM, said:

The ints stand for each candidates on the ballot that is 0-9. I will delve deeper into this when I get home. Thank you Mr.Jon I will post back when I get back to coding.



Try it this way:


Array cands
Index  |  ????
---------------
  0    |  What
  1    |   goes 
  2    |     here?
  3    |
  4    |
  5    |
  6    |
  7    |
  8    |
  9    |




The indexes run from 0..9 - that is, each slot is numbered from 0 to 9, using each number exactly once.
What else in your program is numbered uniquely from 0 to 9?

What, then do the array indexes mean?

Okay, then you have the value associated with each index. So what could that represent? What other arbitrary ints do you have lying around that are each associated with something that's numbered from zero to nine?
Was This Post Helpful? 1
  • +
  • -

#14 MrDoobs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 21-September 13

Re: Seeking help with arrays

Posted 21 October 2013 - 08:27 PM

First of all, Mr.Jon, thank you for your time and expertise. You helped me see the bigger picture in solving this program.

I think I need an array to store the votes into the ballotbox that will then be compared to their respective candidates using conditionals. I may be wrong in thinking this... I will hash it out after I get done studying for this quiz tomorrow.

Many thanks for all your help and time...

This post has been edited by MrDoobs: 21 October 2013 - 08:28 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1