0

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?

9
  • what is wrong with vectors? Commented Aug 6, 2014 at 11:48
  • What did you try so far? What does your textbook say? What does your C++ programming reference/book/resource say about passing arrays to functions? Commented Aug 6, 2014 at 11:51
  • Google "equivalence of pointers and arrays". Commented Aug 6, 2014 at 11:52
  • 3
    Next question for you guys : how to ask questions WITHOUT SCREAMING IN THE EARS OF PEOPLE. Stay tuned. Commented Aug 6, 2014 at 11:55
  • 2
    using std::array (C++11) has a cleaner syntax than C-array. Commented Aug 6, 2014 at 12:05

3 Answers 3

3
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
Sign up to request clarification or add additional context in comments.

Comments

1
//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[]
}

2 Comments

The size has to be assumed with your solution :-/
that's easily modified
0

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...

Comments

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.