Student Marks Sum || Hacker Rank

 Question 17 || Hacker Rank

Student Marks Sum || Hacker Rank

Student Marks Sum


You are given a variety of whole numbers, marks, signifying the imprints scored by understudies in a class.

 

The rotating components , marks0,marks2,marks4 etc signify the characteristics of young men.

Likewise, marks1,marks3,marks5, etc signify the characteristics of young ladies.

The cluster name, marks, functions as a pointer which stores the base location of that exhibit. At the end of the day, contains the marks location where is put away in the memory.

 

For instance, let and stores marks = [3, 2, 5]. Then, is stores 0x7fff957c05f and 0x7fff9575c05f is the memory address of marks0.


Student Marks Sum

 


Capability Depiction

Complete the capability, marks_summation in the manager beneath.

 

marks_summation has the accompanying boundaries:

 

·      int marks[number_of_students]: the imprints for every understudy

·      int number_of_students: the size of marks[]

·      scorch orientation: either 'g' or 'b'

Returns

 

·      int: the amount of imprints for young men if gender = b, or of signs of young ladies if  gender = g

Input Arrangement

The principal line contains number_of_students, signifying the quantity of understudies in the class, thus the quantity of components in marks.

Every one of the resulting lines contains number_of_students.

The following line contains marksi.

Limitations


Student Marks Sum


 

Test Information 0

3

3

2

5

B

 

Test Result 0

8

 

Clarification 0

 

marks = [3, 2, 5] and gender = b .

 

Thus,

marks0 + marks2 = 3+5 = 8

 

Test Information 1

5

1

2

3

4

5

G

 

Test Result 1

6

 

Clarification 1

marks = [1, 2, 3, 4, 5] and gender = g

 

Thus,

Sum = marks1 + marks3 = 2+5 = 8.

 

Test Information 2

1

5

G

 

Test Result 2

0

Clarification 2

marks = [5] and gender = g

Here, marks1 doesn't exist. Thus, sum = 0.



#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)
{
    int sum = 0;
    for(int i = (gender == 'b' ? 0 : gender == 'g' ? 1 : -1); 
i < number_of_students; 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;
}


Output:

Student Marks Sum



Post a Comment

0 Comments