Thread: Problems with Arrays

  1. #1
    Hopeless
    Guest

    Post Problems with Arrays

    I am having real trouble getting a program started. I am sure my brain is just exausted (getting close to end of semester), but I can't figure out how to get this problem started. I don't expect anyone to give me all the code, just a push in the right direction.

    The program must take the input from a data file that will give the values of an Array with 5 rows and 4 columns. The program must then add up the values of each column and print the total of each.

    There are a few other things it must do, but if I could just figure out this first part I am sure I could get the rest. I thank you in advance for any input you can give me.

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Which parts exactly are you having problems with? The reading in part, the array declaration part, the column adding part...?

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, a selection of things you may need:

    Open/Close files:
    fopen()
    fclose()

    Reading data:
    fgets()
    fscanf()

    Conversion:
    strtol()
    atoi()

    Something to output with:
    printf()

    Something to add up with:
    +

    An array:
    myarray[ROWS][COLUMNS]

    And a loop:
    for

    There are others too, you must choose which is best for you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Hopeless
    Guest
    The main thing I was having trouble with is how I read in the values of an array. All the examples we have in our book show the values being declared. Also, once I do have the values read in, I am not sure how I would go about adding all the values from a particular column. I know this is probably really basic, but the only examples that I find in our text book are one dimensional arrays.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Hopeless
    The main thing I was having trouble with is how I read in the values of an array. All the examples we have in our book show the values being declared. Also, once I do have the values read in, I am not sure how I would go about adding all the values from a particular column. I know this is probably really basic, but the only examples that I find in our text book are one dimensional arrays.
    To read the data, lookup the functions I gave you.

    To add the columns, you could try out nested loop for loops (one for loop inside another). Alternatively, if you want to bodge it, you can hard code the array addition as you know it's a fixed size.... but that's not really the way to do it
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Hopeless
    Guest
    Ok, I see what you are saying about adding up the columns. I am still having the problem reading the value into each element of the array. I understand I will be using a scanf statement, but do I have to do a scanf of 20 integers or is there a better way to do it? It doesn't seem like a scanf that long is the best way, but it seems like it would work. If you could let me know if there is a better way to do it or if I should just stick to my rookie techniques I would really appreciate it.

  7. #7
    Hopeless
    Guest
    I just figured out how to get it scanned in with the for loop and scanf. Thanks a lot for all your help.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    4
    Hopeless, you go to UCF? I have an assignment that's extraordinarily similar to yours.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with arrays
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 03-31-2009, 07:21 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. My Arrays!!!, My Arrays!!! Help!?
    By llcool_d2006 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 12:07 PM
  4. Problems with Strings and Arrays.
    By SlyMaelstrom in forum C++ Programming
    Replies: 13
    Last Post: 04-15-2005, 02:13 PM
  5. Help!!! Problems with arrays.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-08-2002, 08:21 PM