Created
January 28, 2016 22:15
-
-
Save jkwill87/e470934f992cc0351696 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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