Friday, 29 April 2016

Solution of URI 1181 :: Line in Array


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#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(x==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;
}

4 comments:

  1. char T[2];
    why in this code a character array has been read as input instead of reading a single character ?

    ReplyDelete
    Replies
    1. Yeah!!! You can do this as you say.
      This is possible one.
      I did this because i am comfortable in such way.

      Delete
  2. why this code went wrong?
    #include
    int main()

    {
    int a[12][12],i,j,n;
    double sum;
    scanf("%d",&n);
    char ch[100];
    gets(ch);
    for(i=0;i<4;i++)
    {
    for(j=0;j<4;j++)
    {
    scanf("%d",&a[i][j]);
    }
    }
    if(ch=='S'){
    sum=0;
    for(j=0;j<12;j++)
    {
    sum=sum+a[2][j];
    }
    printf("%lf\n",sum);
    }
    else if(ch=='M')
    {
    sum=0;
    for(j=0;j<12;j++)
    {
    sum=sum+a[2][j];
    }
    printf("%.1lf\n",sum/12.0);
    }
    return 0;

    }

    ReplyDelete
  3. ur code is not working bcz string(%s)...u should use char(%c)

    #include
    int main()
    {
    int i, j, a;
    double x[12][12], m=0;
    char ch;
    scanf("%d %c", &a, &ch);
    for(i=0;i<12;i++)
    {
    for(j=0;j<12;j++)
    {
    scanf("%lf", &x[i][j]);
    if(i==a)
    m+=x[i][j];
    }
    }
    if(ch=='S')
    printf("%.1lf\n", m);
    else if(ch=='M')
    printf("%.1lf\n", m/12.0);
    }

    ReplyDelete

Note: only a member of this blog may post a comment.