954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help vector problem

This code is supposed to sort the numbers I input. I get only 0's

// 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];
    }
}
mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

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:

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


edit: haha, I beat you Ancient Dragon!

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

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]

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

now it wont compile when I have

void fillArray(vector& list, int length)

in both the def and the prototype

any ideas

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

Post your updated code (and any errors you receive).

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
<ul><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>
</ul>


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)" ([EMAIL="?fillArray@@YAXV?$vector@HV?$allocator@H@std@@@std@@H@Z"]?fillArray@@YAXV?$vector@HV?$allocator@H@std@@@std@@H@Z[/EMAIL]) referenced in function _main

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

now it compiles, and I dont get zero's , but it still does not sort.

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

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:

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

duh

thanks

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You