Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 18th, 2012, 04:37 AM
alan117 alan117 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 2 alan117 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 13 sec
Reputation Power: 0
Homework - Counting element(s) in array(s)

Hi all
I need some help please with Java and arrays.
I have two arrays: customerID and transactionValue (they are pairs so each transaction has a customer ID and a customer may have made several transactions).
The number of elements in each array has an int variable called ‘size’.

I need to work out, and display (using loops and ‘if’ statements, not allowed to create any other arrays or lists): for each customer I need to display the number of transactions that are on the list.
So I thought I could use variable (int for customer ID and double for transaction values) to “go through the array and count the number of times that for example customer 0 is represented, when that is done do the loop again and see how many times customer 1 is represented and so on”.
This is my initial thoughts but I may be totally wrong, and clearly I need more code:
int count=0;
int thisValue;

for (index=0;index<size;index++){

for (thisValue=0;thisValue<size;thisValue++){

if (customerID[index]==thisValue){

count=count++;
}

}

}

System.out.println ("The count is= "+count);
}






The final output must look something like this:

Customer ID Transaction Value
2, 45.2
1, 55.2
3, 10.3
3, 20
0, 400.2
4, 100.30
3, 50.5

Customer: 2, Transactions:1
Customer: 1, Transactions: 1
Customer: 3, Transactions 3
Customer: 0, Transactons: 1
Customer: 4, Transactions: 1

I have been struggling for hours so any pointers would be most welcome- thanks

Reply With Quote
  #2  
Old August 18th, 2012, 07:20 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,961 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 23 m 31 sec
Reputation Power: 345
Before you try writing code you should make a list of the steps (an algorithm) that the program needs to do (pseudo code). Once the logic in the list of steps is worked out, then move on to write the code.
Can you post the steps the program needs to do to solve the problem. When that is worked out, move on to writing the code.

When posting code please wrap the code it code tags to preserve it formatting.

Reply With Quote
  #3  
Old August 19th, 2012, 03:59 AM
alan117 alan117 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 2 alan117 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 13 sec
Reputation Power: 0
Code:
for ( i = 0 ; i < 10 ; i++ ) { 
count = 0 ;
for ( j = 0 ; j < 10 ; j++) {
if (customerID[i] == customerID[j]) { count++ ; }
}
Print customerID[i] + count here
}


Having written it down I have produced the above code and that compiles and produces the following output:
C:\***3>java As
CustomerID, Transaction Value
3 30.31
2 13.21
0 23.09
3 48.58
1 4.12
4 38.53
2 8.83
2 9.3
4 85.21
2 72.34
customer:3, 0
customer:2, 0
customer:0, 0
customer:3, 0
customer:1, 0
customer:4, 0
customer:2, 0
customer:2, 0
customer:4, 0
customer:2, 0

What I do not understand now is why the code will not count the customers “once” and then find how many transactions they each have?

So I want it to now print:
customer:3, 2
customer:2, 4
customer:0, 1
customer:4, 2

Any pointers please?

Reply With Quote
  #4  
Old August 19th, 2012, 06:29 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,961 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 23 m 31 sec
Reputation Power: 345
Quote:
count the customers “once”

You need to work out the logic before writing the code. If you do it manually, how would you do it?
How would you determine if a customer had been counted already?

Reply With Quote
  #5  
Old August 19th, 2012, 07:23 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,961 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 23 m 31 sec
Reputation Power: 345

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Counting element(s) in array(s)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap