Arrays

Please explain to me on how to get the out for this code:
1
2
3
4
5
6
7
8
9
 int list [10], i, j;
for ( i = 9; i >= 0; --i )  
{
	list [i] = i * 100 – 5;
	if  ( i % 3  == 1 )  list [i] = list [i+1] * 200;
}
for ( j = 0; j < 10; ++j ) cout << list[j]+ " " ;
cout << endl;


2)Assume you have an array called “nums” that has 20 integers in it. Also, assume that it is
populated already. Write the code to display the values in “nums” with 4 numbers on each
line separated by a couple of blank spaces.
{ I.e. you will have 5 lines printed, 4 numbers on each line. }

Do NOT reference 4 values of “nums” in one “cout” statement.
For example, do NOT write something like this:
cout << nums[1] << nums[2] << nums[3] << nums[4] <<endl;.
Do not write something like the above using a variable or an expression instead of the
numbers in the subscripts.

Hint: you are using the "typical" loop and printing nums[i] in one cout.
Consider when do you print blank spaces VS when to go to the next line.

From what i understood, this is what i have:
1
2
3
int nums[20];
for(i=20;>=0,++i);
   ( list [20]=i


Not sure where to go from here...
Last edited on
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <iomanip>

int main()
{
    const char couple_of_spaces[] = "  " ;
    const char new_line = '\n' ;

    {
        const int SZ = 10 ;
        int list[SZ] ;

        for( int i = SZ-1 ; i >= 0 ; --i )
        {
            if( i%3 == 1 ) list [i] = list [i+1] * 200 ;
            else list[i] = i * 100 - 5 ;
        }

        // classical for loop
        for( int i = 0 ; i < SZ ; ++i ) std::cout << list[i] << couple_of_spaces ;
        std::cout << new_line ;

        // range based loop http://www.stroustrup.com/C++11FAQ.html#for
        for( int value : list ) std::cout << value << couple_of_spaces ;
        std::cout << new_line ;
    }

    std::cout << "-----------------------\n" ;

    {
        const int SZ = 20 ;
        const int nums[SZ] = { 13, 87, 99, 32, 56, 71, 44, 65, 24, 56, 10, 78, 51, 51, 52 /* zeroes */ } ;

        // classical for loop (4 values per line)
        for( int i = 0 ; i < SZ ; ++i )
        {
            std::cout << std::setw(2) << nums[i] ;

            if( i%4 == 3 ) std::cout << new_line ;
            else std::cout << couple_of_spaces ;
        }
    }
}

http://coliru.stacked-crooked.com/a/ed158333f28ea4ef
Thank you! are you able to help me with this problem? I tried to run it into my program but there are errors..

show the output:
1
2
3
4
5
6
7
8
int list [10], i, j;
for ( i = 9; i >= 0; --i )  
{
	list [i] = i * 100 – 5;
	if  ( i % 3  == 1 )  list [i] = list [i+1] * 200;
}
for ( j = 0; j < 10; ++j ) cout << list[j]+ " " ;
cout << endl

Line 4:
1
2
3
// replace \226 (which unfortunately looks like a minus) with an actual minus
// list [i] = i * 100 – 5;
list [i] = i * 100 /* – */ - 5;


Line 7:
1
2
// for ( j = 0; j < 10; ++j ) cout << list[j]+ " " ;
for ( j = 0; j < 10; ++j ) cout << list[j] << " " ; // << instead of + 
I dont think i needed to change anything? my problem was actually what i had written before. I just dont understand how to get the output....
this is what i got:
Output:
0 -5
1 95
2 195
3 295
4 395
5 495
6 595
7 695
8 795
9 895

would this be correct?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    const int SZ = 10 ;
    int list [SZ] ;

    for ( int i = SZ-1; i >= 0; --i )
    {
        list [i] = i * 100 - 5;
        if  ( i % 3  == 1 )  list [i] = list [i+1] * 200;
    }

    for ( int j = 0; j < SZ; ++j ) std::cout << "list[" << j << "] " << list[j] << '\n' ;
}

list[0] -5
list[1] 39000
list[2] 195
list[3] 295
list[4] 99000
list[5] 495
list[6] 595
list[7] 159000
list[8] 795
list[9] 895

http://coliru.stacked-crooked.com/a/4dd29e4b339f6eb5
please explain why you keep on changing the code?.... my issue does not involve const int sz=10, nothing with sz, or std:: cout....it looks like this:

1
2
3
4
5
6
7
8
int list [10], i, j;
for ( i = 9; i >= 0; --i )  
{
	list [i] = i * 100 – 5;
	if  ( i % 3  == 1 )  list [i] = list [i+1] * 200;
}
for ( j = 0; j < 10; ++j ) cout << list[j]+ " " ;
cout << endl;
sorry did not mean to press the reported button!!
This is wrong: cout << list[j]+ " " ;
See http://www.cplusplus.com/forum/beginner/180020/#msg884480


> sorry did not mean to press the reported button!!

You did not press the report button.
If you fix the error in line 7 the output will look like this.

-5 39000 195 295 99000 495 595 159000 795 895

BTW. The task says:

Assume you have an array called “nums” that has 20 integers in it. Also, assume that it is
populated already. Write the code to display the values in “nums” with 4 numbers on each
line separated by a couple of blank spaces.
{ I.e. you will have 5 lines printed, 4 numbers on each line. }

Why do you have only 10 numbers?
Topic archived. No new replies allowed.