URI / BEE CROWD 1118 - Several Scores with Validation - Solution in C,C++,Python

URI / BEE CROWD 1118 - Several Scores with Validation - Solution in C,C++,Python | URI - BEECROWD - BEE 1118 Solution in C,C++,Python

                                                                                        beecrowd | 1118

Several Scores with Validation

Adapted by Neilor Tonin, URI  Brazil

                                                                                             Timelimit: 1


URI / BEE CROWD 1118 - Several Scores with Validation - Solution in C,C++,Python | URI - BEECROWD - BEE 1118 Solution in C,C++,Python:

Write an program to read two scores of a student. Calculate and print the semester average. The program must accept only valid scores (a score must fit in the range [0.10]). Each score must be validated separately.

The program must print a message "novo calculo (1-sim 2-nao)" that means "new calculate (1-yes 2-no)". After, the input will be (1 or 2). 1 means a new calculation, 2 means that the execution must be finished.

Input

The input file contains several positive or negative floating-point (double) values​. After the input of 2 valid scores, an integer number X will be read. Your program must stop when X = 2.

Output

If an invalid score is read, must be printed the message "nota invalida". When two valid scores are read, the message "media = " must be printed folowed by the average between these 2 scores. The message "novo calculo (1-sim 2-nao)" must be printed after reading X. This message should be displayed again if the standard input number for X is less than 1 or greater than 2, as example below.

The output average must be printed with 2 digits after the decimal point.

Input SampleOutput Sample

-3.5
3.5
11.0
10.0
4
1
8.0
9.0
2

nota invalida
nota invalida
media = 6.75
novo calculo (1-sim 2-nao)
novo calculo (1-sim 2-nao)
media = 8.50
novo calculo (1-sim 2-nao)

N.B: Don't copy paste the code as same. Just try to understand it and try yourself.

  URI / BEE 1118 Solution in C :   

  URI / BEECROWD Online Judge 1118 Solve  in C :                                          

#include<stdio.h>
int main()
{
    float n,s,p,k;
    s = p = k = 0;
    while(1)
    {
        scanf("%f",&n);
        if(n < 0.0 || n > 10.0)
            printf("nota invalida\n");
        else
        {
            s += n;
            p++;
            if(p==2)
            {
                s/=2;
                printf("media = %.2lf\n",s);
                printf("novo calculo (1-sim 2-nao)\n");
                while(1)
                {
                    scanf("%f",&n);
                    if((int)n==1)
                    {
                        s = p = 0;
                        k=1;
                        break;
                    }
                    else if((int)n==2)
                        return 0;
                    else
                        printf("novo calculo (1-sim 2-nao)\n");
                }
            }
        }
    }
    return 0;
}

1 Response to URI / BEE CROWD 1118 - Several Scores with Validation - Solution in C,C++,Python


  1. #include
    int main()
    {
    double m,n,p,q,t;
    int x;
    do
    {
    do
    {
    scanf("%lf",&m);
    if(m>=0 && m<=10)
    {
    p=m;
    }

    else printf("nota invalida\n");
    }
    while(m<0 || m>10);

    do
    {
    scanf("%lf",&n);
    if(n>=0 && n<=10)
    {
    q=n;
    }

    else printf("nota invalida\n");
    }
    while(n<0 || n>10);

    t=(p+q)/2;
    printf("media = %.2lf\n",t);

    do
    {
    printf("novo calculo (1-sim 2-nao)\n");
    scanf("%d",&x);
    if(x==2) break;
    }
    while(x!=1);
    }
    while(x==1);

    return 0;
    }

    ReplyDelete