Importing csv file and then sort it with c++












-1















I have a project that told me to import a csv file and then sort it (The file contains million of numbers). I googled for a merge sort source code and found this, i tried it and the sorting is working. (I compiled this code in ubuntu with g++ via Virtual Box).



#include<iostream>
#include<vector>

using namespace std;

void print(vector<int> v)
{
for(int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}

vector<int> merge(vector<int> left, vector<int> right)
{
vector<int> result;
while ((int)left.size() > 0 || (int)right.size() > 0) {
if ((int)left.size() > 0 && (int)right.size() > 0) {
if ((int)left.front() <= (int)right.front()) {
result.push_back((int)left.front());
left.erase(left.begin());
}
else {
result.push_back((int)right.front());
right.erase(right.begin());
}
} else if ((int)left.size() > 0) {
for (int i = 0; i < (int)left.size(); i++)
result.push_back(left[i]);
break;
} else if ((int)right.size() > 0) {
for (int i = 0; i < (int)right.size(); i++)
result.push_back(right[i]);
break;
}
}
return result;
}

vector<int> mergeSort(vector<int> m)
{
if (m.size() <= 1)
return m;

vector<int> left, right, result;
int middle = ((int)m.size()+ 1) / 2;

for (int i = 0; i < middle; i++) {
left.push_back(m[i]);
}

for (int i = middle; i < (int)m.size(); i++) {
right.push_back(m[i]);
}

left = mergeSort(left);
right = mergeSort(right);
result = merge(left, right);

return result;
}

int main()
{
vector<int> v;

v.push_back(38);
v.push_back(27);
v.push_back(43);
v.push_back(3);
v.push_back(9);
v.push_back(82);
v.push_back(10);

print(v);
cout << "------------------" << endl;

v = mergeSort(v);

print(v);
}


But when i insert the code to import the csv file, it only read the file and the sorting is not running, am i placed it wrong? Here is my code :



#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>

using namespace std;

const int jumlahData = 999999;
double data[jumlahData] = {0};

void loadData(const char* namaFile) {
string line;
ifstream myfile (namaFile);

if (myfile.is_open()) {
int counter = 0;
while (getline(myfile, line, ',')) {
//cout << line << 'n';
data[counter] = stod(line);
counter++;
}
myfile.close();
}
}

void print(vector<int> v)
{
for(int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}

vector<int> merge(vector<int> left, vector<int> right)
{
vector<int> result;
while ((int)left.size() > 0 || (int)right.size() > 0) {
if ((int)left.size() > 0 && (int)right.size() > 0) {
if ((int)left.front() <= (int)right.front()) {
result.push_back((int)left.front());
left.erase(left.begin());
}
else {
result.push_back((int)right.front());
right.erase(right.begin());
}
} else if ((int)left.size() > 0) {
for (int i = 0; i < (int)left.size(); i++)
result.push_back(left[i]);
break;
} else if ((int)right.size() > 0) {
for (int i = 0; i < (int)right.size(); i++)
result.push_back(right[i]);
break;
}
}
return result;
}

vector<int> mergeSort(vector<int> m)
{
if (m.size() <= 1)
return m;

vector<int> left, right, result;
int middle = ((int)m.size()+ 1) / 2;

for (int i = 0; i < middle; i++) {
left.push_back(m[i]);
}

for (int i = middle; i < (int)m.size(); i++) {
right.push_back(m[i]);
}

left = mergeSort(left);
right = mergeSort(right);
result = merge(left, right);

return result;
}

int main()
{
vector<int> v;

loadData("dataDemo.csv");

print(v);
cout << "------------------" << endl;

v = mergeSort(v);

print(v);
}









share|improve this question


















  • 1





    you can use std::sort.

    – apple apple
    Nov 24 '18 at 4:21






  • 1





    and where your loadData write into main::v?

    – apple apple
    Nov 24 '18 at 4:25













  • ... also, annotate your code so we can understand what your purpose of the directions it's taking is.

    – Ted Lyngmo
    Nov 24 '18 at 4:31








  • 1





    Your file data is stored in the global array double data, not a vector<int>.

    – not an alien
    Nov 24 '18 at 4:33


















-1















I have a project that told me to import a csv file and then sort it (The file contains million of numbers). I googled for a merge sort source code and found this, i tried it and the sorting is working. (I compiled this code in ubuntu with g++ via Virtual Box).



#include<iostream>
#include<vector>

using namespace std;

void print(vector<int> v)
{
for(int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}

vector<int> merge(vector<int> left, vector<int> right)
{
vector<int> result;
while ((int)left.size() > 0 || (int)right.size() > 0) {
if ((int)left.size() > 0 && (int)right.size() > 0) {
if ((int)left.front() <= (int)right.front()) {
result.push_back((int)left.front());
left.erase(left.begin());
}
else {
result.push_back((int)right.front());
right.erase(right.begin());
}
} else if ((int)left.size() > 0) {
for (int i = 0; i < (int)left.size(); i++)
result.push_back(left[i]);
break;
} else if ((int)right.size() > 0) {
for (int i = 0; i < (int)right.size(); i++)
result.push_back(right[i]);
break;
}
}
return result;
}

vector<int> mergeSort(vector<int> m)
{
if (m.size() <= 1)
return m;

vector<int> left, right, result;
int middle = ((int)m.size()+ 1) / 2;

for (int i = 0; i < middle; i++) {
left.push_back(m[i]);
}

for (int i = middle; i < (int)m.size(); i++) {
right.push_back(m[i]);
}

left = mergeSort(left);
right = mergeSort(right);
result = merge(left, right);

return result;
}

int main()
{
vector<int> v;

v.push_back(38);
v.push_back(27);
v.push_back(43);
v.push_back(3);
v.push_back(9);
v.push_back(82);
v.push_back(10);

print(v);
cout << "------------------" << endl;

v = mergeSort(v);

print(v);
}


But when i insert the code to import the csv file, it only read the file and the sorting is not running, am i placed it wrong? Here is my code :



#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>

using namespace std;

const int jumlahData = 999999;
double data[jumlahData] = {0};

void loadData(const char* namaFile) {
string line;
ifstream myfile (namaFile);

if (myfile.is_open()) {
int counter = 0;
while (getline(myfile, line, ',')) {
//cout << line << 'n';
data[counter] = stod(line);
counter++;
}
myfile.close();
}
}

void print(vector<int> v)
{
for(int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}

vector<int> merge(vector<int> left, vector<int> right)
{
vector<int> result;
while ((int)left.size() > 0 || (int)right.size() > 0) {
if ((int)left.size() > 0 && (int)right.size() > 0) {
if ((int)left.front() <= (int)right.front()) {
result.push_back((int)left.front());
left.erase(left.begin());
}
else {
result.push_back((int)right.front());
right.erase(right.begin());
}
} else if ((int)left.size() > 0) {
for (int i = 0; i < (int)left.size(); i++)
result.push_back(left[i]);
break;
} else if ((int)right.size() > 0) {
for (int i = 0; i < (int)right.size(); i++)
result.push_back(right[i]);
break;
}
}
return result;
}

vector<int> mergeSort(vector<int> m)
{
if (m.size() <= 1)
return m;

vector<int> left, right, result;
int middle = ((int)m.size()+ 1) / 2;

for (int i = 0; i < middle; i++) {
left.push_back(m[i]);
}

for (int i = middle; i < (int)m.size(); i++) {
right.push_back(m[i]);
}

left = mergeSort(left);
right = mergeSort(right);
result = merge(left, right);

return result;
}

int main()
{
vector<int> v;

loadData("dataDemo.csv");

print(v);
cout << "------------------" << endl;

v = mergeSort(v);

print(v);
}









share|improve this question


















  • 1





    you can use std::sort.

    – apple apple
    Nov 24 '18 at 4:21






  • 1





    and where your loadData write into main::v?

    – apple apple
    Nov 24 '18 at 4:25













  • ... also, annotate your code so we can understand what your purpose of the directions it's taking is.

    – Ted Lyngmo
    Nov 24 '18 at 4:31








  • 1





    Your file data is stored in the global array double data, not a vector<int>.

    – not an alien
    Nov 24 '18 at 4:33
















-1












-1








-1








I have a project that told me to import a csv file and then sort it (The file contains million of numbers). I googled for a merge sort source code and found this, i tried it and the sorting is working. (I compiled this code in ubuntu with g++ via Virtual Box).



#include<iostream>
#include<vector>

using namespace std;

void print(vector<int> v)
{
for(int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}

vector<int> merge(vector<int> left, vector<int> right)
{
vector<int> result;
while ((int)left.size() > 0 || (int)right.size() > 0) {
if ((int)left.size() > 0 && (int)right.size() > 0) {
if ((int)left.front() <= (int)right.front()) {
result.push_back((int)left.front());
left.erase(left.begin());
}
else {
result.push_back((int)right.front());
right.erase(right.begin());
}
} else if ((int)left.size() > 0) {
for (int i = 0; i < (int)left.size(); i++)
result.push_back(left[i]);
break;
} else if ((int)right.size() > 0) {
for (int i = 0; i < (int)right.size(); i++)
result.push_back(right[i]);
break;
}
}
return result;
}

vector<int> mergeSort(vector<int> m)
{
if (m.size() <= 1)
return m;

vector<int> left, right, result;
int middle = ((int)m.size()+ 1) / 2;

for (int i = 0; i < middle; i++) {
left.push_back(m[i]);
}

for (int i = middle; i < (int)m.size(); i++) {
right.push_back(m[i]);
}

left = mergeSort(left);
right = mergeSort(right);
result = merge(left, right);

return result;
}

int main()
{
vector<int> v;

v.push_back(38);
v.push_back(27);
v.push_back(43);
v.push_back(3);
v.push_back(9);
v.push_back(82);
v.push_back(10);

print(v);
cout << "------------------" << endl;

v = mergeSort(v);

print(v);
}


But when i insert the code to import the csv file, it only read the file and the sorting is not running, am i placed it wrong? Here is my code :



#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>

using namespace std;

const int jumlahData = 999999;
double data[jumlahData] = {0};

void loadData(const char* namaFile) {
string line;
ifstream myfile (namaFile);

if (myfile.is_open()) {
int counter = 0;
while (getline(myfile, line, ',')) {
//cout << line << 'n';
data[counter] = stod(line);
counter++;
}
myfile.close();
}
}

void print(vector<int> v)
{
for(int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}

vector<int> merge(vector<int> left, vector<int> right)
{
vector<int> result;
while ((int)left.size() > 0 || (int)right.size() > 0) {
if ((int)left.size() > 0 && (int)right.size() > 0) {
if ((int)left.front() <= (int)right.front()) {
result.push_back((int)left.front());
left.erase(left.begin());
}
else {
result.push_back((int)right.front());
right.erase(right.begin());
}
} else if ((int)left.size() > 0) {
for (int i = 0; i < (int)left.size(); i++)
result.push_back(left[i]);
break;
} else if ((int)right.size() > 0) {
for (int i = 0; i < (int)right.size(); i++)
result.push_back(right[i]);
break;
}
}
return result;
}

vector<int> mergeSort(vector<int> m)
{
if (m.size() <= 1)
return m;

vector<int> left, right, result;
int middle = ((int)m.size()+ 1) / 2;

for (int i = 0; i < middle; i++) {
left.push_back(m[i]);
}

for (int i = middle; i < (int)m.size(); i++) {
right.push_back(m[i]);
}

left = mergeSort(left);
right = mergeSort(right);
result = merge(left, right);

return result;
}

int main()
{
vector<int> v;

loadData("dataDemo.csv");

print(v);
cout << "------------------" << endl;

v = mergeSort(v);

print(v);
}









share|improve this question














I have a project that told me to import a csv file and then sort it (The file contains million of numbers). I googled for a merge sort source code and found this, i tried it and the sorting is working. (I compiled this code in ubuntu with g++ via Virtual Box).



#include<iostream>
#include<vector>

using namespace std;

void print(vector<int> v)
{
for(int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}

vector<int> merge(vector<int> left, vector<int> right)
{
vector<int> result;
while ((int)left.size() > 0 || (int)right.size() > 0) {
if ((int)left.size() > 0 && (int)right.size() > 0) {
if ((int)left.front() <= (int)right.front()) {
result.push_back((int)left.front());
left.erase(left.begin());
}
else {
result.push_back((int)right.front());
right.erase(right.begin());
}
} else if ((int)left.size() > 0) {
for (int i = 0; i < (int)left.size(); i++)
result.push_back(left[i]);
break;
} else if ((int)right.size() > 0) {
for (int i = 0; i < (int)right.size(); i++)
result.push_back(right[i]);
break;
}
}
return result;
}

vector<int> mergeSort(vector<int> m)
{
if (m.size() <= 1)
return m;

vector<int> left, right, result;
int middle = ((int)m.size()+ 1) / 2;

for (int i = 0; i < middle; i++) {
left.push_back(m[i]);
}

for (int i = middle; i < (int)m.size(); i++) {
right.push_back(m[i]);
}

left = mergeSort(left);
right = mergeSort(right);
result = merge(left, right);

return result;
}

int main()
{
vector<int> v;

v.push_back(38);
v.push_back(27);
v.push_back(43);
v.push_back(3);
v.push_back(9);
v.push_back(82);
v.push_back(10);

print(v);
cout << "------------------" << endl;

v = mergeSort(v);

print(v);
}


But when i insert the code to import the csv file, it only read the file and the sorting is not running, am i placed it wrong? Here is my code :



#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>

using namespace std;

const int jumlahData = 999999;
double data[jumlahData] = {0};

void loadData(const char* namaFile) {
string line;
ifstream myfile (namaFile);

if (myfile.is_open()) {
int counter = 0;
while (getline(myfile, line, ',')) {
//cout << line << 'n';
data[counter] = stod(line);
counter++;
}
myfile.close();
}
}

void print(vector<int> v)
{
for(int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}

vector<int> merge(vector<int> left, vector<int> right)
{
vector<int> result;
while ((int)left.size() > 0 || (int)right.size() > 0) {
if ((int)left.size() > 0 && (int)right.size() > 0) {
if ((int)left.front() <= (int)right.front()) {
result.push_back((int)left.front());
left.erase(left.begin());
}
else {
result.push_back((int)right.front());
right.erase(right.begin());
}
} else if ((int)left.size() > 0) {
for (int i = 0; i < (int)left.size(); i++)
result.push_back(left[i]);
break;
} else if ((int)right.size() > 0) {
for (int i = 0; i < (int)right.size(); i++)
result.push_back(right[i]);
break;
}
}
return result;
}

vector<int> mergeSort(vector<int> m)
{
if (m.size() <= 1)
return m;

vector<int> left, right, result;
int middle = ((int)m.size()+ 1) / 2;

for (int i = 0; i < middle; i++) {
left.push_back(m[i]);
}

for (int i = middle; i < (int)m.size(); i++) {
right.push_back(m[i]);
}

left = mergeSort(left);
right = mergeSort(right);
result = merge(left, right);

return result;
}

int main()
{
vector<int> v;

loadData("dataDemo.csv");

print(v);
cout << "------------------" << endl;

v = mergeSort(v);

print(v);
}






c++ csv merge mergesort






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 4:18









FataFata

6




6








  • 1





    you can use std::sort.

    – apple apple
    Nov 24 '18 at 4:21






  • 1





    and where your loadData write into main::v?

    – apple apple
    Nov 24 '18 at 4:25













  • ... also, annotate your code so we can understand what your purpose of the directions it's taking is.

    – Ted Lyngmo
    Nov 24 '18 at 4:31








  • 1





    Your file data is stored in the global array double data, not a vector<int>.

    – not an alien
    Nov 24 '18 at 4:33
















  • 1





    you can use std::sort.

    – apple apple
    Nov 24 '18 at 4:21






  • 1





    and where your loadData write into main::v?

    – apple apple
    Nov 24 '18 at 4:25













  • ... also, annotate your code so we can understand what your purpose of the directions it's taking is.

    – Ted Lyngmo
    Nov 24 '18 at 4:31








  • 1





    Your file data is stored in the global array double data, not a vector<int>.

    – not an alien
    Nov 24 '18 at 4:33










1




1





you can use std::sort.

– apple apple
Nov 24 '18 at 4:21





you can use std::sort.

– apple apple
Nov 24 '18 at 4:21




1




1





and where your loadData write into main::v?

– apple apple
Nov 24 '18 at 4:25







and where your loadData write into main::v?

– apple apple
Nov 24 '18 at 4:25















... also, annotate your code so we can understand what your purpose of the directions it's taking is.

– Ted Lyngmo
Nov 24 '18 at 4:31







... also, annotate your code so we can understand what your purpose of the directions it's taking is.

– Ted Lyngmo
Nov 24 '18 at 4:31






1




1





Your file data is stored in the global array double data, not a vector<int>.

– not an alien
Nov 24 '18 at 4:33







Your file data is stored in the global array double data, not a vector<int>.

– not an alien
Nov 24 '18 at 4:33














2 Answers
2






active

oldest

votes


















1














your loadData("dataDemo.csv"); call does not writes to a previously declared local vector<int> v;. Your vector v has no elements. You need to write a method that populates vector v based on the contents of double data[jumlahData]
namely..



int main()
{
vector<int> v;

loadData("dataDemo.csv");
populateVectorWithData(v, data); //TODO implement me
print(v);
cout << "------------------" << endl;

v = mergeSort(v);

print(v);
}





share|improve this answer
























  • With you except for reading into the array and copying to the vector rather than reading directly into the vector. If, quoting the asker, [t]he file contains million of numbers and the array size is less than a million, using the array just seems to be a really bad idea. Feeding the asker a bit of code demonstrating loadData with a vector and using push_back wouldn't rob them of too much education.

    – user4581301
    Nov 24 '18 at 5:12











  • I'm sorry, could you tell me how to do it? i understand your answer a little bit, but i don't know what syntax to write and where it should be placed. Thank you sir.

    – Fata
    Nov 24 '18 at 5:16



















0














As others have pointed out, you are not sorting the actual data that you read in. This can be easily be solved by returning a count of the data-points you read-in.



A better approach, one that I suspect you are trying, is to send a reference of the vector from main to loadData and push_back the data on the vector. Furthermore, it is advisable to use your implementations provided libraries when possible. In other words, do not reinvent the wheel and just use <algorithm> instead of your own or someone else's sorting algorithm. It helps prevent nasty bugs.



int main()
{
//return the count of integers read into data in loadData()
//also change the vector & array types to be consistent, please!
std::vector<int> v (data, loadData("dataDemo.csv");

print(v); //print the loaded vector
cout << "------------------" << endl;

std::sort(v.begin(), v.end()); // using <algorithm> (̶i̶t̶'̶s̶ ̶q̶u̶i̶c̶k̶s̶o̶r̶t̶)̶

print(v);
}





share|improve this answer





















  • 1





    it's quicksort actuallyyyyy... it often isn't. These days it tends to be something called introsort.

    – user4581301
    Nov 24 '18 at 5:15











  • interesting; thanks for the knowledge senpai!

    – slmatrix
    Nov 24 '18 at 5:18











  • I'm sorry i forgot to tell, i only allowed to choose between merge/genetic sort, so can i use this method or no? thank you.

    – Fata
    Nov 24 '18 at 5:45











  • yes you can, do: v = mergeSort(v); instead.

    – slmatrix
    Nov 24 '18 at 5:47











  • in loadData you use counter++ to count how many numbers you've read-in. The v we construct in main needs to know that and also needs to know where data is hence why I do std::vector<int> v (data, loadData("dataDemo.csv");. Notice that I included the function call in the constructor, meaning that v completes construction after loadData returns with the count variable.

    – slmatrix
    Nov 25 '18 at 2:27













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455101%2fimporting-csv-file-and-then-sort-it-with-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














your loadData("dataDemo.csv"); call does not writes to a previously declared local vector<int> v;. Your vector v has no elements. You need to write a method that populates vector v based on the contents of double data[jumlahData]
namely..



int main()
{
vector<int> v;

loadData("dataDemo.csv");
populateVectorWithData(v, data); //TODO implement me
print(v);
cout << "------------------" << endl;

v = mergeSort(v);

print(v);
}





share|improve this answer
























  • With you except for reading into the array and copying to the vector rather than reading directly into the vector. If, quoting the asker, [t]he file contains million of numbers and the array size is less than a million, using the array just seems to be a really bad idea. Feeding the asker a bit of code demonstrating loadData with a vector and using push_back wouldn't rob them of too much education.

    – user4581301
    Nov 24 '18 at 5:12











  • I'm sorry, could you tell me how to do it? i understand your answer a little bit, but i don't know what syntax to write and where it should be placed. Thank you sir.

    – Fata
    Nov 24 '18 at 5:16
















1














your loadData("dataDemo.csv"); call does not writes to a previously declared local vector<int> v;. Your vector v has no elements. You need to write a method that populates vector v based on the contents of double data[jumlahData]
namely..



int main()
{
vector<int> v;

loadData("dataDemo.csv");
populateVectorWithData(v, data); //TODO implement me
print(v);
cout << "------------------" << endl;

v = mergeSort(v);

print(v);
}





share|improve this answer
























  • With you except for reading into the array and copying to the vector rather than reading directly into the vector. If, quoting the asker, [t]he file contains million of numbers and the array size is less than a million, using the array just seems to be a really bad idea. Feeding the asker a bit of code demonstrating loadData with a vector and using push_back wouldn't rob them of too much education.

    – user4581301
    Nov 24 '18 at 5:12











  • I'm sorry, could you tell me how to do it? i understand your answer a little bit, but i don't know what syntax to write and where it should be placed. Thank you sir.

    – Fata
    Nov 24 '18 at 5:16














1












1








1







your loadData("dataDemo.csv"); call does not writes to a previously declared local vector<int> v;. Your vector v has no elements. You need to write a method that populates vector v based on the contents of double data[jumlahData]
namely..



int main()
{
vector<int> v;

loadData("dataDemo.csv");
populateVectorWithData(v, data); //TODO implement me
print(v);
cout << "------------------" << endl;

v = mergeSort(v);

print(v);
}





share|improve this answer













your loadData("dataDemo.csv"); call does not writes to a previously declared local vector<int> v;. Your vector v has no elements. You need to write a method that populates vector v based on the contents of double data[jumlahData]
namely..



int main()
{
vector<int> v;

loadData("dataDemo.csv");
populateVectorWithData(v, data); //TODO implement me
print(v);
cout << "------------------" << endl;

v = mergeSort(v);

print(v);
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 4:39









HappyKeyboardHappyKeyboard

1066




1066













  • With you except for reading into the array and copying to the vector rather than reading directly into the vector. If, quoting the asker, [t]he file contains million of numbers and the array size is less than a million, using the array just seems to be a really bad idea. Feeding the asker a bit of code demonstrating loadData with a vector and using push_back wouldn't rob them of too much education.

    – user4581301
    Nov 24 '18 at 5:12











  • I'm sorry, could you tell me how to do it? i understand your answer a little bit, but i don't know what syntax to write and where it should be placed. Thank you sir.

    – Fata
    Nov 24 '18 at 5:16



















  • With you except for reading into the array and copying to the vector rather than reading directly into the vector. If, quoting the asker, [t]he file contains million of numbers and the array size is less than a million, using the array just seems to be a really bad idea. Feeding the asker a bit of code demonstrating loadData with a vector and using push_back wouldn't rob them of too much education.

    – user4581301
    Nov 24 '18 at 5:12











  • I'm sorry, could you tell me how to do it? i understand your answer a little bit, but i don't know what syntax to write and where it should be placed. Thank you sir.

    – Fata
    Nov 24 '18 at 5:16

















With you except for reading into the array and copying to the vector rather than reading directly into the vector. If, quoting the asker, [t]he file contains million of numbers and the array size is less than a million, using the array just seems to be a really bad idea. Feeding the asker a bit of code demonstrating loadData with a vector and using push_back wouldn't rob them of too much education.

– user4581301
Nov 24 '18 at 5:12





With you except for reading into the array and copying to the vector rather than reading directly into the vector. If, quoting the asker, [t]he file contains million of numbers and the array size is less than a million, using the array just seems to be a really bad idea. Feeding the asker a bit of code demonstrating loadData with a vector and using push_back wouldn't rob them of too much education.

– user4581301
Nov 24 '18 at 5:12













I'm sorry, could you tell me how to do it? i understand your answer a little bit, but i don't know what syntax to write and where it should be placed. Thank you sir.

– Fata
Nov 24 '18 at 5:16





I'm sorry, could you tell me how to do it? i understand your answer a little bit, but i don't know what syntax to write and where it should be placed. Thank you sir.

– Fata
Nov 24 '18 at 5:16













0














As others have pointed out, you are not sorting the actual data that you read in. This can be easily be solved by returning a count of the data-points you read-in.



A better approach, one that I suspect you are trying, is to send a reference of the vector from main to loadData and push_back the data on the vector. Furthermore, it is advisable to use your implementations provided libraries when possible. In other words, do not reinvent the wheel and just use <algorithm> instead of your own or someone else's sorting algorithm. It helps prevent nasty bugs.



int main()
{
//return the count of integers read into data in loadData()
//also change the vector & array types to be consistent, please!
std::vector<int> v (data, loadData("dataDemo.csv");

print(v); //print the loaded vector
cout << "------------------" << endl;

std::sort(v.begin(), v.end()); // using <algorithm> (̶i̶t̶'̶s̶ ̶q̶u̶i̶c̶k̶s̶o̶r̶t̶)̶

print(v);
}





share|improve this answer





















  • 1





    it's quicksort actuallyyyyy... it often isn't. These days it tends to be something called introsort.

    – user4581301
    Nov 24 '18 at 5:15











  • interesting; thanks for the knowledge senpai!

    – slmatrix
    Nov 24 '18 at 5:18











  • I'm sorry i forgot to tell, i only allowed to choose between merge/genetic sort, so can i use this method or no? thank you.

    – Fata
    Nov 24 '18 at 5:45











  • yes you can, do: v = mergeSort(v); instead.

    – slmatrix
    Nov 24 '18 at 5:47











  • in loadData you use counter++ to count how many numbers you've read-in. The v we construct in main needs to know that and also needs to know where data is hence why I do std::vector<int> v (data, loadData("dataDemo.csv");. Notice that I included the function call in the constructor, meaning that v completes construction after loadData returns with the count variable.

    – slmatrix
    Nov 25 '18 at 2:27


















0














As others have pointed out, you are not sorting the actual data that you read in. This can be easily be solved by returning a count of the data-points you read-in.



A better approach, one that I suspect you are trying, is to send a reference of the vector from main to loadData and push_back the data on the vector. Furthermore, it is advisable to use your implementations provided libraries when possible. In other words, do not reinvent the wheel and just use <algorithm> instead of your own or someone else's sorting algorithm. It helps prevent nasty bugs.



int main()
{
//return the count of integers read into data in loadData()
//also change the vector & array types to be consistent, please!
std::vector<int> v (data, loadData("dataDemo.csv");

print(v); //print the loaded vector
cout << "------------------" << endl;

std::sort(v.begin(), v.end()); // using <algorithm> (̶i̶t̶'̶s̶ ̶q̶u̶i̶c̶k̶s̶o̶r̶t̶)̶

print(v);
}





share|improve this answer





















  • 1





    it's quicksort actuallyyyyy... it often isn't. These days it tends to be something called introsort.

    – user4581301
    Nov 24 '18 at 5:15











  • interesting; thanks for the knowledge senpai!

    – slmatrix
    Nov 24 '18 at 5:18











  • I'm sorry i forgot to tell, i only allowed to choose between merge/genetic sort, so can i use this method or no? thank you.

    – Fata
    Nov 24 '18 at 5:45











  • yes you can, do: v = mergeSort(v); instead.

    – slmatrix
    Nov 24 '18 at 5:47











  • in loadData you use counter++ to count how many numbers you've read-in. The v we construct in main needs to know that and also needs to know where data is hence why I do std::vector<int> v (data, loadData("dataDemo.csv");. Notice that I included the function call in the constructor, meaning that v completes construction after loadData returns with the count variable.

    – slmatrix
    Nov 25 '18 at 2:27
















0












0








0







As others have pointed out, you are not sorting the actual data that you read in. This can be easily be solved by returning a count of the data-points you read-in.



A better approach, one that I suspect you are trying, is to send a reference of the vector from main to loadData and push_back the data on the vector. Furthermore, it is advisable to use your implementations provided libraries when possible. In other words, do not reinvent the wheel and just use <algorithm> instead of your own or someone else's sorting algorithm. It helps prevent nasty bugs.



int main()
{
//return the count of integers read into data in loadData()
//also change the vector & array types to be consistent, please!
std::vector<int> v (data, loadData("dataDemo.csv");

print(v); //print the loaded vector
cout << "------------------" << endl;

std::sort(v.begin(), v.end()); // using <algorithm> (̶i̶t̶'̶s̶ ̶q̶u̶i̶c̶k̶s̶o̶r̶t̶)̶

print(v);
}





share|improve this answer















As others have pointed out, you are not sorting the actual data that you read in. This can be easily be solved by returning a count of the data-points you read-in.



A better approach, one that I suspect you are trying, is to send a reference of the vector from main to loadData and push_back the data on the vector. Furthermore, it is advisable to use your implementations provided libraries when possible. In other words, do not reinvent the wheel and just use <algorithm> instead of your own or someone else's sorting algorithm. It helps prevent nasty bugs.



int main()
{
//return the count of integers read into data in loadData()
//also change the vector & array types to be consistent, please!
std::vector<int> v (data, loadData("dataDemo.csv");

print(v); //print the loaded vector
cout << "------------------" << endl;

std::sort(v.begin(), v.end()); // using <algorithm> (̶i̶t̶'̶s̶ ̶q̶u̶i̶c̶k̶s̶o̶r̶t̶)̶

print(v);
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 5:26

























answered Nov 24 '18 at 5:12









slmatrixslmatrix

15611




15611








  • 1





    it's quicksort actuallyyyyy... it often isn't. These days it tends to be something called introsort.

    – user4581301
    Nov 24 '18 at 5:15











  • interesting; thanks for the knowledge senpai!

    – slmatrix
    Nov 24 '18 at 5:18











  • I'm sorry i forgot to tell, i only allowed to choose between merge/genetic sort, so can i use this method or no? thank you.

    – Fata
    Nov 24 '18 at 5:45











  • yes you can, do: v = mergeSort(v); instead.

    – slmatrix
    Nov 24 '18 at 5:47











  • in loadData you use counter++ to count how many numbers you've read-in. The v we construct in main needs to know that and also needs to know where data is hence why I do std::vector<int> v (data, loadData("dataDemo.csv");. Notice that I included the function call in the constructor, meaning that v completes construction after loadData returns with the count variable.

    – slmatrix
    Nov 25 '18 at 2:27
















  • 1





    it's quicksort actuallyyyyy... it often isn't. These days it tends to be something called introsort.

    – user4581301
    Nov 24 '18 at 5:15











  • interesting; thanks for the knowledge senpai!

    – slmatrix
    Nov 24 '18 at 5:18











  • I'm sorry i forgot to tell, i only allowed to choose between merge/genetic sort, so can i use this method or no? thank you.

    – Fata
    Nov 24 '18 at 5:45











  • yes you can, do: v = mergeSort(v); instead.

    – slmatrix
    Nov 24 '18 at 5:47











  • in loadData you use counter++ to count how many numbers you've read-in. The v we construct in main needs to know that and also needs to know where data is hence why I do std::vector<int> v (data, loadData("dataDemo.csv");. Notice that I included the function call in the constructor, meaning that v completes construction after loadData returns with the count variable.

    – slmatrix
    Nov 25 '18 at 2:27










1




1





it's quicksort actuallyyyyy... it often isn't. These days it tends to be something called introsort.

– user4581301
Nov 24 '18 at 5:15





it's quicksort actuallyyyyy... it often isn't. These days it tends to be something called introsort.

– user4581301
Nov 24 '18 at 5:15













interesting; thanks for the knowledge senpai!

– slmatrix
Nov 24 '18 at 5:18





interesting; thanks for the knowledge senpai!

– slmatrix
Nov 24 '18 at 5:18













I'm sorry i forgot to tell, i only allowed to choose between merge/genetic sort, so can i use this method or no? thank you.

– Fata
Nov 24 '18 at 5:45





I'm sorry i forgot to tell, i only allowed to choose between merge/genetic sort, so can i use this method or no? thank you.

– Fata
Nov 24 '18 at 5:45













yes you can, do: v = mergeSort(v); instead.

– slmatrix
Nov 24 '18 at 5:47





yes you can, do: v = mergeSort(v); instead.

– slmatrix
Nov 24 '18 at 5:47













in loadData you use counter++ to count how many numbers you've read-in. The v we construct in main needs to know that and also needs to know where data is hence why I do std::vector<int> v (data, loadData("dataDemo.csv");. Notice that I included the function call in the constructor, meaning that v completes construction after loadData returns with the count variable.

– slmatrix
Nov 25 '18 at 2:27







in loadData you use counter++ to count how many numbers you've read-in. The v we construct in main needs to know that and also needs to know where data is hence why I do std::vector<int> v (data, loadData("dataDemo.csv");. Notice that I included the function call in the constructor, meaning that v completes construction after loadData returns with the count variable.

– slmatrix
Nov 25 '18 at 2:27




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455101%2fimporting-csv-file-and-then-sort-it-with-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks