0

In a program I am currently working on I have a template function included in a separate .h file that reads in five columns of data from a .txt file. The data is passed to the main program and in this instance I only care about the array title "MISC_DATA". I am trying to determine the largest value in the array "MISC_DATA" and have written another function that the data has to be passed to, in order to determine this. However, the compiler is telling me that it does not recognize the function call "Maximum_Value". I am pretty sure that it is having problems with the variable MISC_DATA included in the routine call and not the function itself. Either it does not recognize MISC_DATA as an array or I have the syntax wrong. I'm only including the important snippets of code to make it more readable. The Read_Five_Columns functions works fine, it is the function "Maximum_Value", which is not being recognized by the compiler because of how the pointer array MISC_DATA is written in the main program. For clarification the variable MISC_DATA in the function call is a float which contains the array and the variable "size_Mis" is an integer which contains the array size. Any thoughts would be 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 data
    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;
return 0;
}

int Maximum_Value(float *array,int array_size)
{
    float max = 0;
   for(int i =10; i < array_size-1; i++)
   {
        if(array[i] > max) max = array[i];
   }
    return max;
}
4
  • Why does Maxmimum_Value return int instead of float? Commented Nov 10, 2014 at 17:20
  • MISC_DATA is a std::vector and not an array or a pointer. Commented Nov 10, 2014 at 17:21
  • 2
    And don't include header files inside a function. Commented Nov 10, 2014 at 17:21
  • Thank you for the comments. The header file definitions are mistakenly cut and pasted after the call to main, but in the real program are listed before. I should have double checked that before hitting send. You are right, I did not correctly pick up on the difference between vector and an array. Commented Nov 10, 2014 at 18:18

1 Answer 1

6

There are four problems I see here.

int main(int argc, const char * argv[]) {

#include "Use_RNG.h"
#include "Read_Columnar_File.h"
#include <fstream>
#include <iostream>

All of this stuff is in the wrong order. You should not include system header files into function bodies, and typically you include standard library stuff before other stuff. Fix it to read like this:

#include <fstream>
#include <iostream>

#include "Use_RNG.h"
#include "Read_Columnar_File.h"

int main(int argc, const char * argv[]) {

Secondly, you don't declare Maximum_Value before you use it. You need to either move the definition of this function before the definition of main() or you need to add a prototype before main():

int Maximum_Value(float *array,int array_size);

int main(int argc, const char * argv[]) {

Then, you attempt to pass an std::vector<float> as a float* which does not work:

value = Maximum_Value(MISC_DATA,size_Mis);

However, because the storage for vectors is guaranteed to be contiguous and laid out like an array, you can pass a pointer to the first member safely:

value = Maximum_Value(&MISC_DATA[0],size_Mis);

Finally, you return int from Maximum_Value when you should probably be returning float.


If possible I would suggest leveraging std::max_element, which is part of the standard <algorithm> header:

// If you don't have C++11 then use std::vector<float>::iterator instead of auto.
auto max = std::max_element(MISC_DATA.begin(), MISC_DATA.end());

Now max is an iterator to the largest element, so *max would be the largest float itself.

(If the input range was empty, then max will be equal to MISC_DATA.end(), so the equivalent to your function would be value = max == MISC_DATA.end() ? 0f : *max;.)

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

5 Comments

my apologies allot of the issues you correctly mention are because of an accident in cutting and pasting and are not listed accordingly in the actual program. In the actual program definitions are before main() and there is a prototype before the main() as well. This was just an artifact of pasting from the main program to this website to fast and not checking before I hit submit. So if I can pass a pointer to the first element in an array; how can I pass the whole array which needs to be analyzed in the function?
@Jon The same way I proposed. Since the memory in a vector is guaranteed to be laid out the same way as an array, (&vec[0])[n] has the same value as vec[n]. That is, you can treat &vec[0] as a pointer to the first element in an array and use pointer math (as you do in the function) to fetch successive elements, same as you can for any other array.
Ahh, I see, you only need to pass a reference to the first element and then the pointer math in the function begins to point to the successive elements/memory locations. Thank you very much!!
@Jon No problem. However, I would strongly suggest using std::max_element from the standard library. auto max = std::max_element(MISC_DATA.begin(), MISC_DATA.end()); and then max will be an iterator to the largest element (so *max would be the value).
Again, I was not aware that such a function existed in the standard template library. Regardless, this still acted as a good learning assignment.

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.