| | |
need urgent help with vector
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 17
Reputation:
Solved Threads: 0
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:
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: c Syntax (Toggle Plain Text)
#include <iostream> void fill_array(int a[], int size, int& number_used); void sort(int a[], int number_used); void swap_values(int& v1, int& v2); int index_of_smallest(const int a[], int start_index, int number_used); int main( ) { using namespace std; cout << "This program sorts numbers from lowest to highest.\n"; int sample_array[10], number_used; fill_array(sample_array, 10, number_used); sort(sample_array, number_used); cout << "In sorted order the numbers are:\n"; for (int index = 0; index < number_used; index++) cout << sample_array[index] << " "; cout << endl; return 0; } void fill_array(int a[], int size, int& number_used) { using namespace std; cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } number_used = index; } void sort(int a[], int number_used) { int index_of_next_smallest; for (int index = 0; index < number_used - 1; index++) { index_of_next_smallest = index_of_smallest(a, index, number_used); swap_values(a[index], a[index_of_next_smallest]); } } void swap_values(int& v1, int& v2) { int temp; temp = v1; v1 = v2; v2 = temp; } int index_of_smallest(const int a[], int start_index, int number_used) { int min = a[start_index], index_of_min = start_index; for (int index = start_index + 1; index < number_used; index++) if (a[index] < min) { min = a[index]; index_of_min = index; } return index_of_min; }
Last edited by Ancient Dragon; May 16th, 2007 at 7:55 am. Reason: corrected code tags
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.
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.
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)
vector<int> sample_array; 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.
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
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).
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.
![]() |
Similar Threads
- can a vector take array of char(char *) (ASP.NET)
- Urgent help (Networking Hardware Configuration)
- Homepage keeps switching back to a porn site (Web Browsers)
- Importing SQL Script File - Urgent !! (Database Design)
Other Threads in the C++ Forum
- Previous Thread: Question on Book
- Next Thread: caputure file
Views: 1539 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int java lib library lines linkedlist linker loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort string strings struct studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






