2D array of names

This is a discussion on 2D array of names within the C Programming forums, part of the General Programming Boards category; Why is it that I do not get the date printed ? Code: int main() { ... char month[][MONTHS] = ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    29

    2D array of names

    Why is it that I do not get the date printed ?
    Code:
    int main()
    {
    ...
    
    char month[][MONTHS] = {"Jan", "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec"};
    
    ...
    
    printf("The lowest rainfall of %fmm falls in %s,\n" , lowest , month);
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You've got the array declaration backwords. Should be:

    Code:
    char months[MONTHS][]
    Next, you need to index into the array to specify which string to print, eg: months[which].

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    if you imagined accessing values stored in your your array in terms of x & y co-ordinates or across & down then it is the opposite of our usual usage, you would say 'down and across'>>

    Code:
    int main()
    {
    
    int myarray[4][4];
    
    int down = 2;
    int across = 2;
    
                         myarray[down][across] = 10;
                         printf("At  2 down and 2 across my array holds > %d\n", myarray[down][across]);
    
    return (0);
    }
    Last edited by rogster001; 08-27-2009 at 05:11 AM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    If you're going to post code, please don't use bad practices: SourceForge.net: Implicit main - cpwiki
    Also indent your code properly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    remembering the array starts with element 0 of course! so its actually "at three down and 3 across" if your user is counting from 1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matching of names in 2d array of strings...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-25-2009, 09:59 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  5. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM

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