Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics (Retired) Fundamentals of C Arrays

Colby Newton
Colby Newton
522 Points

Arrays

Challenge task 3 of 4 Assign the number 1.41421 to index 1 of math_constants. Bummer! As a reminder, arrays are 'zero-based'. Assigning a number to an array would look something like: name_of_array[0] = 1; PreviewRecheck work arrays.c

1 float math_constants[1]; 2 math_constants[0] = 2.71828; 3 math_constants[1] = 1.41421;

5 Answers

Andrew Shook
Andrew Shook
31,709 Points

The problem is actually on your first line of code. When declaring the size of an array, the number you put in the square brackets is not zero indexed. You need to put the actually size in. Since you put 1 your array can't hold two number. This is what you need:

float math_constants[2];
math_constants[0] = 2.71828;
math_constants[1] = 1.41421;

This is a bug, Douglass Turner , I just tried this challenge and it will pass if you enter:

float math_constants[1];
Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

Thanks for bringing this to our attention. I have fixed the challenge so when you define an array with anything other than 2 then it won't go past the first task.

Troy Fine
Troy Fine
7,592 Points

@Colby Newton Don't give up. I did the challenge myself. Should look like below.

float math_constants[1];

math_constants[0] = 2.71828;

math_constants[1] = 1.4.1421;

The initial declaration of "float math_constants[1];" works because the array starts with index 0 and ends with index 1, leaving space for two values.

It looks like you did it right Colby but the challenge is not accepting your answer or mine, but I'll tell you that you did set the value right.

To set multiple values in a C array you can leave the length bracket empty and it will initialize with what values you leave in curly braces with values separated by commas to represent the indexes. Example below.

float someArray;

someArray [ ] = { 2.56, 3.51, 2.98};

The someArray was declared, and initialized with three indexes, with index 0 set to 2.56, index 1 set to 3.51, and index 2 set to 2.98. I hope this helps you understand arrays a little more.

As far as the code challenge going bonkers we'll need to notify a treehouse teacher or web administrator.

Andrew Shook
Andrew Shook
31,709 Points

No, when you declare an array in C the number you put into the brackets is not zero index. In fact it must be an integer larger than zero. Read here, here and here

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

When declaring an array it has nothing to do with the index. For example, you want to buy 4 apples, that's the quantity of apples you want to purchase. It has nothing to do with the order in which you are going to eat them. Similarly, when declaring an array, you want to specify the actual amount of elements the array will hold.

Troy Fine
Troy Fine
7,592 Points

I'm sorry Andrew, I probably explained that poorly, what I meant is that the array (size) in this challenge, starts with index zero and ends with index one. This leaves two indexes in the array to which we can place two separate values.

I hope that clarifies things. I've edited my first post to reflect this.

Colby Newton
Colby Newton
522 Points

Guys I understood the idea of arrays, but missed the idea of defining the actual number(size) of array. I get how it works at least. I successfully completed the task. On to the next one. How is everyone's progress coming? What is a realistic goal to have to create/ program an application?

Andrew Shook
Andrew Shook
31,709 Points

Depends on what you want to do. Coding a full blown application, whether web, phone, tablet or computer, is a lot like writing a novel. While it is a creative endeavor, it requires a lot of technical skills that need time to be cultivated. You need to know how to plan the project, break it into pieces, code it, and then refine it until you have a finished product. Just like a novel, programs require rewrites to fix bugs and optimize performance.

Realistically, if you have an idea for an app and you want to make it yourself, then focus on the ios development track here and finish all of the courses. Once you've done that, you should have enough skills to make a working prototype of your app. Also, and I say this to everyone want to learn how to program, go out and buy a book on the C programming language. I really can't stress that enough, because C is the basis for almost every modern programming language ( PHP, Ruby, Java, Python, OBJ-C, C#, C++, JavaScript) so you will learn a lot about basic programming for every language. You will also learn a lot about memory management, how things are stored in memory, and how programs go from words to 1s and 0s. Granted, a lot of the languages I mentioned above were developed to get away from the difficulties associated with C, but by removing those difficulties they have also remove some important aspects of learning to program.

Colby Newton
Colby Newton
522 Points

Andrew would you make any type of recommendation on a C beginners book? Basically, I am reaching, for the stars with my app idea. I think (probably like most) that it will be a success. Although, I am a realist and it at least will teach me coding and app dev. Do you think Andrew that it would be best to learn C before divulging onto the track any further? Would it be more beneficial to know the meat and potatoes? I want my app to run like a champ. It might look graphically poor, but my idea of it is that people can actually use it.

Troy Fine
Troy Fine
7,592 Points

I totally agree with Andrew on this. C is a must foundation for programming. C++ is a good soft introduction to object oriented programming after you learn C but C++ still operates on different rules concerning how classes and objects talk to each other in comparison to Objective-C. But it might help you understand the inner workings of an object a little bit more. Objective-C in it's self is easy to learn. Mastering the API's however is going to take a lot longer. Something I find my self pulling my hair out over at times.
As you can see by my profile pic I've made great progress in the pulling out of my hair.