Program does not execute anything when it enters function

Asked

Viewed 227 times

5

My program runs, but right after asking for the value prob() the .exe stop working.

I thought it was some communication problem between functions on account of the matrix lattice[][4] as an argument, but I have already searched, set up and the same thing keeps happening. I am almost sure that the problem lies in the communication between the main() and the 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;
}
  • 2

    Does it give any error? Edit and put the includes or anything else the code has. http://answall.com/help/mcve. De ccara I saw that your code does not even compile.

  • had not put complete, because the rest is all good, the business is the main() and the label(). But Taí, the complete code...

  • Boy, ta compiling here. But as I said, the . exe hangs after asking 'Probability': ''...

  • Did you debug? It hangs where exactly inside the function prob? Would be in the scanf? Locking is consistent (i.e., always occurs)?

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++) 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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Note that I improved the code a little. It would still be cool to use keys even where you don’t need them. Even because it is already used in some places, so maintain consistency. This is very important in programming.

When so, isolate the problem, print the value of the variables step by step or use a tool debug that will show to you. Seeing the program run is the way to find the problem. That’s basically what I did to find the problem, so I needed the code in order to compile.

  • 2

    Well observed. AP should not take this comment wrong, but running a debugging helps a lot to find this type of problem. Here’s a chance to learn from mistakes. :)

  • Hell, that’s all it was ... thanks, execution in order now.

  • 1

    Debugging is one of the best ways to learn. Listening to the most experienced is another :)

  • Debugging. Noted. :D

  • 3

    :) Other Higor things: 1) Welcome to SOPT. 2) If you haven’t already, do the [tour] and read [help]. It will help you a lot in the use of the community.

  • Thanks guys, very useful site. ;)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.