Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A for loop to set a 2D array in C

  1. Dec 24, 2008 #1
    Is there anything wrong with this code? Im 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 (Text):
         for (i = 0; i < loopCount1; i++)
         {
             for (j = 0; j < loopCount2; j++)
             {
                 responseLoop[i][j] = 0;
                 j++;
                 responseLoop[i][j] = 200;
             }
         }
     
  2. jcsd
  3. Dec 25, 2008 #2
    it should be some thing like this

    Code (Text):

         int nextIsZero=0;

         for (i = 0; i < loopCount1; i++)
         {
             for (j = 0; j < loopCount2; j++)
             {
                 responseLoop[i][j] = (nextIsZero ? 0 : 200);
                 nextIsZero = ~nextIsZero;
             }
         }
     
     
  4. Dec 25, 2008 #3
    sorry for being a noob, but can you explain the code please? ive never seen the format (nextIsZero ? 0 : 200) or seen the tilde (~) used
     
  5. Dec 25, 2008 #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".
     
  6. Dec 25, 2008 #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 (Text):

              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.
     
  7. Dec 25, 2008 #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 realised that it wont 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: Dec 25, 2008
  8. Dec 25, 2008 #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;
    }
     
  9. Dec 25, 2008 #8
    the file im 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.
     
  10. Dec 25, 2008 #9
    Code (Text):
     while(fgets(option, sizeof (option), spResponses) != NULL) //read file
    {
    is "option" a character array?
    What is the value of (sizeof(option))?
     
  11. Dec 25, 2008 #10
    yes, char option[70], so sizeof(option) should be 70
     
    Last edited: Dec 25, 2008
  12. Dec 25, 2008 #11
    aha, I fixed it. Thanks for the help!
     
  13. Jan 6, 2009 #12

    harborsparrow

    User Avatar
    Gold Member

    for (i = 0; i < loopCount1; i++)
    {
    for (j = 0; j < loopCount2; j++)
    {

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

    }
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: A for loop to set a 2D array in C
Loading...