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.



  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. int marks_summation(int* marks, int number_of_students, char gender) {
  6. int s = 0, i = 0;
  7. if (gender == 'g') {
  8. i++;
  9. }
  10. for (; i < number_of_students; i = i+2) {
  11. s += marks[i];
  12. }
  13. return s;
  14. }
  15. int main() {
  16. int number_of_students;
  17. char gender;
  18. int sum;
  19. scanf("%d", &number_of_students);
  20. int *marks = (int *) malloc(number_of_students * sizeof (int));
  21. for (int student = 0; student < number_of_students; student++) {
  22. scanf("%d", (marks + student));
  23. }
  24. scanf(" %c", &gender);
  25. sum = marks_summation(marks, number_of_students, gender);
  26. printf("%d", sum);
  27. free(marks);
  28. return 0;
  29. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube