| | |
help vector problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 159
Reputation:
Solved Threads: 0
This code is supposed to sort the numbers I input. I get only 0's
c Syntax (Toggle Plain Text)
// Group Project 10.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <vector> using namespace std; void selectionSort (vector< int> list, int length); void fillArray(vector< int> list, int length); int main () { int length; vector< int> list(4); length = list.size (); cout << "Enter 4 integers: "; //Prompt user to input 10 digits fillArray(list, length); //Function that fills the array with the digits from the console cout << endl; // selectionSort (list, length); unsigned int index; for (index = 0; index < length; index++) { cout << list[index]; } } selectionSort (vector<int> list, int length) { int index; int smallestIndex; int minIndex; int temp; for (index = 0; index < length -1; index ++) { smallestIndex = index; for (minIndex = index +1; minIndex < length; minIndex++) { if (list[minIndex] < list[smallestIndex]) { smallestIndex = minIndex; } temp = list[smallestIndex]; list[smallestIndex] = list[index]; list[index] = temp; } } } //Function to fill up the array void fillArray(vector< int> list, int length) { int i; for (i = 0; i < length; i++) { cin >> list[i]; } }
Last edited by Ancient Dragon; Mar 25th, 2007 at 9:08 pm. Reason: removed color tags and added code tags
You're passing a copy of the vector list to fillArray, which goes out of scope as soon as the function returns. Try changing it to a reference:
edit: haha, I beat you Ancient Dragon!
C++ Syntax (Toggle Plain Text)
void fillArray(vector<int> &list, int length) { // ...
edit: haha, I beat you Ancient Dragon!
Last edited by John A; Mar 25th, 2007 at 9:11 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
you need to pass the vector by reference, not by value. like this:
[edit] Sorry Joe, I didn't see your post before I posted mine [/edit]
void fillArray(vector<int>& list, int length)[edit] Sorry Joe, I didn't see your post before I posted mine [/edit]
Last edited by Ancient Dragon; Mar 25th, 2007 at 9:12 pm.
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 forgot the void function return type
depending on what compiler you are using you may also have to redefine length as size_t instead of int.
void selectionSort (vector<int>& list, size_t length)depending on what compiler you are using you may also have to redefine length as size_t instead of int.
Last edited by Ancient Dragon; Mar 25th, 2007 at 10:19 pm.
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: Mar 2007
Posts: 159
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
<ol style="list-style-type: decimal"><li><LI class=li1>// Group Project 10.cpp : Defines the entry point for the console application. <LI class=li1>// <LI class=li1> #include "stdafx.h" <LI class=li1> #include <iostream> <LI class=li2> #include <vector> <LI class=li1> using namespace std; <LI class=li1> void selectionSort (vector< int> list, int length); <LI class=li1> void fillArray(vector< int>& list, int length); <LI class=li1> <LI class=li2>int main () <LI class=li1>{ <LI class=li1> int length; <LI class=li1> vector< int> list(4); <LI class=li1> length = list.size (); <LI class=li2> <LI class=li1> cout << "Enter 4 integers: "; //Prompt user to input 10 digits <LI class=li1> fillArray(list, length); //Function that fills the array with the digits from the console <LI class=li1> cout << endl; <LI class=li1> <LI class=li2>// selectionSort (list, length); <LI class=li1> <LI class=li1> unsigned int index; <LI class=li1> for (index = 0; index < length; index++) <LI class=li1> { <LI class=li2> cout << list[index]; <LI class=li1> } <LI class=li1>} <LI class=li1> <LI class=li1>selectionSort (vector<int> list, int length) <LI class=li2>{ <LI class=li1> int index; <LI class=li1> int smallestIndex; <LI class=li1> int minIndex; <LI class=li1> int temp; <LI class=li2> <LI class=li1> for (index = 0; index < length -1; index ++) <LI class=li1> { <LI class=li1> smallestIndex = index; <LI class=li1> for (minIndex = index +1; minIndex < length; minIndex++) <LI class=li2> { <LI class=li1> if (list[minIndex] < list[smallestIndex]) <LI class=li1> { <LI class=li1> smallestIndex = minIndex; <LI class=li1> } <LI class=li2> <LI class=li1> temp = list[smallestIndex]; <LI class=li1> list[smallestIndex] = list[index]; <LI class=li1> list[index] = temp; <LI class=li1> } <LI class=li2> } <LI class=li1>} <LI class=li1> <LI class=li1>//Function to fill up the array <LI class=li1>void fillArray(vector< int>& list, int length) <LI class=li2>{ <LI class=li1> int i; <LI class=li1> for (i = 0; i < length; i++) <LI class=li1> { <LI class=li1> cin >> list[i]; <LI class=li2> }</li> <li>}</li> </ol>
c:\Documents and Settings\jmartinez\My Documents\C++\page 606 num 10\page 606 num 10.cpp(19): 1. warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
c:\Documents and Settings\jmartinez\My Documents\C++\page 606 num 10\page 606 num 10.cpp(30): 2. warning C4018: '<' : signed/unsigned mismatch
page 606 num 10 3. error LNK2019: unresolved external symbol "void __cdecl fillArray(class std::vector<int,class std::allocator<int> >,int)" (?fillArray@@YAXV?$vector@HV?$allocator@H@std@@@std@@H@Z) referenced in function _main
it doesn't sort because you commented that line out in main(). And what is all that crap at the beginning of every line that you posted? I hope you didn't put that there intentially :eek:
Last edited by Ancient Dragon; Mar 25th, 2007 at 11:24 pm.
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.
![]() |
Similar Threads
- strstream problem (C++)
- save self-defined object in vector problem (C++)
- passing vector to a pointer? (C++)
Other Threads in the C++ Forum
- Previous Thread: Supposed to tell the output of this array, but I'm not sure how it works
- Next Thread: plotting sine curve
Views: 1208 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






