A for loop to set a 2D array in C

In summary, the code is trying to set each set of values for responseLoop[i] to read like this: 0,200,0,200,0,200,0,200... ect. for some reason, the numbers are not being set correctly. To fix the problem, you can try: If loopCount2 is odd, you may have a problem of array out-of-bound. To solve that problem, you can try:If the data file is binary, then it is a completely different story.
  • #1
VirObitus
19
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
  • #2
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;
         }
     }
 
  • #3
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
 
  • #4
The ~ performs the bitwise NOT operation on the integer.
(nextIsZero ? 0 : 200); translates to:

"If nextIsZero is true, then return 0, otherwise return 200".
 
  • #5
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.
 
  • #6
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:
  • #7
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;
}
 
  • #8
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.
 
  • #9
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;
}

}
 

What is a for loop used for in setting a 2D array in C?

A for loop is commonly used in programming to iterate through a set of instructions for a specified number of times. In the context of setting a 2D array in C, a for loop can be used to assign values to each element in the array.

How does a for loop set a 2D array in C?

A for loop can use nested loops to iterate through each row and column of the 2D array. Within the loop, the elements of the array can be assigned values using index notation.

What are the advantages of using a for loop to set a 2D array in C?

A for loop allows for efficient and concise code by automating the process of setting values in a 2D array. It also allows for flexibility in the size and dimensions of the array.

What are some common mistakes when using a for loop to set a 2D array in C?

Some common mistakes include not properly initializing the array, using incorrect index values, and not properly terminating the loop. It is important to carefully plan and test the loop to avoid these errors.

Can a for loop be used to set a 2D array with non-numeric values in C?

Yes, a for loop can be used to set a 2D array with any data type in C. However, it is important to ensure that the correct data type is used for each element in the array to avoid errors.

Similar threads

  • Programming and Computer Science
Replies
4
Views
609
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
943
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
5
Views
991
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top