having trouble with arrays

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2005
Posts: 46
Reputation: blackdove is an unknown quantity at this point 
Solved Threads: 0
blackdove blackdove is offline Offline
Light Poster

having trouble with arrays

 
1
  #1
Mar 22nd, 2005
I'm not finished with my program yet, but i've run into a few problems. The first main problem i see is that i seem to be doing something wrong to display the array that the user enters. It only shows one value, and it displays it twice. I cant check my other functions because that one doesn't work properly. Can anyone tell me what im doing wrong?
I also get 2 errors saying:
(37) : error C2065: 'max' : undeclared identifier
(40) : error C2065: 'min' : undeclared identifier
my code is below.
thx in advance for any help.

  1.  
  2. #include<iostream.h>
  3.  
  4. int get_array(int array[], int& size);
  5. int display_array(int array[], int& size);
  6. void reverse_array(int array[], int size);
  7. int find_max(int array[], int size);
  8. int find_min(int array[], int size);
  9. void sort_array(int array[], int size);
  10. int smallest_spread();
  11. int largest_spread();
  12. int positive_negative();
  13.  
  14. int index_of_smallest(const int array[], int start_index, int number_used);
  15. void swap_values(int& v1, int& v2);
  16.  
  17.  
  18. void main()
  19. {
  20. int size;
  21. int original_array[20];
  22. int positive_array[20];
  23. int negative_array[20];
  24.  
  25. get_array(original_array, size);
  26. cout << "\nOriginal";
  27. cout << display_array(original_array, size);
  28.  
  29. reverse_array(original_array, size);
  30. cout << "\nReversed";
  31. cout << display_array(original_array, size);
  32.  
  33. find_max(original_array, size);
  34. cout << "\nMaximum value = "<< max;
  35.  
  36. find_min(original_array, size);
  37. cout << "\nMinimum value= " << min;
  38.  
  39. sort_array(original_array, size);
  40. cout << "\nSorted";
  41. cout << display_array(original_array, size);
  42. }
  43.  
  44.  
  45. int get_array(int array[], int& size)
  46. {
  47.  
  48. cout << "Enter the array size: ";
  49. cin >> size;
  50. if(size > 20)
  51. cout << "Invalid size entered.";
  52. else
  53. for(int i=0; i < size; i++)
  54. {
  55. cout << "Enter element #" << i+1 << ": ";
  56. cin >> array[i];
  57. }
  58. return size;
  59. }
  60.  
  61. int display_array(int array[], int& size)
  62. {
  63. for(int i=0; i < size; i++)
  64. {
  65. cout << " array: " << array[i];
  66. return array[i];
  67. while(i<size)
  68. cout<< ", ";
  69. }
  70. return 0;
  71. }
  72.  
  73. void reverse_array(int array[], int size)
  74. {
  75. int temp;
  76. int j = size-1;
  77. for(int i = 0; i < size/2; i++, j--)
  78. {
  79. temp = array[i];
  80. array[i] = array[j];
  81. array[j] = temp;
  82. }
  83.  
  84. }
  85.  
  86. int find_max(int array[], int size)
  87. {
  88. int max=array[0];
  89. for(int i=1; i < size; i++);
  90. {
  91.  
  92. if(array[i]> max)
  93. max = array[i];
  94. }
  95. return max;
  96. }
  97.  
  98. int find_min(int array[], int size)
  99. {
  100. int min = array[0];
  101. for(int i=1; i < size; i++)
  102. {
  103. if(array[i] < min)
  104. min = array[i];
  105. }
  106. return min;
  107. }
  108.  
  109. void sort_array(int array[], int size)
  110. {
  111. int index_of_next_smallest;
  112. for(int i = 0; i < size-1; i++)
  113. {
  114. index_of_next_smallest= index_of_smallest(array, i, size);
  115. swap_values(array[i], array[index_of_next_smallest]);
  116. }
  117. }
  118. void swap_values(int& v1, int& v2)
  119. {
  120. int temp;
  121. temp = v1;
  122. v1 = v2;
  123. v2 = temp;
  124. }
  125. int index_of_smallest(const int array[], int start_index, int number_used)
  126. {
  127. int min = array[start_index],
  128. index_of_min = start_index;
  129. for(int i = start_index + 1; i < number_used; i++)
  130. if(array[i] < min)
  131. {
  132. min = array[i];
  133. index_of_min = i;
  134. }
  135. return index_of_min;
  136. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: having trouble with arrays

 
1
  #2
Mar 22nd, 2005
I also get 2 errors saying:
(37) : error C2065: 'max' : undeclared identifier
(40) : error C2065: 'min' : undeclared identifier
It means that in main, these two aren't declared!

What are they, integers, floats, an array, ...

Out of curiousity;
Tough I'm far from a programmer and I'm certain that I'll never be as good as most of you who post here, I find it hard to understand that, alltough you wrote this program, you fail to understand the simple errors that are shown to you while trying to compile.

Please, don't think that I'm trying to put you down, as I said, I'm not good at this myself, but such simple things as searching for errors while compiling certainly the ones you have are issues that you should be able to solve yourself, don't you think?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 46
Reputation: blackdove is an unknown quantity at this point 
Solved Threads: 0
blackdove blackdove is offline Offline
Light Poster

Re: having trouble with arrays

 
0
  #3
Mar 22nd, 2005
well i wasn't exactly asking for help with those 2 errors. i was more so looking for help with the array problem.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,858
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: having trouble with arrays

 
0
  #4
Mar 22nd, 2005
>void main()
This is not, and never has been, correct C++. main returns an integer.

>int display_array(int array[], int& size)
I notice that size is never modified, so the primary reason for passing a non-const reference doesn't apply. Because a reference is usually the same size as an int or bigger, there's no performance reasons. So why are you passing size by reference?

>It only shows one value, and it displays it twice.
That's because you print array[i] when i is 0 then return that same value immediately, where the calling function prints it. Perhaps this would be more to your liking:
  1. void display_array ( int array[], int size )
  2. {
  3. for ( int i = 0; i < size - 1; i++ )
  4. cout<< array[i] <<", ";
  5. cout<< array[i] <<endl;
  6. }
Then in main, change
  1. cout << display_array(original_array, size);
to
  1. display_array(original_array, size);
Just as a side note in case it wasn't obvious, a comma typically is not wanted after the last item, so my display loop prints all but the last item (array[size - 1]) with a comma in the loop body, then after the loop the last item is printed with a newline and the stream is flushed. At this point i would have the value of size - 1, so it all works out.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 46
Reputation: blackdove is an unknown quantity at this point 
Solved Threads: 0
blackdove blackdove is offline Offline
Light Poster

Re: having trouble with arrays

 
0
  #5
Mar 22nd, 2005
thank you so much. i had just noticed the comma problem i've got it for the most part now. I just noticed that when it prints out the max, it prints the last value that was entered, yet the minimum value prints out correctly. any idea why? :-| i dont see why one would work and not the other.

  1.  
  2. int find_max(int array[], int size, int& max)
  3. {
  4. max = array[0];
  5. for(int i=1; i < size; i++);
  6. {
  7. if(array[i] > max)
  8. max = array[i];
  9. }
  10. return max;
  11. }
  12.  
  13. int find_min(int array[], int size, int& min)
  14. {
  15. min = array[0];
  16. for(int i=1; i < size; i++)
  17. {
  18. if(array[i] < min)
  19. min = array[i];
  20. }
  21. return min;
  22. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 46
Reputation: blackdove is an unknown quantity at this point 
Solved Threads: 0
blackdove blackdove is offline Offline
Light Poster

Re: having trouble with arrays

 
0
  #6
Mar 22nd, 2005
How would I change the size of the positive and negative arrays so that it doesn't print out the total size of the original array? The way i have it set up now, it prints out the positive numbers, and the rest of the array is filled with 0's.
Here's a sample of my output:
  1. Original array: -12, -8, 9, 4, 5
  2. Reversed array: 5, 4, 9, -8, -12
  3. Maximum value = 9
  4. Minimum value= -12
  5. Sorted array: -12, -8, 4, 5, 9
  6. Smallest spread = 1
  7. Largest spread = 12
  8. Positive array: 4, 5, 9, 0, 0
  9. Negative array: -12, -8, 0, 0, 0
  10. Press any key to continue

This is my code as I have it now:
  1.  
  2. #include<iostream.h>
  3.  
  4. int get_array(int array[], int& size);
  5. void display_array(int array[], int size);
  6. void reverse_array(int array[], int size);
  7. int find_max(int array[], int size, int& max);
  8. int find_min(int array[], int size, int& min);
  9. void sort_array(int array[], int size);
  10. int smallest_spread(int array[], int size, int& s_spread);
  11. int largest_spread(int array[], int size, int& l_spread);
  12. void positive_negative(int array[], int size, int positive[], int negative[]);
  13.  
  14. int index_of_smallest(const int array[], int start_index, int number_used);
  15. void swap_values(int& v1, int& v2);
  16.  
  17.  
  18. void main()
  19. {
  20. int size, biggest, smallest, small_spread, large_spread;
  21. int original_array[20];
  22. int positive_array[20];
  23. int negative_array[20];
  24.  
  25. get_array(original_array, size);
  26. cout << "\nOriginal";
  27. display_array(original_array, size);
  28.  
  29. reverse_array(original_array, size);
  30. cout << "\nReversed";
  31. display_array(original_array, size);
  32.  
  33. find_max(original_array, size, biggest);
  34. cout << "\nMaximum value = "<< biggest;
  35.  
  36. find_min(original_array, size, smallest);
  37. cout << "\nMinimum value= " << smallest;
  38.  
  39. sort_array(original_array, size);
  40. cout << "\nSorted";
  41. display_array(original_array, size);
  42.  
  43. smallest_spread(original_array, size, small_spread);
  44. cout << "\nSmallest spread = " << small_spread;
  45.  
  46. largest_spread(original_array, size, large_spread);
  47. cout << "\nLargest spread = " << large_spread;
  48.  
  49. positive_negative(original_array, size, positive_array, negative_array);
  50. cout << "\nPositive";
  51. display_array(positive_array, size);
  52. cout << "\nNegative";
  53. display_array(negative_array, size);
  54.  
  55.  
  56. }
  57.  
  58.  
  59. int get_array(int array[], int& size)
  60. {
  61.  
  62. cout << "Enter the array size: ";
  63. cin >> size;
  64. if(size > 20)
  65. cout << "Invalid size entered.";
  66. else
  67. for(int i=0; i < size; i++)
  68. {
  69. cout << "Enter element #" << i+1 << ": ";
  70. cin >> array[i];
  71. }
  72. return size;
  73. }
  74.  
  75. void display_array(int array[], int size)
  76. {
  77. cout << " array: ";
  78. for(int i=0; i < size; i++)
  79. {
  80. cout << array[i];
  81. if (i < size-1)
  82. cout<< ", ";
  83. }
  84. }
  85.  
  86. void reverse_array(int array[], int size)
  87. {
  88. int temp;
  89. int j = size-1;
  90. for(int i = 0; i < size/2; i++, j--)
  91. {
  92. temp = array[i];
  93. array[i] = array[j];
  94. array[j] = temp;
  95. }
  96.  
  97. }
  98.  
  99. int find_max(int array[], int size, int& max)
  100. {
  101. max = array[0];
  102. for(int i=1; i < size; i++)
  103. {
  104. if(array[i] > max)
  105. max = array[i];
  106. }
  107. return max;
  108. }
  109.  
  110. int find_min(int array[], int size, int& min)
  111. {
  112. min = array[0];
  113. for(int i=1; i < size; i++)
  114. {
  115. if(array[i] < min)
  116. min = array[i];
  117. }
  118. return min;
  119. }
  120.  
  121. void sort_array(int array[], int size)
  122. {
  123. int index_of_next_smallest;
  124. for(int i = 0; i < size-1; i++)
  125. {
  126. index_of_next_smallest= index_of_smallest(array, i, size);
  127. swap_values(array[i], array[index_of_next_smallest]);
  128. }
  129. }
  130. void swap_values(int& v1, int& v2)
  131. {
  132. int temp;
  133. temp = v1;
  134. v1 = v2;
  135. v2 = temp;
  136. }
  137. int index_of_smallest(const int array[], int start_index, int number_used)
  138. {
  139. int min = array[start_index],
  140. index_of_min = start_index;
  141. for(int i = start_index + 1; i < number_used; i++)
  142. if(array[i] < min)
  143. {
  144. min = array[i];
  145. index_of_min = i;
  146. }
  147. return index_of_min;
  148. }
  149.  
  150. int smallest_spread(int array[], int size, int& s_spread)
  151. {
  152. int spread;
  153. s_spread = array[1] - array[0];
  154. for(int i = 1; i < size-1 ; i++)
  155. {
  156. spread = array[i+1] - array[i];
  157. if(spread < s_spread)
  158. s_spread = spread;
  159. }
  160. return s_spread;
  161. }
  162.  
  163. int largest_spread(int array[], int size, int& l_spread)
  164. {
  165. int spread;
  166. l_spread = array[1] - array[0];
  167. for(int i = 1; i < size-1 ; i++)
  168. {
  169. spread = array[i+1] - array[i];
  170. if(spread > l_spread)
  171. l_spread = spread;
  172. }
  173. return l_spread;
  174. }
  175.  
  176. void positive_negative(int array[], int size, int positive[], int negative[])
  177. {
  178. int i,p = 0,n = 0 ;
  179. for(i=0; i < size; i++)
  180. {
  181. positive[i]=0;
  182. negative[i]=0;
  183. }
  184. for(i=0; i < size; i++)
  185. {
  186.  
  187.  
  188. if(array[i] >= 0)
  189. {
  190. positive[p] = array[i];
  191. p++;
  192.  
  193. }
  194. else
  195. {
  196. negative[n] = array[i];
  197. n++;
  198.  
  199. }
  200. }
  201. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1506 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC