I've taken a step back to learn a few more basics, as I'm having trouble with certain things. Right now I'm just messing with a simple array. I'm getting some strange output though..
Firstly to output the contents of an array you needn't manually output each element.
Secondly, it does print 7 for the addition. But you forgot to put a newline between addition and the last element that is printed, namely a[5], with the value of 6.
Hey yeah, that's definitely a lot easier. That's only good for printing an entire array or sections though right? no good for say 100 element array, and you only want to print say 20 elements in various locations?
That's only good for printing an entire array or sections though right?
Yes.
But it's not always printing that you want to do. Maybe you want to check if all elements are below a certain value... so for 100 elements you'll write the same if (a[] < value) 100 times?
Thanks for the link. I'm gonna go read it, lol.. First I have another question about the loop. It's probably covered there, but I'll shoot anyway.
does the loop increment once the first time it's executed? I was messing with the loop to learn it. With the below code it has to be set like this to display 3 elements ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<iostream>
#include<string>
int main()
{
usingnamespace std;
int a[] = {1,2,3};
for (int i=0; i <= 2; ++i){
cout << a[i] << endl;
}
return 0;
}
basically if you start the loop i at 0 you want to end the condition one below the last element, and if you start i at i you want to end it 1 above?
for (int i=0; i < 100; ++i)
{
cout << i << endl;
cout << "text" << endl;
}
// is the same as
int i=0;
while (i < 100)
{
cout << i << endl;
cout << "text" << endl;
++i;
}