Arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 31
Reputation: RaCheer is an unknown quantity at this point 
Solved Threads: 0
RaCheer RaCheer is offline Offline
Light Poster

Arrays

 
0
  #1
Mar 30th, 2007
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!

  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. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 120
Reputation: ft3ssgeek is an unknown quantity at this point 
Solved Threads: 7
ft3ssgeek's Avatar
ft3ssgeek ft3ssgeek is offline Offline
Junior Poster

Re: Arrays

 
0
  #2
Mar 30th, 2007
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.
  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...
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 31
Reputation: RaCheer is an unknown quantity at this point 
Solved Threads: 0
RaCheer RaCheer is offline Offline
Light Poster

Re: Arrays

 
0
  #3
Mar 30th, 2007
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:

  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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Arrays

 
0
  #4
Mar 30th, 2007
You might want to initialize your counter array.
Last edited by John A; Mar 30th, 2007 at 7:59 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 120
Reputation: ft3ssgeek is an unknown quantity at this point 
Solved Threads: 7
ft3ssgeek's Avatar
ft3ssgeek ft3ssgeek is offline Offline
Junior Poster

Re: Arrays

 
0
  #5
Mar 30th, 2007
exactly...what have you got so far on your code to display the number(s) that occur the most?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 31
Reputation: RaCheer is an unknown quantity at this point 
Solved Threads: 0
RaCheer RaCheer is offline Offline
Light Poster

Re: Arrays

 
0
  #6
Mar 30th, 2007
Ok...I initialized the array to zero like so:

  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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Arrays

 
0
  #7
Mar 30th, 2007
>Why is this?
Because you misplaced your return statement.
  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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 120
Reputation: ft3ssgeek is an unknown quantity at this point 
Solved Threads: 7
ft3ssgeek's Avatar
ft3ssgeek ft3ssgeek is offline Offline
Junior Poster

Re: Arrays

 
0
  #8
Mar 31st, 2007
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:
  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...
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 120
Reputation: ft3ssgeek is an unknown quantity at this point 
Solved Threads: 7
ft3ssgeek's Avatar
ft3ssgeek ft3ssgeek is offline Offline
Junior Poster

Re: Arrays

 
0
  #9
Mar 31st, 2007
ahh, ok, good catch joe...
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 31
Reputation: RaCheer is an unknown quantity at this point 
Solved Threads: 0
RaCheer RaCheer is offline Offline
Light Poster

Re: Arrays

 
0
  #10
Mar 31st, 2007
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!

  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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