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];
    }
}

Recommended Answers

All 9 Replies

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!

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]

now it wont compile when I have

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

in both the def and the prototype

any ideas

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

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.

[LIST=1]<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>    }
[*]}
[/LIST]

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)" ([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

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

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:

duh

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.