Bifalgorithm post · Computer science one · Passing Arrays of Structures

How do you pass an array of pointers to structures to a function?

How do you pass an array of pointers to structures to a function? (Computer science one · Passing Arrays of Structures) — "Nevertheless, as we’ve previously se…

Ideas in this flowchart

  • Computing the average GPA from an array of pointers to Student structures
  • Maintaining an array of pointers can be preferable in this context.
  • The function receives a roster of Student pointers and its size n.
  • The running sum is initialized to 0.0.
  • The loop processes each roster element while i is less than n.
  • Each student's GPA is added to the running sum.
  • The average is returned as sum divided by n.
  • The gpa member is accessed with the arrow operator instead of the dot operator.
Feed/

How do you pass an array of pointers to structures to a function?

No cover

Channel

Computer science one

1 page

"Nevertheless, as we’ve previously seen it is sometimes preferable to maintain an array of pointers to structures. If we had an array of pointers, we would have a function that looks 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\nThe only difference here is in how we access the gpa member variable using the arrow operator instead of the dot operator."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.