Sample Input 1
5
1
2
3
4
5
g
Sample Output 1
6
Sample Input 2
1
5
g
#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) {
if (number_of_students <= 0)
return 0;
if (gender == 'g') {
gender = 'b';
marks++;
}
return *marks + marks_summation(marks + 2, number_of_students - 2, gender);
}
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;
}
5
1
2
3
4
5
g
Sample Output 1
6
Sample Input 2
1
5
g
Solution in C:
#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) {
if (number_of_students <= 0)
return 0;
if (gender == 'g') {
gender = 'b';
marks++;
}
return *marks + marks_summation(marks + 2, number_of_students - 2, gender);
}
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;
}
No comments:
Post a Comment
Thanks..