Printf returns 0

0

The file ends.txt has two columns, the first one corresponds to the last ending of a number and the second one to the number of times a finished number has appeared in it. The objective of the program is to show the ending that has appeared more times and how many times it has appeared but when executing the two values both the termination and the number of times it has appeared give me 0. Help pls.

#include <stdio.h>

int main() {
    int num[10], veces[10], i, x, y = 0;
    FILE *f1;
    f1 = fopen("termina.txt", "r");
    if (f1 == NULL) {
        printf("Error al abrir el archivo");
        return -1;
    } else {
        for(i = 0; i <= 9; i++) {
            fscanf(f1,"%d %d", &num[i], &veces[i]);
            if (veces[i] > y) {
                veces[i] = y;
                num[i] = x;
            }
        }
        fclose(f1);
        printf("Terminacion que mas veces ha aparecido: %d (%d veces)", x, y);
    }
    /*El archivo de texto termina.txt contiene lo siguiente:
    0 728
    1 732
    2 736
    3 697
    4 738
    5 749
    6 705
    7 732
    8 726
    9 724
    */
    return 0;
}
    
asked by Juanra Granados 15.10.2018 в 12:28
source

2 answers

4

You are doing the assignment the other way around. This:

if (veces[i]>y){
    veces[i]=y;
    num[i]=x;
    }

It should be like this:

if (veces[i]>y){
    y=veces[i];
    x=num[i];
    }

Otherwise, what you do is overwrite veces and num .

    
answered by 15.10.2018 / 12:41
source
1

The function printf does not return 0, surely it returns a value between 50 and 60. The return value of printf is the number of characters you have written, in your case you write a string of about 50 characters with two numbers between 1 and 3 digits.

What worries you is not what returns printf but what you write; before going into detail in what is your failure I will point out some problems with your code:

answered by 16.10.2018 в 08:05