Search code examples
cwhile-loopequalityinequality

Testing for inequality with while() in C


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.


Solution

  • 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.