A for loop to set a 2D array in C

Click For Summary

Discussion Overview

The discussion revolves around a C programming issue related to initializing a 2D array using nested for loops. Participants explore various approaches to achieve a specific pattern of values (0 and 200) in the array, while also addressing potential issues with array bounds and file reading for determining loop counts.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant shares their initial code for setting values in a 2D array but notes that the numbers are not being set correctly.
  • Another participant suggests an alternative approach using a conditional operator to alternate between 0 and 200, introducing the variable nextIsZero.
  • A participant requests clarification on the use of the conditional operator and the bitwise NOT operation.
  • One participant points out a potential issue with array bounds if loopCount2 is odd and proposes a modified loop structure to handle this case.
  • A participant expresses confusion about unexpected values in the array and reveals a mistake in how loopCount2 is being calculated based on file reading.
  • Another participant advises checking the structure of the data file to ensure proper line counting and suggests a function to count lines in a text file.
  • A participant confirms the format of the text file being read and provides an example of its content.
  • One participant inquires about the character array used for reading lines from the file and its size.
  • A participant confirms the size of the character array and expresses gratitude for the assistance received.
  • Another participant presents a new approach to initializing the 2D array based on the row index being even or odd.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to initialize the array, as multiple methods are proposed and discussed. There is also uncertainty regarding the handling of odd loop counts and the implications of file reading on loopCount2.

Contextual Notes

Some participants mention potential issues with array bounds and the need to understand the file structure for accurate line counting. There are also unresolved questions about the behavior of the code in specific scenarios.

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;
}

}
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K