Bifalgorithm post · Computer science one · Passing Arrays of Structures

How do you pass an array of structures to a function to compute an average GPA?

How do you pass an array of structures to a function to compute an average GPA? (Computer science one · Passing Arrays of Structures) — "We often also have nee…

Ideas in this flowchart

  • Passing arrays of structures to functions
  • A student roster can be passed to compute the average GPA
  • The function is declared as computeAverageGpa(const Student *roster, int n)
  • roster refers to the array of Student structures
  • n specifies the number of students in the roster
  • The function iterates through the roster
  • Each student's GPA is added to sum
  • The function returns sum divided by n
  • The array of structures is not passed by value
  • The bytes for each Student structure are not copied onto the call stack
Feed/

How do you pass an array of structures to a function to compute an average GPA?

No cover

Channel

Computer science one

1 page

"We often also have need to pass arrays of structures to functions. As an example, consider passing an entire roster of students to a function in order to compute the average GPA. If we have an array of structures, we would have a function that looked something like the following.\n\n/**\n\n* Computes the average GPA of the Student structures in\n\n* the given roster (which is of size n).\n\n*/\n\ndouble computeAverageGpa(const Student *roster, int n) {\n\ndouble sum = 0.0;\n\nint i;\n\nfor(i=0; i<n; i++) {\n\nsum += roster[i].gpa;\n\n}\n\nreturn sum / n;\n\n}\n\nWhen we pass in the array of structures, it is not passed by value. That is, the total number of bytes for each student is not copied onto the call stack."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.