The program got error (Array)

The output should be like this
ARRAY IS NICE TO BE USED & FUN TO PLAY!
ARRAY IS NICE TO BE USED & FUN TO PLAY!
! ARRAY IS NICE TO BE USED & FUN TO PLAY
Y! ARRAY IS NICE TO BE USED & FUN TO PLA
AY! ARRAY IS NICE TO BE USED & FUN TO PL
LAY! ARRAY IS NICE TO BE USED & FUN TO P
PLAY! ARRAY IS NICE TO BE USED & FUN TO
PLAY! ARRAY IS NICE TO BE USED & FUN TO
O PLAY! ARRAY IS NICE TO BE USED & FUN T
TO PLAY! ARRAY IS NICE TO BE USED & FUN

But now it shows different.
What is the problem?
Here is the code:

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
using namespace std;

void display (char fun[], int funsize)
{
    for (int i=0; i<funsize; i++ )
        cout<<fun[i];
cout<<endl;
}

void rotateRight (char a[], int funsize)
{
    char temp = a[funsize];
    for (int j = 0; j<=funsize - 1; j++)
        a[j] = a[j-1];
        a[0] = temp;
}

int main()
{
    const int funsize = 41;
    char fun[funsize] = "ARRAY IS FUN TO BE USED & FUN TO PLAY!";

    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    rotateRight(fun, funsize);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
}
Your current array size is 39 (with null).
line 22 should be "NICE" not "FUN" at the start.
With the extra letter the new size would be 40.

Also this can be in a loop
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
display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    rotateRight(fun, funsize);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);
    display (fun, funsize);
    rotateRight(fun, funsize-1);


Also line 16 you are assigning the null character to the first element.
When j == 0 what is the value of j - 1 in the array? Anyways this would essentially be assigning the first letter to each of them. I would start from the right and work towards the left.

I made a few simple changes to your code.

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
#include <iostream>
using namespace std;

void display (char fun[], int funsize)
{
    cout << fun << endl; //its null terminated so it will output like this
}

void rotateRight (char a[], int funsize)
{
    char temp = a[funsize-2]; //size - 1 == null, so last character is size - 2
    for(int i = funsize - 2; i > 0; --i)
        a[i] = a[i-1];
    a[0] = temp; 
}

int main()
{
    const int funsize = 40;
    char fun[funsize] = "ARRAY IS NICE TO BE USED & FUN TO PLAY!";

    for(int i = 0; i < 20; ++i)
    {
        display (fun, funsize);
        rotateRight(fun, funsize);
    }
}
ARRAY IS NICE TO BE USED & FUN TO PLAY!
!ARRAY IS NICE TO BE USED & FUN TO PLAY
Y!ARRAY IS NICE TO BE USED & FUN TO PLA
AY!ARRAY IS NICE TO BE USED & FUN TO PL
LAY!ARRAY IS NICE TO BE USED & FUN TO P
PLAY!ARRAY IS NICE TO BE USED & FUN TO 
 PLAY!ARRAY IS NICE TO BE USED & FUN TO
O PLAY!ARRAY IS NICE TO BE USED & FUN T
TO PLAY!ARRAY IS NICE TO BE USED & FUN 
 TO PLAY!ARRAY IS NICE TO BE USED & FUN
N TO PLAY!ARRAY IS NICE TO BE USED & FU
UN TO PLAY!ARRAY IS NICE TO BE USED & F
FUN TO PLAY!ARRAY IS NICE TO BE USED & 
 FUN TO PLAY!ARRAY IS NICE TO BE USED &
& FUN TO PLAY!ARRAY IS NICE TO BE USED 
 & FUN TO PLAY!ARRAY IS NICE TO BE USED
D & FUN TO PLAY!ARRAY IS NICE TO BE USE
ED & FUN TO PLAY!ARRAY IS NICE TO BE US
SED & FUN TO PLAY!ARRAY IS NICE TO BE U
USED & FUN TO PLAY!ARRAY IS NICE TO BE 


*edit if you don't count the null as part of the size you could change the -2 to -1 and then when you call it subtract 1.
Last edited on
Topic archived. No new replies allowed.