Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (anonymous):

what will the following code display int numbers[] = {99, 87, 66, 55, 101}; for (int i = 1; i < 4; i++) cout << numbers[i] << ‘ ’;

OpenStudy (anonymous):

I dont get why they use int i =1

OpenStudy (nick67):

87 66 55

OpenStudy (anonymous):

They use int i = 1 because they are initializing the first value to be 1. You have two options. You can actually try running the code, or you can walk through the code. I'd suggest the latter: If you replace i with 1 in the bottom line, what happens? Then what is the next value of i? And so on and so forth.

OpenStudy (anonymous):

@nick67: Do NOT just give answers to people. That goes against the code of conduct of the site.

OpenStudy (nick67):

when the problem are answers instead of questions something is not working

OpenStudy (anonymous):

This is clearly a problem right off a homework/test. It's not about something not working.

OpenStudy (anonymous):

i still just want to understand, im not just going to take the answer without asking how

OpenStudy (anonymous):

if int i =1 then how does that correspond to the numbers above. Like, how is the int i = 1 communicating with those numbers

OpenStudy (anonymous):

?

OpenStudy (anonymous):

did you get my message nick?

OpenStudy (anonymous):

An array of values is numbered, starting at 0. For example, let's say I had an array called animals[] = {'bird','dog','cat','platypus','monkey','elephant'} You might say that 'cat' is the third thing in the list, but not in computer science! We start counting with 0, so 'bird' is #0, 'dog' is #1, and 'cat' is #2. So let's say I want to look into the list and get a specific element. If I type animals[4], the program goes into the list and counts up...0,1,2,3,4...and then pulls out the element it is now pointing to. So if I say animal = animals[4], then the variable animal will hold the value 'monkey'. Does that make sense?

OpenStudy (anonymous):

One way to always follow up on this sort of thing, since you're obviously in front of a computer, is to try and run it. Then try and play with it. What happens when you change the 1 to a 3, or 2, or 0? It's learning to play with it that will most help you understand it.

OpenStudy (anonymous):

zachkt- you asked what the i= 1 is. This basically SETS UP (initializes) the variable i by declaring it to be an integer, and giving the the starting value of 1. Because it's in a for loop in this case, it starts as 1, but then next time around it's a 2, then 3, and then it stops. So your question was "if int i =1 then how does that correspond to the numbers above. Like, how is the int i = 1 communicating with those numbers" the variable i is used as an INDEX of what's in the array {99, 87, 66, 55, 101} Think of the index as a reference to each location in the array. So the positions are like {0,1,2,3,4} - many languages use a zero-based index, so the first one is 0, the second is 1, and so on. so in this case numbers[1] is 87 numbers[0] doesn't exist numbers[5] doesn't exist numbers[4] is the FIFTH value, so it's 101. Instead of actually using a number in the brackets, we use the variable i, which represents the number that's changing (from 1 to 3) thru the loop. The result of this exercise will be to display the indexes for 1, 2, 3 out of the possible 0,1,2,3,4 so it'll be the 2nd, 3rd and 4th numbers - 87, 66, 55.

OpenStudy (anonymous):

numbers is an array which contains 5 elements {99, 87, 66, 55, 101} i is used as a counter variable and initialized with value 1 numbers[0]= 99; numbers[1]=87; numbers[2]=66; numbers[3]=55; numbers[4]=101; Now the for loop runs for i=1 till i=3 and stops at i=4 as it violates i<4 condition so the above program prints to console 87 66 55

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!