Friday, 18 March 2016

Solution of URI 1118 :: Several Scores with Validation


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,b,c,d,e,f,g,h=1,x=1,y=1,z=1;
    for(h=1;;h=1,x=1,y=1,z=1)
    {
        scanf("%lf",&a);
        if(a<0 || a>10)
        {
            printf("nota invalida\n");
            continue;
        }
        while(x==1)
        {
            scanf("%lf",&b);
            if(b<0 || b>10)
            {
                printf("nota invalida\n");
                continue;
            }
            x+=2;
        }
        c=(a+b)/2.0;
        printf("media = %.2lf\n",c);
        printf("novo calculo (1-sim 2-nao)\n");
        while(y==1)
        {
            scanf("%lf", &g);
            if(g<1 || g>2)
            {
                printf("novo calculo (1-sim 2-nao)\n");
                continue;
            }
            y+=2;
        }
        if(g==2)
            break;
    }
    return 0;
}

11 comments:

  1. why did you use the variables x,y and z ? And i don't understand this increment, can you explain too?

    ReplyDelete
  2. parleto expain korbo,,,,easy code gulare etto boro korce

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete

  4. why my code is shown wrong........everything is looking clear and acceptable........but it is showing wrong..






    #include
    int main()
    {
    int i,X=0;
    double a,c=0;
    for(i=0;;i++)
    {
    scanf("%lf",&a);
    if(a>=0&&a<=10)
    {
    c=c+a;
    X++;
    }
    else
    {
    printf("nota invalida\n");
    }
    if(X==2)
    {
    printf("media = %.2lf\n",c/2);
    X=0;
    {
    c=0;
    break;
    }

    }


    }
    for(i=0;;i++)
    {
    printf("novo calculo (1-sim 2-nao)\n");
    scanf("%lf",&a);
    if(a==1)
    {

    for(i=0;;i++)
    {
    scanf("%lf",&a);
    if(a>=0&&a<=10)
    {
    c=c+a;
    X++;
    }
    else
    {
    printf("nota invalida\n");
    }
    if(X==2)
    {
    printf("media = %.2lf\n",c/2);
    X=0;
    break;
    }
    }

    }
    else if(a==2)
    break;
    }



    return 0;
    }

    ReplyDelete
  5. note: goto statement make your code inefficient. :)
    #include
    int main()
    {
    float x,sum=0.0;
    int y=0,t,l;
    l:
    while(1)
    {
    if(y==2)
    {
    printf("media = %.2f\n",sum/2.00);
    printf("novo calculo (1-sim 2-nao)\n");
    while(scanf("%d",&t)==1)
    {
    if(t<1 || t>2)
    {
    printf("novo calculo (1-sim 2-nao)\n");
    continue;
    }
    if(t==1)
    {
    y=0;
    sum=0;
    goto l;
    }
    else
    goto m;
    }
    m:
    break;
    }
    scanf("%f",&x);
    if(x>=0.0 && x<=10.0)
    {
    y++;
    sum=sum+x;
    }
    else
    printf("nota invalida\n");
    }
    return 0;
    }

    ReplyDelete
  6. its more simple




    #include
    int main()
    {
    float score,sum=0,count=0,com;
    while(1)
    {
    if(count==2)
    break;
    scanf("%f",&score);
    if(score>=0&&score<=10)
    {
    count++;
    sum = sum + score;
    }
    else
    printf("nota invalida\n");
    }
    printf("media = %.2f\n",sum/2);

    while(1)
    {
    printf("novo calculo (1-sim 2-nao)\n");
    scanf("%f",&com);
    if(com==2)
    break;
    if(com!=1&&com!=2)
    continue;
    if(com==1)
    {
    float score,sum=0,count=0,com;
    while(1)
    {
    if(count==2)
    break;
    scanf("%f",&score);
    if(score>=0&&score<=10)
    {
    count++;
    sum = sum + score;
    }
    else
    printf("nota invalida\n");
    }
    printf("media = %.2f\n",sum/2);

    }
    }
    return 0;
    }

    ReplyDelete
  7. //Shazzed Hossain Shajal
    #include
    int main()
    {
    double x,y,av,n;
    while(1)
    {
    scanf("%lf",&x);
    if(x<0 || x>10)
    {
    printf("nota invalida\n");
    continue;
    }
    while(1)
    {
    scanf("%lf",&y);
    if(y<0 || y>10)
    {
    printf("nota invalida\n");
    continue;
    }
    break;
    }
    av=(x+y)/2.0;
    printf("media = %.2f\n",av);
    printf("novo calculo (1-sim 2-nao)\n");
    while(1)
    {
    scanf("%lf",&n);
    if(n!=1 && n!=2)
    {
    printf("novo calculo (1-sim 2-nao)\n");
    continue;
    }
    break;
    }
    if(n==2)
    {
    break;
    }
    }
    }

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete










  9. class Program
    {
    static void Main(string[] args)
    {

    {
    bool a, b; double soma = 0.0, media = 0.0, n1, n2; int x;

    do
    {

    do
    {
    n1 = double.Parse(Console.ReadLine(), CultureInfo.InvariantCulture);
    if (n1 > 0.0 && n1 <= 10.0)
    {
    a = true;
    soma = n1;
    }
    else
    {
    a = false;
    Console.WriteLine("nota invalida");
    }
    } while (a == false);

    do
    {
    n2 = double.Parse(Console.ReadLine(), CultureInfo.InvariantCulture);
    if (n2 > 0.0 && n2 <= 10.0)
    {
    b = true;
    soma = n1 + n2;
    }
    else
    {
    b = false;
    Console.WriteLine("nota invalida");
    }
    } while (b == false);

    media = soma / 2.0;
    Console.WriteLine("media = " + media.ToString("F2", CultureInfo.InvariantCulture));

    do
    {
    Console.WriteLine("novo calculo (1-sim 2-nao)");
    x = int.Parse(Console.ReadLine());
    } while (x < 1 || x > 2);


    } while (x != 2 || x == 1);
    }

    }
    }

    eu encadeei alguns "do" pra dar certo em C#.

    ReplyDelete
  10. Repeat teke boji nai vai, i don’t understand from repeat, please describ?

    ReplyDelete
  11. Isn't it is the easiest way to solve in C.
    #include

    int main()
    {
    double a,b=0,c=0,s=0;
    int x;
    while(scanf("%lf",&a))
    {
    if(a>=0&&a<=10)
    {
    b++;
    s+=a;
    }
    else printf("nota invalida\n");
    if(b==2)
    {
    printf("media = %.2lf\n",s/2);
    s=0;
    printf("novo calculo (1-sim 2-nao)\n");
    while(scanf("%d",&x))
    {
    if(x==1)
    {
    b=0;
    break;
    }
    else if(x==2)
    {
    c=x;
    break;
    }
    else printf("novo calculo (1-sim 2-nao)\n");
    }
    if(c==2) break;
    }
    }
    return 0;
    }//end of line

    ReplyDelete

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