You are given an array of integers marks denoting the marks scored by students in a class.
- The alternating elements marks[0], marks[2], marks[4], and so on denote the marks of boys.
- Similarly,marks[1],marks[3],marks[5] and so on denote the marks of girls.
Write a program to find the sum of marks of the given gender.
Example:
Input: marks[]={1,2,3,4,5}, gender='g'
Output: 6
Approach
C
#include <stdio.h>int marks_summation(int *marks, int n, char gender){//Write your code here.int ans = 0;if (gender == 'b'){for (int i = 0; i < n; i += 2){ans += marks[i];}}else{for (int i = 1; i < n; i += 2){ans += marks[i];}}return ans;}int main(){int marks[] = {1, 2, 3, 4, 5};char gender = 'g';int n = sizeof(marks) / sizeof(marks[0]);int sum = marks_summation(marks, n, gender);printf("%d", sum);return 0;}
No comments:
Post a Comment