Why i get this result?

Finally debuged my program, but when I compile it with a test data series, always get wrong result a single 0 in proba.txt file.

The code is:

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
65
66
67
68
//degree distribution with visibility transform

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <math.h>
using namespace std;

#define adatszam 3

int fokszam;
int i;
int a,b,c;
float ya,yb,yc;
float data[adatszam];
int k[adatszam];
FILE  *inf, *outf;


int main() {
    inf = fopen("proba.txt","r");
    for (i=0; i<adatszam; i++)
    {
    fscanf(inf,"%d %f", &k[i], &data[i]);
    }
    fclose(inf);
    for (i = 0; i < adatszam; ++i) {
        fokszam = 0;
        a = k[i];
        ya = data[i];
        for (int n = 0; n < adatszam; ++n) {
            if (n==i) continue;
            b = k[n];                        
            yb = data[n];
            int sz = 0;
            if (i>n){
                     for (int a = n+1; a<i; a++) {
                         c = k[a];
                         yc = data[a];
                         if (yc < yb + (ya-yb)*((b-c)/(b-a)))
                            sz += 1;
                         if (sz==(i-n+1))
                            fokszam+=1;
                            }
                     }
            else {
                 for (int a = n-1; a>i; a--) {
                         c = k[a];
                         yc = data[a];
                         if (yc < yb + (ya-yb)*((b-c)/(b-a)))
                            sz += 1;
                         if (sz==(i-n+1))
                            fokszam+=1;
                                   }
                 }
         ofstream myfile ("probagraf.txt");
         if (myfile.is_open())
         {
         myfile << fokszam << '\n';
         }
         myfile.close();
    }
}
    cout << "Elkeszultem a kiertekelessel";
    cin.get();
    return 0;
}


It runs succesfully because i get the line "Elkeszultem a kiertekelessel" (at the end of the program, see in line 65).
If anyone have any idea why I got this obviously wrong result, pls help me out.

P.s.: if u need clarification about what sequence does what just ask!
Last edited on
Would you care to share the results of your debugging? If you already debugged it, then you should have identified the problem already.

I just have a couple of words of advice. The bracket alignment throughout is inconsistent and very difficult to read. Secondly, I see you are using some C++ headers but still writing this as if it were a C program. I find it odd that you are using fscanf to read the input file but using C++ file stream to write to the output file. Why would you want to do that?

I don't understand the problem that you are describing. proba.txt is the input file, correct? What is the content of that file prior to running the program? Are you saying that the fscanf is not reading the input file correctly? In other words, 0 is being read even though the file contains something else? Did you check inf in the debugger to verify that it is not 0? In other words, did the file actually exist and was it opened properly?
Last edited on
Topic archived. No new replies allowed.