Skip to content

Instantly share code, notes, and snippets.

@jkwill87
Created January 28, 2016 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkwill87/e470934f992cc0351696 to your computer and use it in GitHub Desktop.
Save jkwill87/e470934f992cc0351696 to your computer and use it in GitHub Desktop.
#include <stdio.h>
/**
* average.c
*
* this program will allocate memory for an integer array on the stack, and
* demonstrate how C treats values passed by value as opposed to by reference.
*/
int averageGradeValue (int grades[], int count, int bonus);
int main() {
int grades[5] = {50, 60, 40, 30, 70};
int average = 0;
for (int i = 0; i < 5; i++) {
average += grades[i];
}
average /= 5;
printf ("Your average mark is %d. ", average);
puts (average < 50 ? "You failed!" : "You passed!");
average = averageGradeValue (grades, 5, 5); // passed value modified
printf ("Your average mark is %d. ", average);
puts (average < 50 ? "You failed!" : "You passed!");
return 0;
}
int averageGradeValue (int grades[], int count, int bonus) {
int average = 0;
for (int i = 0; i < count; i++) {
average += grades[i];
}
average /= count;
return average + bonus;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment