943,584 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 547
  • C++ RSS
Jul 16th, 2008
0

Parrallel array problem

Expand Post »
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008
Jul 16th, 2008
0

Re: Parrallel array problem

You have sorted your income array and that is the reason why.
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Jul 16th, 2008
0

Re: Parrallel array problem

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??
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008
Jul 16th, 2008
0

Re: Parrallel array problem

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:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Jul 16th, 2008
0

Re: Parrallel array problem

Yep thanks its workin nw!!
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Exception in Visual C++
Next Thread in C++ Forum Timeline: C++ awesomest book?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC