Thread: Simple Alphabet/Array Problem

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    76

    Simple Alphabet/Array Problem

    That I can't get. My instructor went over this last night and I'm trying to study up but apparently didn't save the program. Here is my code:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int alphabet[26] = {'A'};
    	int i = 0;
    	
    	
    	for(i = 0;i < 25; i++)
    	{
    			printf ("%d\t%c\n", alphabet[i], alphabet[i]);
    			alphabet[i]++;
    			      
    	}
    		
    	
    	return 0;
    }
    The elements within the array are not incrementing. I get 25 outputs, the first being A and 65 but then all zero's. What am I missing that is needed to increment the elements within the array?

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I'm sure the elements are being incremented... but you printed out the original value so you won't see the updated one. I think they'll all be 'B's.
    Oh and your loop should probably go for (i = 0; i < 26; i++)

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    76
    Quote Originally Posted by nonoob View Post
    I'm sure the elements are being incremented... but you printed out the original value so you won't see the updated one. I think they'll all be 'B's.
    Oh and your loop should probably go for (i = 0; i < 26; i++)
    Thanks, no they are not B's but one 65 A then all garbage.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    76
    Got it:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int alphabet[26];
    	int i = 0;
    	
    	alphabet[0] = 'A';
    	for(i = 0;i < 26; i++)
    	{
    		alphabet[i] = alphabet[0];
    		printf ("%d\t%c\n", alphabet[i], alphabet[i]);
    		alphabet[0]++;
    	}
    	return 0;
    }
    }
    Last edited by dolfaniss; 05-06-2011 at 11:40 AM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dolfaniss View Post
    Thanks, no they are not B's but one 65 A then all garbage.
    Because you're only intializing the first element of the array.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Hint I learned early on... should help you when you are under pressure.

    Without getting into the dangers of "magic numbers", when you want to go through a loop MAX_NUM times (say 26 times like in your case) or you've declared a variable with MAX_NUM elements (say 26) and you want to use them all, your for loop should be from 0 to <MAX_NUM.

    Like this:

    Code:
    int foo[MAX_NUM];
    for(i = 0;i < MAX_NUM; i++)
    Note, it's not

    Code:
    for(i = 0;i < MAX_NUM-1; i++)
    nor is it

    Code:
    for(i = 0;i <= MAX_NUM; i++)

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Because you're only intializing the first element of the array.
    Technically he is initializing all elements of the array. Default behavior is to auto populate the uninitialized elements to zero, so long as you have actually initialized at least one of them, which is why we can safely do this:
    Code:
    int x[5] = {0};
    To set everything to zero.

    True, it's not what he expects it to do, but they are initialized.


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

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    True, it's not what he expects it to do, but they are initialized.
    Quzah.
    Picky picky picky... Yes, the first character is initialized to his 'A'... the rest will be set to 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple array problem
    By Native in forum C Programming
    Replies: 8
    Last Post: 11-24-2010, 03:59 PM
  2. alphabet order problem(Datafile)
    By korena in forum C Programming
    Replies: 2
    Last Post: 08-31-2008, 08:09 AM
  3. Simple array problem
    By nevrax in forum C Programming
    Replies: 4
    Last Post: 04-16-2007, 06:10 PM
  4. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  5. Simple 2D array problem...In a Hurry!
    By 67stangman in forum C++ Programming
    Replies: 8
    Last Post: 04-18-2002, 01:22 PM