Arrays

This is a discussion on Arrays within the C Programming forums, part of the General Programming Boards category; How would I write a nested for statement that initializes each element of the array t[2][5] to zero? Also how ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    18

    Arrays

    How would I write a nested for statement that initializes each element of the array t[2][5] to zero?

    Also how do I display the elements of the first row of array t[2][5]?

    Thank you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you think you'd do it? Give me a break! TRY IT ON YOUR OWN FIRST!

    Break it down in logical steps. for each row for each colum set this value to that value. It's not hard. Give it a try eh?

    Quzah. :very very very mad:

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    18
    is this right?:

    for (row = 0; row <=2; row++)
    t[row][5] = 0;
    for (column = 0; column <=5, column++)
    t[2][column] = 0;

    im still tryin to figure out the part about printing the elements of the first row.

    sorry to have ........ed ya off to hell like that.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not you exclusively. Every day, people show up and say "do my homework plz", without even trying it on their own. There is a FAQ at this site, there's also a link, right at the very very top, that says "READ THIS FIRST", and yet, noone ever reads it. That's what ........es me off.

    I don't mind helping people (actually I enjoy it, otherwise I wouldn't be here) who make an attempt at trying something.

    Ok, think of an array as a grid. (Or a line, if you've just got a single dimension array). As such, how would you count or use the elements in the array, one at a time? Well, you'd start at zero and work your way up.

    You need two loops here, if you're doing a single cell at a time.
    One increments the row, and one increments the column.

    Thus, you start at row0 and go up through row(N-1).
    The same goes for columns. colum0 through column(N-1).
    Where N is the max size of your array.

    Thus, if we have an array 6 blocks long, we start at zero, and to up through 5. This means we can use a loop like this:

    for( row = 0; row < 6; row++ )

    You can do the same for columns. Then you end up changing the value at that spot, which something like this:

    array[row][column] = 0;

    That help? I've given you enough to go on, but not the whole code.

    Quzah.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    18
    so would this work forl array t[2][5]?

    for (row=0, row < 2, row++)
    for (column=0, column < 5, column++)
    t[row][column]=0;

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, did it work? Slap that sucker in a block of code, and try it out. Yes, it should work, assuming you declared your variables first and what not.

    Quzah.

  7. #7
    free(me);
    Join Date
    Oct 2001
    Location
    Santo Domingo, DN, Dominican Republic
    Posts
    98

    ...

    I was just cruising around the board in search of something interesting since i have absolutely nothing to do right now. And i stumbled upon these Homework posts Quazh is so understandably upset about, just looking over them made me a little angry.

    How can someone ask "would this work?"? If you have a compiler you can answer that question yourself ALL too EASILY. If it doens't work THEN you post and ask "WHY doesn't this work?".

    If you ask for someone to help before you actually try to do something, it's just like asking them to do it for you. Programming is all about doing stuff on your own, if you never try to do anything you'll never get stuck, and more often than not it's when you get stuck that you actually learn the important lessons. Programming is about solving problems, solving problems is about using you brain.

    In closing: First use your brain, then IF/WHEN you get stuck ask for help, and all the people here wll be more than happy to help you. I know I will, and i think so will Quazh.

    adios,
    biterman.

    PS: It also helps to actually ponder HARD on the problem that has you stuck -until you're at your wits end-, if you still can't figure it out then when someone explains it to you it feels much better. Like a HUGE load of your back.
    Do you know how contemptous they are of you?

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    amen

    amen
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    18
    If you can't say anything nice, then just keep your damn mouth shut. Just because you may be somewhat of an expert, doesn't mean you put down everyone else. If you don't like what they posted or what they asked for, then just move on. *Deleted*

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Now now. There are some valid points here, you do at least appear to be breaking board rules.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    Take it easy guys, I think that Quzah said it all.

    And yes,

    AMEN

  12. #12
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    so would this work forl array t[2][5]?

    for (row=0, row < 2, row++)
    for (column=0, column < 5, column++)
    t[row][column]=0;
    No it wont. Look closely at your for statements.Then look closely at Quzah's and you will see the difference.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  13. #13
    Unregistered
    Guest

    Smile

    yeah i know i put , instead of ; i always do that stupid mistake.

  14. #14
    free(me);
    Join Date
    Oct 2001
    Location
    Santo Domingo, DN, Dominican Republic
    Posts
    98

    ...

    If you can't say anything nice, then just keep your damn mouth shut. Just because you may be somewhat of an expert, doesn't mean you put down everyone else. If you don't like what they posted or what they asked for, then just move on. *Deleted*
    Was that directed at me? I read my reply a couple times again looking for the part that wasn't nice. I failed to find it.

    adios,
    biterman.
    Do you know how contemptous they are of you?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21