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

Math constants array

How is this code not passing the challenge?

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

As Holger said the first definition should have been float math_constants[2]; Even though you only define 0 and 1 that doesn't mean that your definition should be [1]. :)

2 Answers

Holger Liesegang
Holger Liesegang
50,595 Points

Welcome to Treehouse, David :)

this would be

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

...as your float array has been to small to hold 2 numbers.

Kind Regards Holger

Do you know why the array indexes start at zero but the number of items in the array doesn't? Thanks for your answer!

Also the first task says make an array that can hold 2 numbers and this line of code passes:

float math_constants[1]; Surely that shouldn't work and is confusing to the next challenges?

Holger Liesegang
Holger Liesegang
50,595 Points

Mostly per definition but you might think of it like that: if you declare the C array you name the number of items and e.g. 8 items are 8 items and not 7 :) - but if you want to access an item via the index this index starts for the programming language C at 0 for the first item. Most programming languages have got zero-based array types - there are only a few exeptions like Smalltalk which provides one-based numbering for arrays.

Holger Liesegang
Holger Liesegang
50,595 Points

C - Arrays should deliver you a better understanding of the concepts of arrays in C.

...and the "float math_constants[1]" is kinda confusing, yes :)

Thanks for your quick and detailed response! I'm guessing that line shouldn't pass the first challenge then as float math_constants[1] can only hold one item and not two...

Thanks again.

The question is:

Assign the number 1.41421 to index 1 of math_constants

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