Thread: Array counting...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    7

    Array counting...

    i´m a sort of a newbie..

    i´ve got a following array
    Code:
    int months[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    and a -for-..
    Code:
    	for(i=0;i<month;i++)
    
    		{
    			days=days+months[(i-1)];
    
    		}
    where the days ans months have been asked from the user.
    For example, if the given months is 4, how do i get the -for-
    to count days from the earlier months only (4-1)?
    I think this counted the whole array together...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Lose the "(i-1)". Just use i. Walk through it in your head:
    Code:
    for(i = 0
    ...
    months[(i-1)]
    0 - 1 is not a valid array index.

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

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    7
    yep, this worked....had to write the -for- like this

    Code:
             
    
    for(i=0;(i<month-1);i++)
    
         {
             days=days+months[i];
    
        }

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    always count from 0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM