Parrallel array problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Parrallel array problem

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Parrallel array problem

 
0
  #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 Quick reply to this message  
Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Re: Parrallel array problem

 
0
  #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 Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Parrallel array problem

 
0
  #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 Quick reply to this message  
Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Re: Parrallel array problem

 
0
  #5
Jul 16th, 2008
Yep thanks its workin nw!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC