URI Online Judge | 1117
Score Validation
Adapted by Neilor Tonin, URI Brazil
Timelimit: 1
Write a program that reads two scores of a student. Calculate and print the average of these scores. Your program must accept just valid scores [0..10]. Each score must be validated separately.
Input
The input file contains many floating-point numbers, positive or negative. The program execution will be finished after the input of two valid scores.
Output
When an invalid score is read, you should print the message "nota inválida".
After the input of two valid scores, the message "média = " must be printed followed by the average of the student. The average must be printed with 2 numbers after the decimal point.
After the input of two valid scores, the message "média = " must be printed followed by the average of the student. The average must be printed with 2 numbers after the decimal point.
Input Sample | Output Sample |
-3.5 3.5 11.0 10.0 | nota invalida nota invalida media = 6.75 |
URI Online Judge Solution 1117 || Score Validation in C
#include <stdio.h>
int main()
{
float x = -1, y = -1;
do{
scanf("%d", &x);
if(x < 0 || x > 10)
printf("nota invalida\n");
}while(x < 0 || x > 10);
do{
scanf("%d", &y);
if(y < 0 || y > 10)
printf("nota invalida\n");
}while(y < 0 || y > 10);
printf("media = %.2f\n", ((x + y) / 2));
return 0;
}
URI Online Judge Solution 1117 || Score Validation in JAVA
import
java.io.IOException;
import
java.util.Scanner;
public
class
Main {
public
static
void
main(String[] args)
throws
IOException {
float
firstScore,media =
0
,total =
0
,total2 =
0
;
Scanner input =
new
Scanner(System.in);
while
(total2 !=
2
) {
firstScore =input.nextFloat();
if
(firstScore >=
0.0
&& firstScore <=
10.0
) {
total += firstScore;
total2 +=
1
;
}
else
{
System.out.print(
"nota invalida\n"
);
}
}
media =(
float
) total /
2
;
System.out.printf(
"media = %.2f\n"
,media);
}
}
URI Online Judge Solution 1117 || Score Validation in C++
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float x = -1, y = -1;
do{
cin >> x;
if(x < 0 || x > 10)
cout << "nota invalida" << endl;
}while(x < 0 || x > 10);
do{
cin >> y;
if(y < 0 || y > 10)
cout << "nota invalida" << endl;
}while(y < 0 || y > 10);
cout << "media = " << fixed << setprecision(2) << ((x + y) / 2) << endl;
return 0;
}
Dropbox Download link the solution
URI Question Link
No comments:
Write commentsTo know more about the problem, give us your valuable commment. We'll try to help you. Thanks