Thread: Arrays

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    19

    Arrays

    Code:
    #include <stdio.h>
    #define NO_OF_MONTHS 12
    main()
    {
        int days[NO_OF_MONTHS] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int month;
            
        printf("Please enter a month: ");
        
        do
        {
            scanf("%d", &month);
        }
        while (month < 1 || month > 12);
        
        printf("\nThe number of days in month %d is %d\n", month, days [month-1]);
    }
    Would anyone be able explain the line in bold to me please? What it looks like to me is that the second printf will be shown if the number entered is lower than 1 or higher than 12. I'm obviously wrong though.

    Thanks in advance.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You have a do while loop - the line in bold is saying 'while the number entered for month is less than 1 OR greater than 12, scanf() a number' Basically, because it is a do while loop, scanf() will happen at least once, prompting the user for a number. Then. if the number is greater than one, or less than 12, the program will carry on with the next printf() statement. Otherwise, if the number entered is less than 1 or greater than 12, scanf() will run again, and the user will be expected to enter another number within the range.

    ~/

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    19
    Thanks, thats very clear but I entered the number 13 and it skipped to the next line, I then entered a number in the range of 1-12 and it skipped a line again (and keeps doing so until i exit the cmd prompt).

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    19
    and just to add to my last post. I took the do-while out for experimentation and its obviously ok when entering a number between 1-12 but when i enter a number outside this range it returns a random long number. What makes it decide which number to return when its outside of the range?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The code works just like it should. It's waiting for you to enter the correct number. If you actually want it to prompt for the number, move the first printf line inside the loop:
    Code:
    do
    {
        printf("Please enter a month: ");
        scanf("%d", &month );
    ...
    Oh, and this is C, so main does not automaticly return an integer for you, so add a return statement.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    19
    Ah, its working fine now. Thanks guys.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by mcgeady
    and just to add to my last post. I took the do-while out for experimentation and its obviously ok when entering a number between 1-12 but when i enter a number outside this range it returns a random long number. What makes it decide which number to return when its outside of the range?
    It goes exactly where you tell it to in order to get the number regardless of whether or not the location it is even valid. Consider a system where ints are 32 bits (4 bytes), your array could look like:

    Code:
    var      address
    -------- -----------
    days[0]  0x000000A0
    days[1]  0x000000A4
    days[2]  0x000000A8
    ...
    ...
    ...
    days[10] 0x000000C8
    days[11] 0x000000CC
    Now, the only valid indexes for the array are obviously 0-11 and represented in the range of address' given above. If you try to output the contents of days[12] for example, the program will happily go and try to get whatever 4 bytes are stored at address 0x000000D0 and convert that into an int and then display it. Who knows what that might result in depending on what happens to be stored at that location? There could be a bunch of chars or a double or a pointer to something at that spot. Likewise, if you were to try and output the contents of days[-1] it would go and get the 4 bytes at memory address 0x0000000C and convert that to an int and display it.

    It is up to you the programmer to ensure such things do not happen.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Code:
    #include <stdio.h>
    #define NO_OF_MONTHS 12
    int main() 
    {
        int days[NO_OF_MONTHS] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int month;
    
        printf("Please enter a month: ");
    
       do
       {
          scanf("%d", &month);
       } while (month < 1 || month > 12);
       /*
       If the month is higher than the max OR the month is lower than the minmum, keep asking the user for another month that WORKS
       */
    
        printf("\nThe number of days in month %d is %d\n", month, days [month-1]);
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM