A for loop to set a 2D array in C

AI Thread Summary
The discussion revolves around issues with initializing a 2D array in C using nested for loops. The original code fails to set the expected alternating values of 0 and 200 due to incorrect incrementing of the inner loop index. A suggested alternative using a bitwise operation effectively alternates the values but raises concerns about handling odd loop counts. The conversation shifts to counting lines in a text file, with clarification on using `fgets` and the structure of the input file format. Ultimately, the user resolves their issues and successfully implements the desired functionality.
VirObitus
Messages
19
Reaction score
0
Is there anything wrong with this code? I am trying to make each set of read like this:
0,200,0,200,0,200,0,200... ect. for some reason the numbers are not being set correctly.

Code:
     for (i = 0; i < loopCount1; i++)
     {
         for (j = 0; j < loopCount2; j++)
         {
             responseLoop[i][j] = 0;
             j++;
             responseLoop[i][j] = 200;
         }
     }
 
Technology news on Phys.org
it should be some thing like this

Code:
     int nextIsZero=0;

     for (i = 0; i < loopCount1; i++)
     {
         for (j = 0; j < loopCount2; j++)
         {
             responseLoop[i][j] = (nextIsZero ? 0 : 200);
             nextIsZero = ~nextIsZero;
         }
     }
 
sorry for being a noob, but can you explain the code please? I've never seen the format (nextIsZero ? 0 : 200) or seen the tilde (~) used
 
The ~ performs the bitwise NOT operation on the integer.
(nextIsZero ? 0 : 200); translates to:

"If nextIsZero is true, then return 0, otherwise return 200".
 
If you are simply initializing the sets of values of responseLoope, without considering the existing values of the array, as shown in your code, your code is OK. However, if loopCount2 is odd, you may have a problem of array out-of-bound.
To solve that problem, you can try:
Code:
          if(loopCount2%2*2!=0)
          {printf("loopCount2 is odd, value=%d\n",loopCount2);
            ...do something
          }
         for (j = 0; j < loopCount2-1; j+=2)
         {
             responseLoop[i][j] = 0;
             responseLoop[i][j+1] = 200;
         }
However, if loopCount2 is odd, the last one will not be initialized.
 
for some reason when I check responseLoop values after doing any of the above suggestions, or my original code, the values are either 0's or are in the 200,000's. I am initializing them at those numbers (0,200...) and don't change their values or even use responseLoop anywhere else in the program right now.

*edit*
On second look, I made a mistake which is causing loopCount2 to = 1... which changes my question. Can I count the number of lines in a file? I was trying to use fscanf but then I realized that it won't count more than one line. I'm not really sure how fgets works, but this code is causing loopCount2 to equal 2:

if ((spResponses = fopen("c:\\responses.txt", "r")) == NULL) //open file
{
printf("Error opening input file!\n");
}

else
{
//printf("start counting responses\n\n"); //debug***
loopCount2 = 0;

while(fgets(option, sizeof (option), spResponses) != NULL) //read file
{
loopCount2++;
}

fclose(spResponses); //close file
}
 
Last edited:
First, you have to know the structure of your data file (responses??).
Open it with a text editor. If you see different lines, you can count the number of lines. If you see one single long line, then you have to find the delimiters between the individual responses, and programme accordingly.
If the data file is binary, then it is a completely different story.
If it is a text file, try posting a couple of lines to give an idea of what you are dealing with, unless of course if the data is sensitive.

If necessary, you can count the number of lines in a (text) file along the lines of the following (sorry, I did not compile to test).

long numberOfLines(char *filename)
{
FILE *f=fopen(filename,"r");
char buf[200];//assuming each line does not exceed 200 characters
long count=0;
if(f==NULL)return -1; // unable to open file
while(fgets(f,199,buf)!=NULL)count++;
fclose(f);
return count;
}
 
the file I am reading is a .txt file, and its in the format:
Fine.
OK.
Never been better!
etc.

None of the lines are over 70 characters currently.
 
Code:
 while(fgets(option, sizeof (option), spResponses) != NULL) //read file
{
is "option" a character array?
What is the value of (sizeof(option))?
 
  • #10
yes, char option[70], so sizeof(option) should be 70
 
Last edited:
  • #11
aha, I fixed it. Thanks for the help!
 
  • #12
for (i = 0; i < loopCount1; i++)
{
for (j = 0; j < loopCount2; j++)
{

if( ( i % 2 ) == 0)
{
responseLoop[j] = 200;
}
else
{
responseLoop[j] = 0;
}

}
 
Back
Top