C++ without fear exercise 5.1.2 help!

May 12, 2013 at 11:08pm
There is an exercise in this book that asks to make an array of 5 integers, then print all of them and then the sum of all the numbers. I thought i knew how to do this but I don't think I fully understand arrays yet to do this. any help is appreciated.

Here's my code so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{
    int jack[5] = {100,300,900,400,500};
    int x = 0;

    while(x < 5){
        cout << jack[x] + jack[x++] << endl;

    }

}

May 12, 2013 at 11:14pm
Your gonna want to make a temp variable such as total and then add each consecutive array index to it like
1
2
int total(0);
total = total + jack[i];

it might also be easier to use a for loop if you have covered those yet.
Last edited on May 12, 2013 at 11:14pm
May 12, 2013 at 11:16pm
This program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main()
{
    int jack[5] = {100,300,900,400,500};
    int x = 0;

    while(x < 5){
        cout << jack[x] + jack[x++] << endl;

    }
}


has undfined behavior. You may not use postfix increment operator of a variable with the variable itself in the same expression because the order of evaluation of subexpressions is undefined.

Your tasks are being done simply the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main()
{
    int jack[5] = {100,300,900,400,500};

    for ( int x : jack ) std:;cout << x << ' ';
    std::cout << std::endl;

    int sum = 0;
    for ( int x : jack ) sum += x;

    std::cout << "The sum is equal to " << sum << std::endl;
 }


If your compiler does not support the range-based for statement then try to rewrite the loops yourself using the ordinary for statement.
Last edited on May 12, 2013 at 11:18pm
May 12, 2013 at 11:35pm
Jidder: im going to have to make a function called total? also i would use a for loop. but im using while to make it as basic as possible.

Vlad: im sorry, im a noob, i basically have no flippin idea what you just said. i apologize
Last edited on May 12, 2013 at 11:38pm
May 12, 2013 at 11:38pm
I not only said but also showed you valid code.:) So investigate it.
May 12, 2013 at 11:40pm
ya also you dont have to use std:: because that's what using namespace std; does. also the way you put the for loops was a little confusing. but i think i got the jist of your program. thank you vlad
Last edited on May 12, 2013 at 11:43pm
May 12, 2013 at 11:44pm
I simply did not take into account your statement

using namespace std;

because I copied and pasted your original code as is.

But in any way it is not an error even if this statement is included. I advice simply to remove it.:)
Last edited on May 12, 2013 at 11:44pm
May 12, 2013 at 11:46pm
yeah my compiler doesnt support range-based. and i have no idea what that even is so how would I re-write that?
May 12, 2013 at 11:48pm
As I said use the ordinary for statement instead of the range-based for statement. You should understand what the for statemnet in my code does and substitute it for your for statement.
Last edited on May 12, 2013 at 11:48pm
May 12, 2013 at 11:50pm
but i dont. all i know is you declared the variable x as an integer and assigned it no value then use ':' and then the name of my array... is it basically the same thing as jack[x]?
May 12, 2013 at 11:50pm
whatever man im way more confused than I was earlier. thanks for the help.
May 12, 2013 at 11:58pm
For example the following rahge-base for statement

1
2
    for ( int x : jack ) std:;cout << x << ' ';
    std::cout << std::endl;


can be rewritten as

1
2
    for ( int i = 0; i < 5; i++ ) std:;cout << jack[i] << ' ';
    std::cout << std::endl;
May 12, 2013 at 11:59pm
okay so I got it. thanks vlad sorry for being difficult.

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 jack[5] = {100,300,900,400,500};
    int sum = 0;

    for (int x = 0; x < 5; x++){
        cout << jack[x] << endl;
        sum += jack[x];
    }

cout << sum << endl;


 }


May 13, 2013 at 12:06am
Only it is a bad style of programming to use such identifiers as x as indexes of arrays in such simple loops. It is better to use identifiers i, j, k, l, m, n. This tradition came from FORTRAN.
Last edited on May 13, 2013 at 12:07am
Topic archived. No new replies allowed.