0

Beginner here trying to understand the fundamentals of functions, passing my reference, and vectors/arrays. My code reads a large data file into a vector. I then, somehow, need to convert the vector into an array, sort the array, and read the output. I believe my issue lies within my attempt to convert the vector to an array.

using namespace std;


//function prototype
int readInput(vector<int> &vect);
void sort(int[], int);
void showArray(int[], int);



int main()
{
vector<int> values;
int sum, avg;

sum = readInput(values);

const int SIZE = values.size(); //ISSUE LIES HERE
int arr[SIZE]; //and here 

sort(arr, SIZE);
showArray(arr, SIZE);


avg = sum / values.size();
//cout << "The average is: " << avg;

return 0;
}

int readInput(vector<int> &vect)
{

int count;
int total = 0;

ifstream inputFile("TopicFin.txt"); //open file

if(!inputFile)
{
    return 0; // if file is not found, return 0
}

while(inputFile >> count) //read file
 vect.push_back(count); //add to file

for (int count = 0; count < vect.size(); count++)
 total+=vect[count]; //sum data in vector

return total;

}

void sort(int array[], int size)
{
int startScan, minIndex, minValue;

for(startScan = 0; startScan < (size-1); startScan++)
{
    minIndex = startScan;
    minValue = array[startScan];
    for(int index = startScan + 1; index < size; index++)
    {
        if (array[index] < minValue)
        {
            minValue = array[index];
            minIndex = index;
        }
    }

    array[minIndex] = array[startScan];
    array[startScan] = minValue;
}
}

void showArray(const int array[], int size)
{
for(int count = 0; count < size; count++)
    cout << array[count] << " " << endl;

}
1
  • You can treat a vector as an array, there's really no need to convert anything. Commented Nov 7, 2013 at 18:55

3 Answers 3

5

You don't need to convert the vector to an array. You can sort the vector directly.

std::sort(values.begin(), values.end())

More info on sort here: http://www.cplusplus.com/reference/algorithm/sort/

I will add that in general, you should never use arrays, especially as a new C++ programmer. They are much more complicated than vectors, and are almost never useful in normal C++ code.

http://www.parashift.com/c++-faq/arrays-are-evil.html

Sign up to request clarification or add additional context in comments.

Comments

3

Let me preface by saying that, while this is a good thing to do for learning, converting vectors to arrays is probably not something you should do in real code. In reality, you'd use std::sort to sort your vector.

The root of the issue is that you can't declare an array of size that is unknown at compile-time with the int arr[SIZE] syntax.

const int SIZE = values.size();

The value of this is known when the code is executed, but not at compile time. Therefore int arr[SIZE]; cannot work unlike, say, int arr[100]. To declare an array for which you know the size at runtime, you can do it dynamically like

int* arr = new int[size];

and then you are also forced to delete the array manually.

Comments

0

As seanmcl said, you don't need to convert to an array in order to sort. However, if what you want to do is an exercise in writing a sorting function, then you could simply use values.begin() since the elements of vectors are contiguous. (This is not true for other containers.)

1 Comment

It is for std::array ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.