Question

I am new to C programming. I want to use a while loop to check for the number 500.00 in a line of the 'grid' matrix and to go to the next line if it's not there. For this purpose I wrote this program, but unfortunately it doesn't show any results and I don't know what the problem is.

The program that I have written is here:

for(i=0;i<12;i++){
    c=0;
    for (j=0;j<26;j++){
        while(grid[i][j]!=500.00 && c<=ncmax );
        c++;
    }
    printf("%d \n \n",c);
} 

I changed the while loop to

 while(&grid[i][j]!=500.00  && c<=ncmax ); 

but it shows these errors

error C2440: '!=' : cannot convert from 'double' to 'float *'
error C2446: '!=' : no conversion from 'double' to 'float *'

What should I do with this problem? In general, am i able to use the while loop like this? If you need to see the whole program please let me know.

Was it helpful?

Solution 2

I don't understand your while. Don't you want a if instead?

for(i=0;i<12;i++) {
    c=0;
    for (j=0;j<26;j++) {
        if(grid[i][j]!=500.00  && c<=ncmax) {
            c++;
        } else {
            printf("%d \n \n",c);
        }
    }
}

But I think you could go for something simpler:

for(i=0;i<12;i++) {
    for (j=0;j<26;j++) {
        if(grid[i][j]==500.00) {
            printf("%d %d \n \n",i , j);
        }
    }
}

EDIT: I just noticed that in the first program, c and j have the same value. You could simplify to:

for(i=0;i<12;i++) {
    c=0;
    while(c<26 && c<=ncmax && grid[i][c]!=500.00) {
        c++;
    }
    printf("%d \n \n",c);
}

The output should be the list of c. It is equals to the minimum of 26 or ncmax+1 or the index of the value 500.00 in the line i.

PS: If you know the value of ncmax, you could simplify the condition.

OTHER TIPS

You have a while loop that doesn't do anything, and I suspect it's redundant as well. Look at it carefully; note how it checks for something, but if that something is not found, it doesn't do anything to change the state of affairs. Also look at your semi-colon; that's basically the body of your loop now.

I suspect you want to actually put the check inside your second for-statement or turn that for-statement into a while loop.

Let's take a closer look at what your code is doing, and then what I think you want it to do. I've changed the whitespace and indenting to make it more readable.

for (i = 0; i < 12; i++) { /* each column */
    c = 0;
    for (j = 0; j < 26; j++) { /* each row in this column */
        while(grid[i][j] != 500.00 && c <= ncmax)
            ; /* this is the body of the while loop: do nothing */
        c++;
    }
    printf("%d \n \n",c);
}

Since the body of the while loop does nothing, then if both conditions are true when entering the loop, it will continue to loop forever. You have probably already discovered this.

If your goal is to count the number of values in each column that are equal to 500.0, but stop counting if you reach a maximum number, consider the following approach.

for (i = 0; i < 12; i++) { /* each column */
    c = 0;
    for (j = 0; j < 26 && c <= ncmax; j++) { /* each row */
        if (grid[i][j] == 500.0)
            c++;
    }
    print("%d\n\n", c);
}

By the way, you need to be careful about comparing a float or double for equality. It's better to use closeness.

if (abs(grid[i][j] - 500.0) < EPSILON)
    c++; /* value is "close enough" */

EDIT Fix minor indenting typo. Populate body of last code block.

EDIT #2 If the goal is to find where the 500.0 occurs in each column, then the inner loop can be changed to a while loop as follows, while eliminating a variable.

for (i = 0; i < 12; i++) { /* each column */
    j = 0;

    while (j < 26 && grid[i][j] != 500.0)
        j++; /* each row this column, or until 500.0 */

    if (j == 26)
        print("ERROR: sentinel missing in column %d\n", i)
    else
        print("%d\n\n", j);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top