The F-score of an array of integers is calculated as follows: For each integer in the array, the F-score is 2 if the last digit or the second-last digit of the integer is divisible by 4, or 3 if both the last and second-last digits are divisible by 4, and 0 if none of those digits are divisible by for. Here second-last digit means the digit in the tens place. The F-score is never more than 3 For example: 138 scores 2, 142 scores 2, 32148 scores 3, and 17322 scores 0. The total F-score of an array is simply the sum of the F-scores of all the integers in the array For example: the array {138, 142, 32148, 17322} will score 2+2+3+0 = 7. If an integer does not have a second-last digit, it is assumed to be 0 (which is divisble by 4). Write a function in C which calculates the F-score of 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 F-score of t
Answers
Answered by
0
#include<stdio.h>
int main()
{
int n,i,lastdigit,secondlastdigit,addition=0;
printf("\nEnter no of array elements :");
scanf("%d",&n);
int a[n],fscore[n];
for (i=0;i<=n-1;i++)
{
printf("Array value %d :",i+1);
scanf("%d",&a[i]);
}
printf("\nArray : {");
for (i=0;i<=n-1;i++)
{
lastdigit=a[i]%10;
secondlastdigit=(a[i]/10)%10;
if(lastdigit%4==0&&secondlastdigit%4==0)
fscore[i]=3;
else if(lastdigit%4==0||secondlastdigit%4==0)
fscore[i]=2;
else
fscore[i]=0;
if(i!=n-1)
printf("%d,",a[i]);
else
printf("%d",a[i]);
}
printf("}");
printf("\nF-score : {");
for (i=0;i<=n-1;i++)
{
addition=addition+fscore[i];
if(i!=n-1)
printf("%d,",fscore[i]);
else
printf("%d",fscore[i]);
}
printf("}");
printf("\nAddition of F-scores : %d\n",addition);
return 0;
}
The Output is;
Enter no of array elements :6
Array value 1 :138
Array value 2 :142
Array value 3 :32148
Array value 4 :17322
Array value 5 :8
Array value 6 :5
Array : {138,142,32148,17322,8,5}
F-score : {2,2,3,0,3,2}
Addition of F-scores : 12.
int main()
{
int n,i,lastdigit,secondlastdigit,addition=0;
printf("\nEnter no of array elements :");
scanf("%d",&n);
int a[n],fscore[n];
for (i=0;i<=n-1;i++)
{
printf("Array value %d :",i+1);
scanf("%d",&a[i]);
}
printf("\nArray : {");
for (i=0;i<=n-1;i++)
{
lastdigit=a[i]%10;
secondlastdigit=(a[i]/10)%10;
if(lastdigit%4==0&&secondlastdigit%4==0)
fscore[i]=3;
else if(lastdigit%4==0||secondlastdigit%4==0)
fscore[i]=2;
else
fscore[i]=0;
if(i!=n-1)
printf("%d,",a[i]);
else
printf("%d",a[i]);
}
printf("}");
printf("\nF-score : {");
for (i=0;i<=n-1;i++)
{
addition=addition+fscore[i];
if(i!=n-1)
printf("%d,",fscore[i]);
else
printf("%d",fscore[i]);
}
printf("}");
printf("\nAddition of F-scores : %d\n",addition);
return 0;
}
The Output is;
Enter no of array elements :6
Array value 1 :138
Array value 2 :142
Array value 3 :32148
Array value 4 :17322
Array value 5 :8
Array value 6 :5
Array : {138,142,32148,17322,8,5}
F-score : {2,2,3,0,3,2}
Addition of F-scores : 12.
Similar questions
Math,
7 months ago
English,
7 months ago
India Languages,
7 months ago
Biology,
1 year ago
English,
1 year ago