Write a Function in C to Calculate the O-score of a Given Array
The O-score of an array of integers is calculated as follows: For each integer in the array, the O-score is 2 if the last digit or the second-last digit of the integer is odd, or 3 if both the last and second-last digits are odd, and 0 if none of those digits are odd. Here second-last digit means the digit in the tens place. The O-score is never more than 3 For example: 107 scores 2, 172 scores 2, 32177 scores 3, and 17322 scores 0. The total O-score of an array is simply the sum of the O-scores of all the integers in the array For example: the array {107, 172, 32177, 17322} will score 2+2+3+0 = 7. If an integer does not have a second-last digit, it is assumed to be 0. Write a function in C which calculates the O-score of a given array. The prototype of the function should be as follows: int array_score(int a[], int len); Here, a is the array, and len is the length of the array. The function should return the O-score of the input array.
Answers
Answered by
20
int array_score (int a [], int len) {
for ( int oscore = 0, i=0 ; i< len ; i++ ) {
if (a[i] % 2)
if ((a[i] / 10) %2) oscore += 3;
else oscore += 2;
else
if ((a[i] /10) % 2) oscore += 2;
// else oscore += 0;
}
return oscore ;
}
for ( int oscore = 0, i=0 ; i< len ; i++ ) {
if (a[i] % 2)
if ((a[i] / 10) %2) oscore += 3;
else oscore += 2;
else
if ((a[i] /10) % 2) oscore += 2;
// else oscore += 0;
}
return oscore ;
}
kvnmurty:
:)
Similar questions