simple array question

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..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
using namespace std;

int main()
{
#include<iostream>
using namespace std;

int main()
{


    int a[] = {1,2,3,4,5,6};
    cout << a[0];
    cout << endl << a[1];
    cout << endl << a[2];
    cout << endl << a[3];
    cout << endl << a[4];
    cout << endl << a[5];

    cout << a[0] + a[5];


    return 0;
}
}
Last edited on
1
2
3
4
5
6
7
int main()
{
#include<iostream>
using namespace std;

int main()
{

?
oops sorry, I re pasted new code in a an edited message, I'm gonna fix that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;

int main()
{
    int addition;

    int a[] = {1,2,3,4,5,6};
    cout << a[0];
    cout << endl << a[1];
    cout << endl << a[2];
    cout << endl << a[3];
    cout << endl << a[4];
    cout << endl << a[5];

    addition = a[0]+=a[5];
    cout << addition;


    return 0;
}


addition is coming out as 67, instead of 7. I guess what it's doing is stringing the two elements together, and outputting that?
That's nasty code.

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.

Use for loops to output elements. For example:
1
2
for (int i=0; i < 6; ++i)
    cout << a[i] << endl;
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?

You can put anything in a for loop.
How about you go read this: http://www.cplusplus.com/doc/tutorial/control/
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()
{
    using namespace 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?
No, the loop will start at zero (It will print out the first element in the array), then continue to 1.
Let me clear up the for for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
}
Topic archived. No new replies allowed.