2

Basically I have,

    double marks [STUDENTS][ASSIGNMENTS] = {    (0.1,0.2,0.3,0.4,0.5,0.6,0.7),
                                            (1,0.9,0.8,0.7,0.6,0.5,0.5),
                                            (0.8,0.8,0.8,0.8,0.8,0.8,0.8),
                                            (0.8,0.9,0.7,0.8,0.9,0.7,0.8),
                                            (0.5,0.6,0.7,0.8,0.9,0.5,0.9)};

And I want to get: double studentAverages [STUDENTS] from studentAverages=calculateStudentAverages(marks);

Using:

    double calculateStudentAverages (double marks[STUDENTS][ASSIGNMENTS]){
    double averages[STUDENTS];
    double average;
    for (int i = 0; i < STUDENTS; i++) {
            for (int j = 0; j < ASSIGNMENTS; j++) {
                average = average + marks[i][j];

            }
            averages[i]=average/ASSIGNMENTS;

    }    
    return averages;
}

But I get "cannot convert from 'double [5]' to 'double'" and "cannot convert from 'double' to 'double [5]'"

4
  • On which lines are you getting the errors? Commented Nov 8, 2012 at 18:57
  • You should avoid raw arrays as they behave strangely Commented Nov 8, 2012 at 19:04
  • The line where the function is called I get: '=' : cannot convert from 'double' to 'double [5]' The return line in the functions gives: 'return' : cannot convert from 'double [5]' to 'double' Commented Nov 8, 2012 at 19:08
  • Welcome on SO. Did you know that you can edit your own question to modify or add more information? Please have a look to our FAQ as well stackoverflow.com/faq. Commented Nov 8, 2012 at 19:20

1 Answer 1

3

One of your errors is from the fact that your function is declared to return a single double but you are attempting to return an array of doubles. So you either need to change your function heading to accomodate for that, or only return one double

Not related to the errors, but you should definitely initialize average to 0.0 when you start, otherwise you have undefined behavior in the calculation

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

5 Comments

.. and averages can't really be returned from the function because they are local
I just need the averages of each row returned by the function as a vector but have no idea how.
@user1810269, bames53's link in your original question contains helpful information aso to how you should change your code to avoid errors and have well defined behavior
How can I just make averages a non-local array.
Do not do that. Learn instead!

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.