HackerRank C- Students Marks Sum
Complete the function, marks_summation(int* marks, char gender, int number_of_students) which returns the total sum of:
marks of boys if gender=b
marks of girls if gender=g
The programs reads the elements of marks along with gender . Then, it calls the function marks_summation(marks, gender, number_of_students) to get the sum of alternate elements as explained above and then prints the sum.
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <stdlib.h>
- int marks_summation(int* marks, int number_of_students, char gender) {
- int s = 0, i = 0;
- if (gender == 'g') {
- i++;
- }
- for (; i < number_of_students; i = i+2) {
- s += marks[i];
- }
- return s;
- }
-
- int main() {
- int number_of_students;
- char gender;
- int sum;
-
- scanf("%d", &number_of_students);
- int *marks = (int *) malloc(number_of_students * sizeof (int));
-
- for (int student = 0; student < number_of_students; student++) {
- scanf("%d", (marks + student));
- }
-
- scanf(" %c", &gender);
- sum = marks_summation(marks, number_of_students, gender);
- printf("%d", sum);
- free(marks);
-
- return 0;
- }