Students Marks Sum – Hacker Rank Solution | HackerRank Programming Solutions | HackerRank C Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language C . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight you profile to the recruiters.

In this post, you will find the solution for Students Marks Sum in C-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

C is one of the most widely used Programming Languages. it is basically used to build Operating System. C was developed by Dennis Ritchie in 1972. Below are some examples of C Programming which might you understanding the basics of C Programming.

You are given an array of integers, marks, denoting the marks scored by students in a class.

  • The alternating elements marks0marks2marks4, and so on denote the marks of boys.
  • Similarly, marks1marks3marks5, and so on denote the marks of girls.

The array name marks, works as a pointer which stores the base address of that array. In other words, marks contains the address where marks0 is stored in the memory.For example, let marks = [3,2,5] and marks stores 0x7fff9575c05f. Then, 0x7fff9575c05f is the memory address of marks0.

Students Marks Sum HackerRank

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 girl if gender = g

The locked code stub reads the elements of marksalong 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.


Input Format

  • The first line contains number_of_students, denoting the number of students in the class, hence the number of elements in marks.
  • Each of the number_of_students subsequent lines contains marksi.
  • The next line contains gender.

Constraints

  • number_of_students <=10^3
  • 1<=marksi<=10^3(where 0<= i< number_of_students)
  • gender = g or b

Output Format

The output should contain the sum of all the aternate elements in marks as explained above.


Sample Input 0

3
3
2
5
b

Sample Output 0

8

Explanation 0

marks = [3,2,5] andgender = b.
somarks0+marks2 = 3+5 = 8.

Sample Input 1

5
1
2
3
4
5
g

Sample Output 1 

6

Explanation 1

marks = [1,2,3,4,5] andgender = g.
so Sum = marks1+marks3 = 2+5 = 8.

Sample Input 2

1
5
g

Sample Output 2 

0

Explanation 2

marks = [5] andgender = g.
here does marks1 not exist. so Sum = 0

Students Marks Sum – Hacker Rank Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

//Complete the following function.

int marks_summation(int* marks, int number_of_students, char gender) 
{
  //Write your code here.
  int sum = 0;
    for (int i = (gender == 'b' ? 0 : 1); i < number_of_students; i = i + 2) 
        sum += *(marks + i);
    
    return sum;
}
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;
}

Leave a Comment

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh