Tuesday, 15 September 2015

c++ - Trouble passing a pointer array to a function -



c++ - Trouble passing a pointer array to a function -

in programme working on have template function included in separate .h file reads in 5 columns of info .txt file. info passed main programme , in instance care array title "misc_data". trying determine largest value in array "misc_data" , have written function info has passed to, in order determine this. however, compiler telling me not recognize function phone call "maximum_value". pretty sure having problems variable misc_data included in routine phone call , not function itself. either not recognize misc_data array or have syntax wrong. i'm including of import snippets of code create more readable. read_five_columns functions works fine, function "maximum_value", not beingness recognized compiler because of how pointer array misc_data written in main program. clarification variable misc_data in function phone call float contains array , variable "size_mis" integer contains array size. thoughts appreciated.

int main(int argc, const char * argv[]) { #include "use_rng.h" #include "read_columnar_file.h" #include <fstream> #include <iostream> std::vector<std::string> str3; std::vector<int> str4; std::vector<char> str5; std::vector<int> str6; unsigned long size_mis; std::vector<float> misc_data; // reads in misc. spending info char file1[8]; strcpy(file1, "misc.txt"); read_five_columns(file1,misc_data,str3,str4,str5,str6); str3.clear(); str4.clear(); str5.clear(); str6.clear(); size_mis = misc_data.size(); float value; value = maximum_value(misc_data,size_mis); end_time = clock(); std::cout << std::endl << "total time: " << (end_time-start_time)/clocks_per_sec << std::endl; homecoming 0; } int maximum_value(float *array,int array_size) { float max = 0; for(int =10; < array_size-1; i++) { if(array[i] > max) max = array[i]; } homecoming max; }

there 4 problems see here.

int main(int argc, const char * argv[]) { #include "use_rng.h" #include "read_columnar_file.h" #include <fstream> #include <iostream>

all of stuff in wrong order. should not include scheme header files function bodies, , typically include standard library stuff before other stuff. prepare read this:

#include <fstream> #include <iostream> #include "use_rng.h" #include "read_columnar_file.h" int main(int argc, const char * argv[]) {

secondly, don't declare maximum_value before utilize it. need either move definition of function before definition of main() or need add together prototype before main():

int maximum_value(float *array,int array_size); int main(int argc, const char * argv[]) {

then, effort pass std::vector<float> float* not work:

value = maximum_value(misc_data,size_mis);

however, because storage vectors guaranteed contiguous , laid out array, can pass pointer first fellow member safely:

value = maximum_value(&misc_data[0],size_mis);

finally, homecoming int maximum_value when should returning float.

if possible suggest leveraging std::max_element, part of standard <algorithm> header:

// if don't have c++11 utilize std::vector<float>::iterator instead of auto. auto max = std::max_element(misc_data.begin(), misc_data.end());

now max iterator largest element, *max largest float itself.

(if input range empty, max equal misc_data.end(), equivalent function value = max == misc_data.end() ? 0f : *max;.)

c++ arrays function pointers vector

No comments:

Post a Comment