Thread: Simple array problem

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32

    Simple array problem

    Hey all, back again with another post. This time its arrays!

    I've gotten basic arrays down and working and at the moment am trying to make something a little more complicated (for me at least).

    I have a data.txt file in the same folder as my array.c file which has a load of values set out in 4 columns

    Ie

    77, 66, 80, 81
    40, 5, 35, -1
    51, 58, 62, 34
    0, -1, 21, 18
    61, 69, 58, 49
    81, 82, 90, 76
    44, 51, 60, -1
    64, 63, 60, 66
    -1, 38, 41, 50
    69, 80, 72, 75


    I have to write some code such that my program reads these values and stores them in an array using the unix command ./array < data.txt. Its meant to do some calculations based on the values in data.txt, however at the moment I am doing a simple printf to check if the array storage works.

    At the moment I have the following

    Code:
    #include <stdio.h>
    
    int main()
    {
            int mark[40], i;                        /*setting up array and i as ints*/    
    
            for(i = 0; i < 40; i++)                 /*for i=0, i<40 increase i by 1*/
    
            {
                    scanf("%d",&mark[i]);           /*scan for value to store in array*/
            }
             
            printf("%d\n", mark[0]);                /*testing arrays via printing*/
            printf("%d\n", mark[4]);
            printf("%d\n", mark[8]);
    }
    Running this prints the correct value for mark[0] and the wrong values for mark[4] and mark[8]. This suggests to me my for statement is messed up somehow, but I'm not seeing how. If anyone could show me whats going wrong it would be much apprecaited.

    Advance thanks,
    Native

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    So what is the wrong 'value'?
    Is it garbage or .. what?
    You could check whether scanf() succeed or not. RTM.
    Oops just realize that your input file has comma (,) as delimiter. scanf() does not work that way. Again RTM,
    Last edited by Bayint Naung; 11-24-2010 at 07:28 AM. Reason: wrong usage

  3. #3
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    What does RTM mean? The txt file was given to use with commas in by a professor! blergh

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    RTFM - Wikipedia, the free encyclopedia
    man page scanf section 3
    You could use fgets() to read one line and parse the line using say, strtol()/strtok,etc..

  5. #5
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    I have to use scanf()...
    Also posting read the manual is not of great help - I have reached this point based on lecture notes, book and online resources and now am stuck hence my posting on here >_<
    Last edited by Native; 11-24-2010 at 08:20 AM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Running this prints the correct value for mark[0] and the wrong values for mark[4] and mark[8].
    what exactly are the "wrong" values its printing out. In my experience, when testing reading in input it is helpful to print all the input out, not just a few, so you can watch exactly where everything is going, and you can watch for odd patterns. For example, possibly changing the loop to:

    Code:
     for(i = 0; i < 40; i++)                 /*for i=0, i<40 increase i by 1*/
    
            {
                    scanf("%d",&mark[i]);           /*scan for value to store in array*/
                    printf(mark[%d]: %d\n", i, mark[i]);
            }
    perhaps can help you figure it out.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is very close to what you want, and works fine:
    Code:
    #include <stdio.h>
    
    int main() {
      int i; 
      char buff[80];
      int mark[40];
      printf("\n\n\n");
      i=0;
      while((fgets(buff, sizeof(buff), stdin))!=NULL) {
        if(buff[0] == '\n')  //handles last text line has newline
          break;
        printf("buff: %s", buff);
        sscanf(buff, "%d%*c %d%*c %d%*c %d%*c", &mark[i],&mark[i+1],&mark[i+2],&mark[i+3]);
        printf("%d, %d, %d, %d", mark[i],mark[i+1],mark[i+2],mark[i+3]);
        i+=4;
        printf("\n\n");
        
      }
      printf("\n\n\t\t\t     press enter when ready");
    
      (void) getchar(); 
      return 0;
    }
    the %*c format is just "scan, but don't store this char", format. Handles the commas, and newline, but may not be required to work. Imo, scanf() is picky, and being explicit handling the char's, gets the coding done with fewer mistakes.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    Thanks guys,

    Lero I have tried changing my loop based on what you have put but i keep recieve a compile error on line 15 character 15.
    I have
    Code:
    printf(mark[%d], "%d\n", i, mark[i]);
    and i keep getting an error which reads
    ex4b.c:15:15: error: expected expression before %\ token. I can't see why but my brain is slowly melting after like 2 days spent trying to work this thing.

    Adak your code work very nicely, however I need to then analyse the array to find the total number of coursework marks in the following ranges:
    >= 70
    between 60 and 69
    between 50 and 59
    between 40 and 49
    <= 39

    Would this work with what you have posted? I am not familiar with buff, fgets, stdin and sizeof (currently frantically reading up on all of these).
    - edit : have read up on these and understand them now but the question still stands will they work with the analysing?
    Last edited by Native; 11-24-2010 at 01:46 PM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Of course it will work with whatever analyzing you want to do!

    Remember that now, the data is in the array - that's the thing. Imo it would be much easier if you used a 2D array for mark[], (like mark[][]). That's an easy change to make, and makes each row easier to work with. Something like maybe:

    John 88 78 83 75
    Sally 82 76 91 77
    Ann 80 88 90 85
    Dave 91 90 78 88

    Now it's easy to see how to sum up the marks, and get an average for each student, or whatever you want to do. Each row representing the marks for one student.

    fgets() and sscanf() are common ways to take a row of text input, and then scanf() it with sscanf() into the variables we need. Keep that tool in your programmers toolbox!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with qsort and array via calloc
    By Zimbobo in forum C Programming
    Replies: 2
    Last Post: 11-23-2009, 12:25 AM
  2. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 07:27 AM
  3. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Replies: 6
    Last Post: 10-21-2003, 09:57 PM