lab8a.c - Here we print exactly the same thing but show...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <stdio.h> // Forward Declarations int getStudentCount(int enrollments[], int numSections); int main(int argc, char * argv[]) { // We'll need an index variable int i; // Declare an array of 20 characters, and initialize it from a string. char className[20] = "Computing 1 Lab"; // Declare an array of lab sections, and initialize it with the number // of students in each section. The lab section numbers are 1¬4, but the // indexes here are 0¬3. (The size of the array is determined by the // number of initializer values. This array has four elements because there // are four initializer values.) int sectionEnrollment[] = { 25, 21, 23, 24 }; for (i = 0; i < 4; i++) { // The %s format marker means to print a string. The corresponding // argument (after the format string) is expected to be the address // of the first character of the string to be printed. Notice that // the corresponding argument is 'className'. Here we just utter the // name of the array 'className', and that means "address of the first // element of 'className". printf("%s section %d has %d students enrolled\n", className, i + 1, sectionEnrollment[i]);
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // Here we print exactly the same thing, but show that 'className' // means exactly the same thing as '&className[0]'. ±he ampersand is // the "address of" operator. You've seen it in when used with scanf. prinµ("%s secTon %d has %d students enrolled\n", &className[0], i + 1, secTonEnrollment[i]); prinµ("\n"); } // Show the total enrollment in all lab secTons prin±("²otal enrollment: %d\n", getStudentCount(secTonEnrollment, 4)); return 0; } /** * Calculate the total number of students in all lab secTons * * @param enrollments * Array of secTon enrollments * * @param numSecTons * ²he number of secTons (i.e., the number of array elements) * * @return * ²he total number of students enrolled in all lab secTons. */ int getStudentCount(int enrollments, int numSecTons) { int i; int sum; // Begin with no students counted sum = 0; // For each secTon. .. for (i = 0; i < numSecTons; i++) { // . .. add that secTon's enrollment to the sum sum += enrollments[i]; } // Give 'em the fnal sum: the count oF all students in all secTons return sum; }...
View Full Document

{[ snackBarMessage ]}

Sign up to access 24/7 study resources for your classes

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern