Jan 26, 2014 at 10:54am UTC
Hello! As im learning cpp I'm doing some exercises and I stumbled on a problem can I retrun only one value not changing other values and if I can how can I do it ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include <iostream>
#include <fstream>
using namespace std;
// Global //
const int cMax = 50;
int size;
//int vSize,tSize;
//vSize = sizeof vArray/sizeof (int);
//tSize = sizeof tArray/sizeof (int);
// Links //
int readFile(int vArray[],double tArray[]);
int sortArray(double tArray[],double btArray[]);
// -----Functions------ //
int main(){
int vArray[cMax];
double tArray[cMax];
double btArray[cMax];
readFile(vArray,tArray);
sortArray(tArray,btArray);
for (int i = 0;i<size;i++){
cout << btArray[i] << " " << tArray[i] << endl;
}
}
// Reading from file //
int readFile(int vArray[],double tArray[]){
int temp;
ifstream myfile ("u1" );
if (myfile.is_open()){
myfile >> size;
for (int i = 0;i<size;i++){
myfile >> vArray[i];
myfile >> tArray[i];
}
}
myfile.close();
return vArray,tArray,size;
}
// Finding biggest value //
int sortArray(double tArray[],double btArray[]){
double temp;
for (int i = 0;i<size;i++){
for (int j = 0;j<size;j++){
if (tArray[i]>tArray[j]){
temp = tArray[i];
tArray[i] = tArray[j];
tArray[j] = temp;
}
}
}
for (int i = 0;i<size;i++){
btArray[i] = tArray[i];
}
return btArray; //<--------------- My problem
}
Last edited on Jan 26, 2014 at 11:16am UTC
Jan 26, 2014 at 11:06am UTC
No need to return the array. The sortArray function already modifies the btArray array argument that you pass to the function.
Jan 26, 2014 at 11:11am UTC
Yes but it also modifies tArray values and I want to return only btArray values.
Last edited on Jan 26, 2014 at 11:13am UTC
Jan 26, 2014 at 11:23am UTC
Now you do:
1. Sort tArray.
2. Copy tArray to btArray.
Instead you can do:
1. Copy tArray to btArray.
2. Sort btArray.
Jan 26, 2014 at 11:40am UTC
You could change you sorting code to one simple line of code
Jan 26, 2014 at 12:29pm UTC
Thanks for suggestion Petter87 !