Program does not execute anything when it comes into function

5

My program runs, but soon after asking for the value prob() and .exe to work.

I thought it was some communication problem between functions because of the lattice[][4] array as an argument, but I've already tried, fixed, and the same thing still happens. I'm pretty sure the problem is in the communication between main() and label() .

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int random();
float prob();
int label(float lattice[][4],float);
int i, j;

int random()   /* Sorteia um número entre de 0 a 1 */
{
    float x;
    srand((unsigned)time(NULL));
    x = rand()%100+1;
    x = x/100;
    return x;
}

float prob()
{
    float p;
    printf("Probabilidade: ");
    scanf("%f", &p);
    return p;
}

int label(float lattice[][4], float p)
{

for(i=0; i<4 ; i++)
    for(j=0; j<4; i++)
    {
        lattice[i][j] = random();   
    }

for(i=0; i<4; i++)
    for(j=0; j<4; j++)
    {
        if(lattice[i][j] <= p)      
            lattice[i][j] = 1;
        else
            lattice[i][j] = 0;
    }
return 0;
}

int main()
{
     float lattice[4][4];
     float p = prob();
     label(lattice, p);
     system("pause");
     return 0;
}
    
asked by anonymous 25.07.2015 / 00:26

1 answer

3

The error is that you are incrementing the second loop with the wrong variable:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int random() {
    float x;
    srand((unsigned)time(NULL));
    x = rand()%100+1;
    x = x/100;
    return x;
}
float prob() {
    float p;
    printf("Probabilidade: ");
    scanf("%f", &p);
    return p;
}
int label(float lattice[][4], float p) {
    for(int i = 0; i < 4 ; i++)
        for(int j = 0; j < 4; j++) { // <====== mudei aqui, estava i
            lattice[i][j] = random();   
        }
    for(int i = 0; i < 4; i++)
        for(int j = 0; j < 4; j++) {
            if(lattice[i][j] <= p)      
                lattice[i][j] = 1;
            else
                lattice[i][j] = 0;
        }
    return 0;
}
int main() {
    float lattice[4][4];
    float p = prob();
    label(lattice, p);
    return 0;
}

See working on ideone .

Note that I've improved the code a bit. It would still be cool to use keys even where you do not have to. Even because it already uses in some places, so keep the consistency. This is very important in programming.

When this is the case, isolate the problem by either printing out the value of the variables step by step or using a debug tool that shows you. Seeing the program run is the way to find the problem. It was basically what I did to find the problem, so I needed the code in conditions to compile.

    
25.07.2015 / 00:48