943,982 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2071
  • C++ RSS
May 16th, 2007
0

need urgent help with vector

Expand Post »
I have to write programm to sort numbers from in ascending order, using vector (C++ language)
I did this using array, and it works perfect. Please could you help me replace array to vector. It's very urgent. My code is:

  1. #include <iostream>
  2. void fill_array(int a[], int size, int& number_used);
  3. void sort(int a[], int number_used);
  4. void swap_values(int& v1, int& v2);
  5. int index_of_smallest(const int a[], int start_index, int number_used);
  6. int main( )
  7. {
  8. using namespace std;
  9. cout << "This program sorts numbers from lowest to highest.\n";
  10. int sample_array[10], number_used;
  11. fill_array(sample_array, 10, number_used);
  12. sort(sample_array, number_used);
  13. cout << "In sorted order the numbers are:\n";
  14. for (int index = 0; index < number_used; index++)
  15. cout << sample_array[index] << " ";
  16. cout << endl;
  17. return 0;
  18. }
  19. void fill_array(int a[], int size, int& number_used)
  20. {
  21. using namespace std;
  22. cout << "Enter up to " << size << " nonnegative whole numbers.\n"
  23. << "Mark the end of the list with a negative number.\n";
  24. int next, index = 0;
  25. cin >> next;
  26. while ((next >= 0) && (index < size))
  27. {
  28. a[index] = next;
  29. index++;
  30. cin >> next;
  31. }
  32. number_used = index;
  33. }
  34. void sort(int a[], int number_used)
  35. {
  36. int index_of_next_smallest;
  37. for (int index = 0; index < number_used - 1; index++)
  38. {
  39. index_of_next_smallest =
  40. index_of_smallest(a, index, number_used);
  41. swap_values(a[index], a[index_of_next_smallest]);
  42. }
  43. }
  44. void swap_values(int& v1, int& v2)
  45. {
  46. int temp;
  47. temp = v1;
  48. v1 = v2;
  49. v2 = temp;
  50. }
  51. int index_of_smallest(const int a[], int start_index, int number_used)
  52. {
  53. int min = a[start_index],
  54. index_of_min = start_index;
  55. for (int index = start_index + 1; index < number_used; index++)
  56. if (a[index] < min)
  57. {
  58. min = a[index];
  59. index_of_min = index;
  60. }
  61. return index_of_min;
  62. }
Last edited by Ancient Dragon; May 16th, 2007 at 7:55 am. Reason: corrected code tags
Similar Threads
Reputation Points: 13
Solved Threads: 0
Newbie Poster
MarzenaM is offline Offline
17 posts
since Apr 2007
May 16th, 2007
0

Re: need urgent help with vector

c++ has a sort() function, all you have to write is a comparison function. Here is a short tutorial

If you want to keep all the code you have already written, just replace the array with a vector, then call the vector's resize() method to initialize it to contain 10 elements. I think the rest of the program should work without change.
Last edited by Ancient Dragon; May 16th, 2007 at 8:09 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 16th, 2007
0

Re: need urgent help with vector

I can't use classes and structures, we didn't do it yet.
And I can't just replace array with vector because since vector can dermine the number used with the member function size the function will not need a parameter like numberr_used.
Any other ideas???? I am really desperate.
Reputation Points: 13
Solved Threads: 0
Newbie Poster
MarzenaM is offline Offline
17 posts
since Apr 2007
May 16th, 2007
0

Re: need urgent help with vector

you sait you have to use vector, just replace the array with a vector as mentioned before. If you don't want number_used then delete it from the program. Give it a try and repost your most recent attempt at solving the problem.

C++ Syntax (Toggle Plain Text)
  1. vector<int> sample_array;
  2. sample_array.resize(10);
Last edited by Ancient Dragon; May 16th, 2007 at 10:06 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 16th, 2007
0

Re: need urgent help with vector

You could always try this syntax:

vector<int> sample_array(10);

if you want a vector of ints with space for exactly 10 ints and don't want to call resize().

If you find that the stuff you do in fill() and sort() aren't maintained in the vector in the calling function, then you will need to pass the vector by reference explicitly instead of the automatic pass by reference that happens when you pass arrays.

Oh, and to use the STL vector class you'll need to include the vector header file; and I'd move the using namespace std; line to have global scope rather than function scope so you don't have to type it twice (or more often).
Last edited by Lerner; May 16th, 2007 at 5:40 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

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: Question on Book
Next Thread in C++ Forum Timeline: caputure file





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


Follow us on Twitter


© 2011 DaniWeb® LLC