简体   繁体   中英

C++: Passing string arrays to function WITHOUT USING VECTORS

So for my class assignment I have to do this grade book program. The part I'm struggling with is figuring out how to pass a string array from one function to another, such that the latter function can perform calculations on the data stored in the string array. Well, zooming out further to the bigger picture, the string array (which is for student names) is parallel to a double array (which is for scores), and the function receiving the arrays must find highest and lowest, calculate mean, and print output to screen and file. I get all that last bit, but I can't figure out correct syntax for referring arrays to a function WITHOUT USING VECTORS!

IMPORTANT: In case you somehow missed it, we are not allowed to use vectors for this assignment.

so the general outline is:

//blahblahblah, #includes and other starting things

int myFunc(//prototype-what the heck goes here?)    

int main()
{
    //arrays declared
    string names[MAX_NUM];
    double scores[MAX_NUM];
    //...other stuff main does, including calling myFunc...
}

int myFunc( //header-what the heck goes here?)
{
    //Code here to find highest, lowest, and mean scores from data in scores[]
}

Obviously what is in each indicated "what the heck goes here?" location will be related to what's in the other. But I don't know how to make it all work and every answer I've been able to find just says to use vectors. Which we haven't covered yet and therefore cannot use... Help, please?

template<std::size_t size>
int myFunc(std::string (&names)[size]);

int myFunc(std::string *names, std::size_t numberOfNames);

int myFunc(std::string *names); //implicitly assume names points to MAX_NUM strings
//blahblahblah, #includes and other starting things

int myFunc(string names[], double scores[], int elementCount);

int main()
{
   //arrays declared
   string names[MAX_NUM];
   double scores[MAX_NUM];
   //...other stuff main does, including calling myFunc...
   myFunc(names, scores, elementCount);
}

int myFunc(string names[], double scores[], int elementCount)
{
   //Code here to find highest, lowest, and mean scores from data in scores[]
}

I would probably use

myFunc(std::string* names, double* scores, std::size_t n_students) { /*...*/ }

Alternatively, being over-exact with the no-vectors-policy, you could use a std::list<std::pair<std::string, double>> or something alike...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM