943,987 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2244
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 30th, 2007
0

Arrays

Expand Post »
I am writing a program that allows the user to insert numbers and then the program sorts them into ascending order. I have this accomplished, however, I also need an array to count the number of times each number the user inserts occurs. For ex: Say the user inserts 3 6 7 6 1. I need the array to keep count of the number of times these numbers occur. I also need to display the number/numbers that occur the most. I have struggled with these issues for the past few days and am not very good with arrays. I would really appreciate any help you could offer. Thank you!

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. int fill_array(int a[], int size, int& number_used);
  5. void sort(int a[], int number_used);
  6. void swap_values(int& v1, int& v2);
  7. int index_of_smallest(const int a[], int start_index, int number_used);
  8. int count = 0;
  9. int main()
  10. {
  11.  
  12. cout << "This program sorts numbers from lowest to highest.\n";
  13. int sample_array[100], number_used;
  14. fill_array(sample_array, 100, number_used);
  15. sort(sample_array, number_used);
  16. cout << "\n";
  17. cout << "\n";
  18. cout << "Numbers\t";
  19. cout << "Times" << endl;
  20. for (int index = 0; index < number_used; index++)
  21. {
  22. cout << sample_array[index] << "\t";
  23. cout << "Count will go here." << endl;
  24. }
  25. cout << endl;
  26. cout << "\n";
  27. cout << "You entered " << count << " numbers." << endl;
  28. return 0;
  29. }
  30. int fill_array(int a[], int size, int& number_used)
  31. {
  32. cout << "Enter up to " << size << " nonnegative whole numbers.\n"
  33. << "Mark the end of the list with a negative number.\n";
  34. int next, index = 0;
  35. cin >> next;
  36. while ((next >= 0) && (index < size))
  37. {
  38. a[index] = next;
  39. index++;
  40. cin >> next;
  41. count++;
  42.  
  43. }
  44. number_used = index;
  45. return count;
  46. }
  47. void sort(int a[], int number_used)
  48. {
  49. int index_of_next_smallest;
  50. for (int index = 0; index < number_used - 1; index++)
  51. {
  52. index_of_next_smallest = index_of_smallest(a, index, number_used);
  53. swap_values(a[index], a[index_of_next_smallest]);
  54. }
  55. }
  56. void swap_values(int& v1, int& v2)
  57. {
  58. int temp;
  59. temp = v1;
  60. v1 = v2;
  61. v2 = temp;
  62. }
  63.  
  64. int index_of_smallest(const int a[], int start_index, int number_used)
  65. {
  66. int min = a[start_index],
  67. index_of_min = start_index;
  68. for (int index = start_index + 1; index < number_used; index++)
  69. if (a[index] < min)
  70. {
  71. min = a[index];
  72. index_of_min = index;
  73. }
  74. return index_of_min;
  75. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
RaCheer is offline Offline
31 posts
since Feb 2007
Mar 30th, 2007
0

Re: Arrays

for the first problem, counting the number of times each number occurs in the array, you want to creat a second array that is the same size as the first one. Initialize all cells in this new array to 0. Pass both of those arrays to your counting function. You will have two loops, one inside the other. The first loop will loop through the second array, and the second loop will loop through the first array. By comparing the value of the cell in the first array that corresponds to the cell the second array is currently on, testing each value in the first array and incrementing the value in the second array by one each time you have a match, you will end up with the second array being populated with the number of times each number occured in the first array.
C++ Syntax (Toggle Plain Text)
  1. for(int i=0;i<asize;i++){
  2. for(int j=0;j<asize;j++){
  3. if(arr1[i]==arr1[j])
  4. arr2[i]++;
  5. }
  6. }
  7. }
This oughta get you going again...hope it helps...
Reputation Points: 9
Solved Threads: 7
Junior Poster
ft3ssgeek is offline Offline
124 posts
since Mar 2007
Mar 30th, 2007
0

Re: Arrays

Ok...I have tried out your suggestion and so far I have it working without any compilation errors. However, I get some pretty crazy numbers in my counter column in the results. Here is my new code:

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. int fill_array(int a[], int size, int& number_used);
  5. void sort(int a[], int number_used);
  6. void swap_values(int& v1, int& v2);
  7. int index_of_smallest(const int a[], int start_index, int number_used);
  8. int count_array(int sample_array[], int counter[], int number_used);
  9. int count = 0;
  10. int main()
  11. {
  12.  
  13. cout << "This program sorts numbers from lowest to highest.\n";
  14. int sample_array[100], number_used;
  15. int counter[100];
  16. fill_array(sample_array, 100, number_used);
  17. count_array(sample_array, counter, number_used);
  18. sort(sample_array, number_used);
  19. cout << "\n";
  20. cout << "\n";
  21. cout << "Numbers\t";
  22. cout << "Times" << endl;
  23. for (int index = 0; index < number_used; index++)
  24. {
  25. cout << sample_array[index] << "\t";
  26. cout << counter[index] << endl;
  27. }
  28. cout << endl;
  29. cout << "\n";
  30. cout << "You entered " << count << " numbers." << endl;
  31. return 0;
  32. }
  33. int fill_array(int a[], int size, int& number_used)
  34. {
  35. cout << "Enter up to " << size << " nonnegative whole numbers.\n"
  36. << "Mark the end of the list with a negative number.\n";
  37. int next, index = 0;
  38. cin >> next;
  39. while ((next >= 0) && (index < size))
  40. {
  41. a[index] = next;
  42. index++;
  43. cin >> next;
  44. count++;
  45.  
  46. }
  47. number_used = index;
  48. return count;
  49. }
  50. void sort(int a[], int number_used)
  51. {
  52. int index_of_next_smallest;
  53. for (int index = 0; index < number_used - 1; index++)
  54. {
  55. index_of_next_smallest = index_of_smallest(a, index, number_used);
  56. swap_values(a[index], a[index_of_next_smallest]);
  57. }
  58. }
  59. void swap_values(int& v1, int& v2)
  60. {
  61. int temp;
  62. temp = v1;
  63. v1 = v2;
  64. v2 = temp;
  65. }
  66.  
  67. int index_of_smallest(const int a[], int start_index, int number_used)
  68. {
  69. int min = a[start_index],
  70. index_of_min = start_index;
  71. for (int index = start_index + 1; index < number_used; index++)
  72. if (a[index] < min)
  73. {
  74. min = a[index];
  75. index_of_min = index;
  76. }
  77. return index_of_min;
  78. }
  79. int count_array(int sample_array[], int counter[], int number_used)
  80. {
  81. for(int index=0; index<number_used; index++)
  82. {
  83. for(int index2=0; index2<number_used; index2++)
  84. {
  85. if (sample_array[index]==sample_array[index2])
  86. counter[index]++;
  87. }
  88. return counter[index];
  89. }
  90.  
  91. }


And this is the strange result I get:

Numbers Times

1 -858993548
3 -858993460
3 -858993460
5 -858993460
6 -858993460


Any help in cleaning up my mess would be appreciated! I am also still wondering how to display the number/numbers that occur the most. Thank you!
Reputation Points: 10
Solved Threads: 0
Light Poster
RaCheer is offline Offline
31 posts
since Feb 2007
Mar 30th, 2007
0

Re: Arrays

You might want to initialize your counter array.
Last edited by John A; Mar 30th, 2007 at 7:59 pm.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 30th, 2007
0

Re: Arrays

exactly...what have you got so far on your code to display the number(s) that occur the most?
Reputation Points: 9
Solved Threads: 7
Junior Poster
ft3ssgeek is offline Offline
124 posts
since Mar 2007
Mar 30th, 2007
0

Re: Arrays

Ok...I initialized the array to zero like so:

C++ Syntax (Toggle Plain Text)
  1.  
  2. int counter[100] = {0};


But now in the result it only tells how many times the first number was displayed. The other numbers display 0 times:

Numbers Times

1 1
2 0
3 0

Why is this?

Also, I do not have anything in my code yet to display the number(s) that occur the most. I was wondering how to go about that. I am learning more with your help! Thank you!
Reputation Points: 10
Solved Threads: 0
Light Poster
RaCheer is offline Offline
31 posts
since Feb 2007
Mar 30th, 2007
0

Re: Arrays

>Why is this?
Because you misplaced your return statement.
C++ Syntax (Toggle Plain Text)
  1. int count_array(int sample_array[], int counter[], int number_used)
  2. {
  3. for(int index=0; index<number_used; index++)
  4. {
  5. for(int index2=0; index2<number_used; index2++)
  6. {
  7. if (sample_array[index]==sample_array[index2])
  8. counter[index]++;
  9. }
  10. return counter[index]; // drop this down below the }
  11. }
  12. }
Why are you even using a return statement at all? It's not like the last element in the array is going to be very useful without the other values...

And by the way you may want to consider using a debugger, they make errors like these really easy to spot.
Last edited by John A; Mar 30th, 2007 at 11:38 pm.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 31st, 2007
0

Re: Arrays

Ok...looking through your code I don't see anything wrong with the counter function...One thing to note though, is that you don't need a return value for that counter function...arrays automatically get passed by reference...you could have just as easiler used the following function header:
C++ Syntax (Toggle Plain Text)
  1. void count_array(int sample_array[], int counter[], int number_used)
and not returned a value...I just compiled a small test program on my computer and it ran the code just fine...you might try using void instead and recompiling and see how it goes...
As for the other problem (which number occurs the most...once you get your second array (the one with the count of numbers) you will have your number(s) that occur the most...You just need to figure out how to use it...
Reputation Points: 9
Solved Threads: 7
Junior Poster
ft3ssgeek is offline Offline
124 posts
since Mar 2007
Mar 31st, 2007
0

Re: Arrays

ahh, ok, good catch joe...
Reputation Points: 9
Solved Threads: 7
Junior Poster
ft3ssgeek is offline Offline
124 posts
since Mar 2007
Mar 31st, 2007
0

Re: Arrays

Wonderful...it now works! I will continue to work on displaying the numbers that occur the most often.

I have another question though. I have used a selection sort to sort the numbers into ascending order. Out of curiosity, how could I change it to an insertion sort, so that a number is sorted into place as soon as the user enters it? It seems it would make more sense to di it this way. Thank you!

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. int fill_array(int a[], int size, int& number_used);
  5. void sort(int a[], int number_used);
  6. void swap_values(int& v1, int& v2);
  7. int index_of_smallest(const int a[], int start_index, int number_used);
  8. void count_array(int sample_array[], int counter[], int number_used);
  9. int count = 0;
  10. int main()
  11. {
  12.  
  13. cout << "This program sorts numbers from lowest to highest.\n";
  14. int sample_array[100], number_used;
  15. int counter[100] = {0};
  16. fill_array(sample_array, 100, number_used);
  17. count_array(sample_array, counter, number_used);
  18. sort(sample_array, number_used);
  19. cout << "\n";
  20. cout << "\n";
  21. cout << "Numbers\t";
  22. cout << "Times" << endl;
  23. for (int index = 0; index < number_used; index++)
  24. {
  25. cout << sample_array[index] << "\t";
  26. cout << counter[index] << endl;
  27. }
  28. cout << endl;
  29. cout << "\n";
  30. cout << "You entered " << count << " numbers." << endl;
  31. return 0;
  32. }
  33. int fill_array(int a[], int size, int& number_used)
  34. {
  35. cout << "Enter up to " << size << " nonnegative whole numbers.\n"
  36. << "Mark the end of the list with a negative number.\n";
  37. int next, index = 0;
  38. cin >> next;
  39. while ((next >= 0) && (index < size))
  40. {
  41. a[index] = next;
  42. index++;
  43. cin >> next;
  44. count++;
  45.  
  46. }
  47. number_used = index;
  48. return count;
  49. }
  50. void sort(int a[], int number_used)
  51. {
  52. int index_of_next_smallest;
  53. for (int index = 0; index < number_used - 1; index++)
  54. {
  55. index_of_next_smallest = index_of_smallest(a, index, number_used);
  56. swap_values(a[index], a[index_of_next_smallest]);
  57. }
  58. }
  59. void swap_values(int& v1, int& v2)
  60. {
  61. int temp;
  62. temp = v1;
  63. v1 = v2;
  64. v2 = temp;
  65. }
  66.  
  67. int index_of_smallest(const int a[], int start_index, int number_used)
  68. {
  69. int min = a[start_index],
  70. index_of_min = start_index;
  71. for (int index = start_index + 1; index < number_used; index++)
  72. if (a[index] < min)
  73. {
  74. min = a[index];
  75. index_of_min = index;
  76. }
  77. return index_of_min;
  78. }
  79. void count_array(int sample_array[], int counter[], int number_used)
  80. {
  81. for(int index=0; index<number_used; index++)
  82. {
  83. for(int index2=0; index2<number_used; index2++)
  84. {
  85. if (sample_array[index]==sample_array[index2])
  86. counter[index]++;
  87. }
  88.  
  89. }
  90.  
  91. }
Reputation Points: 10
Solved Threads: 0
Light Poster
RaCheer is offline Offline
31 posts
since Feb 2007

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: misplaced else and removing space characters
Next Thread in C++ Forum Timeline: C++ compilers





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


Follow us on Twitter


© 2011 DaniWeb® LLC