Home > categories > Hardware > Brackets > C++ beginner. Why the square brackets after type?
Question:

C++ beginner. Why the square brackets after type?

here is a program taken from another answer that i'm trying to comprehend:pastebin(dot)com/fXK42W3iregarding the section: float percents[9];int nums[9] { 0 }; why are there square brackets and why is there a 9 in them? also why do you use the {} brackets when defining them as 0? does this have to do with indexing? also:will each line of text in the file be read 1 at a time automatically? where will it be storedthanks for any and all iput/advice

Answer:

float percents[9]; 'percents' is an array. The square brackets here tell us that it is an array that can hold 9 floats. --- for(int i 0; i 9; i ) percents[i] (float)nums[i]/count; the brackets here are for how we access the array 'percents'. As variable 'i' increments (0, 1, 2, 3 9) we access that member of the array (the 1st, the 2nd, etc) This line of code is *Storing* floats into the array 'percents. --- int nums[9] { 0 }; The curly braces here allow us to optionally assign values to the array 'nums' at the same time we define the array 'nums'. A more complete assignment and definition might look like so: int nums[9] { 0,1,2,3,4,5,6,7,8 }; --- The code reads one *integer* at a time from the file. How lines are read depends on how many ints per line are in the file.

Share to: