Is there a mistake somewhere?

Finding max, min and avg of the array. Is there a mistake somewhere? In one test it shows RTE - round time error. How to fix it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int read(int mas[],int n)
{
    ifstream f("dan.txt");
    if(f)
    {
        int i=0;
        for( ; (f>>mas[i])&& i<n ; i++)
        ;
        f.close();
        return i;
    }
    return 0;
}
int sk(int mas[],int n,int& max, int& min, double& sum)
{
    sum=0;
    max=min=0;
    int z=read(mas,n);
    if (z>0)
    {
        max=mas[1];
        min=mas[1];
        for (int i=1; i<z; i++)
        {
            sum += mas[i];
            if (mas[i]<min)
                min=mas[i];
            if (mas[i]>max)
                max=mas[i];
        }
    }
    return z;
}
int main()
{
    int max, min;
    double sum=0;
    const int N=10;
    int mass[N];
    int z=sk(mass,N,max,min,sum);
    if (mass[0]==0) {
    ofstream out ("rez.txt");
    out<<"0"<<" ";
    out<<"0"<<" ";
    out<<"0.00";}
    else {
    ofstream out ("rez.txt");
    out<<min<<" ";
    out<<max<<" ";
    out<<setprecision(2)<<fixed<<sum/mass[0];}
    return 0;
}
Hello DellXT,

I started to test your program and realized I have no idea what is in the "dan.txt" file.

It is better if I use what you are using, so that we get the same outcome.

FYI line 44 is calling a function with uninitialized variabes and these variables have yt to receive a value yet. This will have undetermined results, but make no difference because "z" is never used.

Hope that helps,

Andy
In one test it shows RTE - round time error.

What test? What input data caused that to happen?

Also, "RTE" usually stands for "Run Time Error".

I strongly recommend stepping through your code with a debugger, so that you can examine what's happening at each step, and see what's causing your crash.

EDIT: Is there a reason your sk method is ignoring the first element of the mas array?
Last edited on
Line 25/26: You need to set min/max to mas[0] not 1.
Since you start with 1 you also need to set sum to mas[0]
Right,
dan.txt is a file with the initial data (in this case
5 // it shows how many elements in array, it's mas[0] and that's why it is ignored
13 3 4 -1 16 // mas[1]...mas[5]
By the way, I've found what causes RTE - it's because of 'mas' and 'mass'.
Even in fixed code test server gives WA2 - wrong answer.

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int read(int mas[],int n)
{
ifstream f("dan.txt");
if(f)
{
int i=0;
for( ; (f>>mas[i])&& i<n ; i++)
;
f.close();
return i;
}
return 0;
}
int sk(int mas[],int n,int& max, int& min, double& sum)
{
sum=0;
max=min=0;
int z=read(mas,n);
if (z>0)
{
max=mas[1];
min=mas[1];
for (int i=1; i<z; i++)
{
sum += mas[i];
if (mas[i]<min)
min=mas[i];
if (mas[i]>max)
max=mas[i];
}
}
return z;
}
int main()
{
int max, min;
double sum=0;
const int N=10;
int mas[N];
int z=sk(mas,N,max,min,sum);
if (mas[0]==0) {
ofstream out ("rez.txt");
out<<"0"<<" ";
out<<"0"<<" ";
out<<"0.00";}
else {
ofstream out ("rez.txt");
out<<min<<" ";
out<<max<<" ";
out<<setprecision(2)<<fixed<<sum/mas[0];}
return 0;
}
HackerRank challenges or something? ;D

If so, strange that they'd want an output file.
Last edited on
Why are you posting your code without code tags like an idiot?
Topic archived. No new replies allowed.