Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Simple array problem I am stuck on

this is the challenge

Declare an array of type 'char' called "letters" with a size of 2. Assign the variable "alpha" as the first element of the array "letters".

I am writing this

char letters[2]; letters [0] = alpha;

or

char letters[] = {alpha}

Am I missing code or doing something wrong

10 Answers

Hi Andrew! Here's the correct syntax for this question:

char alpha = 'a';
char letters[2];
letters[0] = alpha;

@Nick -- I like the succinct way in which your array was declared, but both ways work. For the purposes of this problem, I simply broke it down into steps, first initializing the array, and then later adding to the array. See here for further explanations: http://www.thegeekstuff.com/2011/12/c-arrays/

OMG SO MANY WRONG ANSWERS!

lol :) What do you propose are the right answers?

The great thing about Treehouse discussions vs. say, Stack Overflow, is that it's "ok" to be wrong -- we're all learning here! :)

LOL the answer i'm seeking is the right answer, and usually that answer is the right answer! well i've just spent a great deal of time just researching array... i'm still clueless! =P

Ah, I see. Here's a brief explanation of arrays -- hope this helps!

So arrays are just data structures (i.e. they hold data). They're great for storing data in a particular order, which enables you to pull a given object from the list at any given location. This location, or position in the array, is also known as an index.

To help you picture what's going on, say we have a box called box. This box contains 5 objects. Here's what our box array looks like upon defining it:

char box[5];

The char indicates that the type of items inside the box are all one-letter characters. Arrays should always contain elements of the same type.

Now say we want to initialize the array, to actually stuff the box with our lettered objects. Here's what we'd do:

char box[] = {'a','b','c','d','e'};

Now we have a box with 5 letters. Just because the box has 5 elements, though, that does not mean that the highest index is 5. When we want to pull an item from the box, or reference an item, we have to start counting from 0. So, our letter a is at index 0, our letter b is at index 1, and our letter e is at index 4.

So, the 5th letter in our box can also be placed in our box like so:

box[4]={'e'};

Let me know if you have any more questions -- I'm happy to help :) Here's another tutorial I found which I thought had a good explanation of accessing arrays as well, which is a lot more complicated in C than it is in other languages: http://www.tutorialspoint.com/cprogramming/c_arrays.htm

correction char letters[2]; letters[0] = alpha;

Thanks Novall

Nick Blomquist
Nick Blomquist
3,659 Points

that is wrong here is the answer

char letters[2] = {alpha};

Frankie Lamar
Frankie Lamar
11,120 Points

@Nick Blomquist - How can your answer be correct. The array has a zero index so char letters[2] would be pointing to the third item in the array. The instructions say to add the var alpha to the first location in the array. That would be letters[0].

Hey Frankie! Nick's answer is actually ok, because when you're defining an array, the value you insert in the brackets [ ] is the number of elements the array stores (which in this case, starts at 1). Otherwise, you're right -- letters[2] would indicate the third item in the array when accessing that item. See this link for further clarification: http://www.thegeekstuff.com/2011/12/c-arrays/

Ah, I see. Here's a brief explanation of arrays -- hope this helps!

So arrays are just data structures (i.e. they hold data). They're great for storing data in a particular order, which enables you to pull a given object from the list at any given location. This location, or position in the array, is also known as the index.

To help you picture what's going on, say we have a box called box. This box contains 5 objects. Here's what our box array looks like upon defining it:

char box[5]; 

The char indicates that the type of items inside the box are all one-letter characters. Arrays should always contain elements of the same type.

Now say we want to initialize the array, to actually stuff the box with our lettered objects. Here's what we'd do:

char box[] = {'a','b','c','d','e'}; 

Now we have a box with 5 letters. Just because the box has 5 elements, though, that does not mean that the highest index is 5. When we want to pull an item from the box, or reference an item, we have to start counting from 0. So, our letter a is at index 0, our letter b is at index 1, and our letter e is at index 4.

So, the 5th letter in our box can also be placed in our box like so:

box[4]={'e'}; 

Let me know if you have any more questions -- I'm happy to help :) Here's another tutorial I found which I thought had a good explanation of accessing arrays as well, which is a lot more complicated in C than it is in other languages: http://www.tutorialspoint.com/cprogramming/c_arrays.htm

Thanks Navall you've been a great help!!! Soon i'll be an expert like you! =D

Any time! Glad I could help! :)

824 points my gawd!!! ur good.... since november 2013 wow

  1. Declare 2 separate variables of type char named "alpha" and "bravo". Assign the letter 'a' to the variable "alpha" and the letter 'b' to "bravo".

char alpha = 'a'; char bravo = 'b';

  1. Declare an array of type 'char' called "letters" with a size of 2. Assign the variable "alpha" as the first element of the array "letters".

char alpha = 'a'; char bravo = 'b'; char letters[2] = {alpha};

  1. Assign the variable "bravo" as the second element of the array "letters".

char alpha = 'a'; char bravo = 'b'; char letters [2] = {alpha, bravo};

  1. Print the results. Your output should look like "letters a b".

char alpha = 'a'; char bravo = 'b'; char letters [2] = {alpha, bravo}; printf("letters %c %c \n", letters [0], letters [1]);

Nick Blomquist
Nick Blomquist
3,659 Points

here is the full version from step 1 and 2

char alpha = 'a'; char bravo = 'b';

char letters[2] = {alpha};