A for loop to set a 2D array in C

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
11 replies · 5K views
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;
         }
     }
 
Physics 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))?
 
yes, char option[70], so sizeof(option) should be 70
 
Last edited:
aha, I fixed it. Thanks for the help!
 
for (i = 0; i < loopCount1; i++)
{
for (j = 0; j < loopCount2; j++)
{

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

}