Students Marks Sum in C hackerrank solution

      

  #include <stdio.h>
  int marks_summation(int* marks, int number_of_students, char gender) {
      int sum = 0;
      
      if (gender == 'b') {
          for (int i = 0; i < number_of_students; i += 2) {
              sum += marks[i];
          }
      } else if (gender == 'g') {
          for (int i = 1; i < number_of_students; i += 2) {
              sum += marks[i];
          }
      }
      
      return sum;
  }
  
  int main() {
      int number_of_students;
      scanf("%d", &number_of_students);
      
      int marks[number_of_students];
      for (int i = 0; i < number_of_students; i++) {
          scanf("%d", &marks[i]);
      }
      
      char gender;
      scanf(" %c", &gender);
      
      int total_marks = marks_summation(marks, number_of_students, gender);
      printf("%d\n", total_marks);
      
      return 0;
  }
  

  
     
            

Let's go through the solution step by step:

We include the header file to use the standard input/output functions.

The marks_summation() function takes three parameters: marks (an integer array representing the marks of students), number_of_students (an integer representing the total number of students), and gender (a character representing the gender for which the sum of marks needs to be calculated). It calculates and returns the sum of marks based on the given gender.

Inside the marks_summation() function, we declare an integer variable sum and initialize it to 0. This variable will store the sum of marks.

We use conditional statements to check the value of gender. If it is 'b', we iterate through the marks array starting from index 0 and incrementing by 2 (to consider every alternate student). In each iteration, we add the mark to the sum variable.

If the gender is 'g', we iterate through the marks array starting from index 1 and incrementing by 2 (to consider every alternate student). In each iteration, we add the mark to the sum variable.

Finally, we return the calculated sum as the total marks for the specific gender.

In the main() function, we declare an integer variable number_of_students to store the input value for the total number of students.

We use the scanf() function to read the input value from the user and store it in the variable number_of_students.

We declare an integer array marks of size number_of_students to store the marks of each student.

We use a for loop to iterate from 0 to number_of_students - 1. Inside the loop, we use the scanf() function to read the marks for each student and store them in the marks array.

We use the scanf() function again to read the input value for gender and store it in the variable gender. Note the space before %c in the format string to consume any whitespace characters (such as a newline) left in the input buffer.

We call the marks_summation() function with the marks array, number_of_students, and gender as arguments to calculate the total marks for the specified gender.

We use the printf() function to print the total marks.

By providing the expected input and executing the code, you should see the total marks printed according to the problem statement on HackerRank.