Printing best number from for loop

Hey all, hope I won't need to translate the whole code but what I need to do is to print which is the best number from the loop 45th line.
I have a text file and there is n = 4(how many there are melons) and their mass in array 4,3,7,8. I managed to find which is closest to the arithmetical average which is 4. But when I try to assign the number (nr) to the j I get to answer 0. And it should 1 because it's the first melon.

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
57
58
59
60
61
62
63
64
#include <iostream>
#include <fstream>
#include <cmath>
//
using namespace std;
//
const char duom[] = "d.txt";
const char rez[] = "r.txt";
const int size = 100;
//
void skaityti(int &n, int A[]);
void spausdinti(int geriausias, int nr);
void skaiciuoti(int n, int A[], int &geriausias, int &nr);
//
int main()
{
	int n, A[size], geriausias, nr;
	skaityti(n, A);
	skaiciuoti(n, A, geriausias, nr);
	spausdinti(geriausias, nr);
	return 0;
}
//
void skaityti(int &n, int A[])
{
    ifstream in(duom);
    in >> n;
    for(int i = 0; i != n; i++)
    {
        in >> A[i];
    }
    in.close();
}
//
void skaiciuoti(int n, int A[], int &geriausias, int &nr)
{
    int sum = 0;
    double vid, maz = 9999, kuris;

    for(int i = 0; i != n; i++)
    {
        sum += A[i];
    }
    vid = (double)sum / n;
    for(int j = 0; j != n; j++)
    {
        kuris = (double)abs(vid - A[j]);
        if(kuris < maz)
        {
            maz = kuris;
            geriausias = A[j];
            nr = j;
        }
    }
}
//
void spausdinti(int geriausias, int nr)
{
    ofstream out(rez);
    out << "Vidutinio arbuzo mase yra: " << geriausias << endl;
    out << "Jo numeris yra: " << nr << endl;
    out.close();
}
// 
Last edited on
Found issue

Apparently, it's 0 because I started the loop from 0. So I just did nr = ++j;
Last edited on
Topic archived. No new replies allowed.