URI Online Judge Solution 1182 Column in Array Using C, Python Programming Language.
Your job in this problem is to read a number that is a column of an array where an operation will be performed, an uppercase character, indicating the operation to be performed and all elements of a bidimentional array M[12][12]. Then, you have to calculate and print the sum or average of all elements within the green area according to the case. The following figure illustrates the case when is entered the number 5 to the array column, showing all elements that must be considered in the operation.
Input
The first line of the input contains a simple integer C (0 ≤ C ≤ 11) indicating the column to be considered in the operation. The second line of the input contains a single uppercase character T ('S' or 'M'), indicating the operation Sum or Average (Média in portuguese) to be performed with the elements of the array. Follow 144 floating-point numbers of the array.
Output
Print the calculated result (sum or average), with one digit after the decimal point.
Input Sample | Output Sample |
5 | 12.6 |
Solution Using C:
#include <stdio.h>
int main()
{
double a=0.0, M[12][12];
char T[2];
int C,x,y;
scanf("%d", &C);
scanf("%s", &T);
for(x=0;x<=11;x++)
{
for(y=0; y<=11; y++)
{
scanf("%lf", &M[x][y]);
if(y==C)
a+=M[x][y];
}
}
if(T[0]=='S')
printf("%.1lf\n",a);
else if(T[0]=='M')
{
a=a/12.0;
printf("%.1lf\n",a);
}
return 0;
}
Comments
Post a Comment