need urgent help with vector

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2007
Posts: 17
Reputation: MarzenaM is an unknown quantity at this point 
Solved Threads: 0
MarzenaM MarzenaM is offline Offline
Newbie Poster

need urgent help with vector

 
0
  #1
May 16th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,646
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: need urgent help with vector

 
0
  #2
May 16th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 17
Reputation: MarzenaM is an unknown quantity at this point 
Solved Threads: 0
MarzenaM MarzenaM is offline Offline
Newbie Poster

Re: need urgent help with vector

 
0
  #3
May 16th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,646
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: need urgent help with vector

 
0
  #4
May 16th, 2007
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.

  1. vector<int> sample_array;
  2. sample_array.resize(10);
Last edited by Ancient Dragon; May 16th, 2007 at 10:06 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: need urgent help with vector

 
0
  #5
May 16th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 1539 | Replies: 4
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC