help vector problem

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

Join Date: Mar 2007
Posts: 159
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

help vector problem

 
0
  #1
Mar 25th, 2007
This code is supposed to sort the numbers I input. I get only 0's

  1. // Group Project 10.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <vector>
  6. using namespace std;
  7. void selectionSort (vector< int> list, int length);
  8. void fillArray(vector< int> list, int length);
  9.  
  10. int main ()
  11. {
  12. int length;
  13. vector< int> list(4);
  14. length = list.size ();
  15.  
  16. cout << "Enter 4 integers: "; //Prompt user to input 10 digits
  17. fillArray(list, length); //Function that fills the array with the digits from the console
  18. cout << endl;
  19.  
  20. // selectionSort (list, length);
  21.  
  22. unsigned int index;
  23. for (index = 0; index < length; index++)
  24. {
  25. cout << list[index];
  26. }
  27. }
  28.  
  29. selectionSort (vector<int> list, int length)
  30. {
  31. int index;
  32. int smallestIndex;
  33. int minIndex;
  34. int temp;
  35.  
  36. for (index = 0; index < length -1; index ++)
  37. {
  38. smallestIndex = index;
  39. for (minIndex = index +1; minIndex < length; minIndex++)
  40. {
  41. if (list[minIndex] < list[smallestIndex])
  42. {
  43. smallestIndex = minIndex;
  44. }
  45.  
  46. temp = list[smallestIndex];
  47. list[smallestIndex] = list[index];
  48. list[index] = temp;
  49. }
  50. }
  51. }
  52.  
  53. //Function to fill up the array
  54. void fillArray(vector< int> list, int length)
  55. {
  56. int i;
  57. for (i = 0; i < length; i++)
  58. {
  59. cin >> list[i];
  60. }
  61. }
Last edited by Ancient Dragon; Mar 25th, 2007 at 9:08 pm. Reason: removed color tags and added code tags
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help vector problem

 
0
  #2
Mar 25th, 2007
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:
  1. void fillArray(vector<int> &list, int length) {
  2. // ...

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,651
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: help vector problem

 
0
  #3
Mar 25th, 2007
you need to pass the vector by reference, not by value. like this:
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 159
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: help vector problem

 
0
  #4
Mar 25th, 2007
now it wont compile when I have

void fillArray(vector<int>& list, int length)

in both the def and the prototype

any ideas
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help vector problem

 
0
  #5
Mar 25th, 2007
Post your updated code (and any errors you receive).
Last edited by John A; Mar 25th, 2007 at 10:14 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,651
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: help vector problem

 
0
  #6
Mar 25th, 2007
you forgot the void function return type
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 159
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: help vector problem

 
0
  #7
Mar 25th, 2007
  1. <ol style="list-style-type: decimal"><li><LI class=li1>// Group Project 10.cpp : Defines the entry point for the console application.
  2. <LI class=li1>//
  3. <LI class=li1> #include "stdafx.h"
  4. <LI class=li1> #include <iostream>
  5. <LI class=li2> #include <vector>
  6. <LI class=li1> using namespace std;
  7. <LI class=li1> void selectionSort (vector< int> list, int length);
  8. <LI class=li1> void fillArray(vector< int>& list, int length);
  9. <LI class=li1>
  10. <LI class=li2>int main ()
  11. <LI class=li1>{
  12. <LI class=li1> int length;
  13. <LI class=li1> vector< int> list(4);
  14. <LI class=li1> length = list.size ();
  15. <LI class=li2>
  16. <LI class=li1> cout << "Enter 4 integers: "; //Prompt user to input 10 digits
  17. <LI class=li1> fillArray(list, length); //Function that fills the array with the digits from the console
  18. <LI class=li1> cout << endl;
  19. <LI class=li1>
  20. <LI class=li2>// selectionSort (list, length);
  21. <LI class=li1>
  22. <LI class=li1> unsigned int index;
  23. <LI class=li1> for (index = 0; index < length; index++)
  24. <LI class=li1> {
  25. <LI class=li2> cout << list[index];
  26. <LI class=li1> }
  27. <LI class=li1>}
  28. <LI class=li1>
  29. <LI class=li1>selectionSort (vector<int> list, int length)
  30. <LI class=li2>{
  31. <LI class=li1> int index;
  32. <LI class=li1> int smallestIndex;
  33. <LI class=li1> int minIndex;
  34. <LI class=li1> int temp;
  35. <LI class=li2>
  36. <LI class=li1> for (index = 0; index < length -1; index ++)
  37. <LI class=li1> {
  38. <LI class=li1> smallestIndex = index;
  39. <LI class=li1> for (minIndex = index +1; minIndex < length; minIndex++)
  40. <LI class=li2> {
  41. <LI class=li1> if (list[minIndex] < list[smallestIndex])
  42. <LI class=li1> {
  43. <LI class=li1> smallestIndex = minIndex;
  44. <LI class=li1> }
  45. <LI class=li2>
  46. <LI class=li1> temp = list[smallestIndex];
  47. <LI class=li1> list[smallestIndex] = list[index];
  48. <LI class=li1> list[index] = temp;
  49. <LI class=li1> }
  50. <LI class=li2> }
  51. <LI class=li1>}
  52. <LI class=li1>
  53. <LI class=li1>//Function to fill up the array
  54. <LI class=li1>void fillArray(vector< int>& list, int length)
  55. <LI class=li2>{
  56. <LI class=li1> int i;
  57. <LI class=li1> for (i = 0; i < length; i++)
  58. <LI class=li1> {
  59. <LI class=li1> cin >> list[i];
  60. <LI class=li2> }</li>
  61. <li>}</li>
  62. </ol>
errors:page 606 num 10 fatal error LNK1120: 1 unresolved externals
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 159
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: help vector problem

 
0
  #8
Mar 25th, 2007
now it compiles, and I dont get zero's , but it still does not sort.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,651
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: help vector problem

 
0
  #9
Mar 25th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 159
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: help vector problem

 
0
  #10
Mar 25th, 2007
duh

thanks
Reply With Quote Quick reply to this message  
Reply

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




Views: 1208 | Replies: 9
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC