User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,865 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,649 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 203 | Replies: 4 | Solved
Reply
Join Date: Apr 2008
Posts: 22
Reputation: Seamus McCarthy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Newbie Poster

Parrallel array problem

  #1  
Jul 16th, 2008
  1. #include <iostream>
  2. using namespace std;
  3. const int capacity = 20;
  4. string tax_payers[capacity] = {"jane", "tom", "jerry", "joe", "annie", "mary", "kate", "steve", "sarah", "paul", "petra", "peter","elaine", "noreen", "sean", "niamh", "donal", "john", "marie", "colin"};
  5. double income[capacity] = {21000.0, 16500.0, 120000.0, 16000.0, 38000.0, 62500.0, 14500.0, 28500.0, 13500.0, 27500.0, 15000.0, 44000.0, 18000.0, 21000.0, 150000.0, 15500.0, 40000.0, 21000.0, 25000.0, 37500.0};
  6. void swap(int &j, int &k);
  7. int spaces = 25;
  8. int main()
  9. {
  10.  
  11. cout << "\t" << "Sorted list" << endl;
  12.  
  13. for (int j = 0; j < capacity-1; j++)
  14. for(int i = 0; i < capacity-1; i++)
  15. {
  16. if (income[i] < income[i+1])
  17. swap(income[i],income[i+1]);
  18. }
  19.  
  20. for(int i = 0; i < capacity; i++)
  21. cout << "\t" << tax_payers[i] <<" " << "\t" << income[i] << endl;;
  22.  
  23. cout << "\n";
  24.  
  25. cout << "\t" << "Under 25000"<< endl;
  26.  
  27. for(int i=0;i<capacity;i++)
  28. {
  29. if(income[i] < 25000)
  30. cout << "\t"<< tax_payers[i] << "\n ";
  31.  
  32. }
  33.  
  34. cout << "\n";
  35.  
  36. cout << "\t" << "Over 25000" << endl;
  37.  
  38. for(int i=0;i<capacity;i++)
  39. {
  40. if(income[i] > 25000)
  41. cout << "\t" << tax_payers[i] << "\n ";
  42.  
  43. }
  44. return 0;
  45.  
  46. }
  47.  
  48. void swap( int &j, int &k )
  49. {
  50. int tmp = j;
  51. j = k;
  52. k = tmp;
  53. }

Sorry bout early post, went wrong my bad. I jst need help to assign the right tax payer and income to the right person for example sean shud b 150000 and not jane, she shud b assigned 21000. Any ideas why it is doin that??
Last edited by Narue : Jul 16th, 2008 at 2:44 pm. Reason: Fixed code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2005
Location: Phnom Penh, Cambodia
Posts: 415
Reputation: invisal will become famous soon enough invisal will become famous soon enough 
Rep Power: 5
Solved Threads: 36
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Parrallel array problem

  #2  
Jul 16th, 2008
You have sorted your income array and that is the reason why.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote  
Join Date: Apr 2008
Posts: 22
Reputation: Seamus McCarthy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Newbie Poster

Re: Parrallel array problem

  #3  
Jul 16th, 2008
Ya i understand what you are sayin, but i tried this
V
if (tax_payers[i] < tax_payers[i+1])
swap(tax_payers[i],tax_payers[i+1]);
after the first if statement but still doesn't assignin them right, did i set up the parallel arrays wrong or something??
Reply With Quote  
Join Date: Mar 2005
Location: Phnom Penh, Cambodia
Posts: 415
Reputation: invisal will become famous soon enough invisal will become famous soon enough 
Rep Power: 5
Solved Threads: 36
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Parrallel array problem

  #4  
Jul 16th, 2008
I haven't read all of your code because it is hard to read wihtout code tag. However, I think your sort should be in this way:

  1. string temp;
  2.  
  3. for (int j = 0; j < capacity-1; j++) {
  4. for(int i = 0; i < capacity-1; i++) {
  5. if (income[i] < income[i+1]) {
  6. swap(income[i],income[i+1]);
  7. temp = tax_payers[i];
  8. tax_payers[i] = tax_payers[i+1];
  9. tax_payers[i+1] = temp;
  10. }
  11. }
  12. }
When you sort your income array, the oringinal order of income will probally be changed. To keep it parrallel to the tax payers, you need to change the tax payers order too.
Last edited by invisal : Jul 16th, 2008 at 2:46 pm.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote  
Join Date: Apr 2008
Posts: 22
Reputation: Seamus McCarthy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Newbie Poster

Re: Parrallel array problem

  #5  
Jul 16th, 2008
Yep thanks its workin nw!!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:17 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC