help with code, rotating an array

My code outputs the following array first: 0 1 4 9 16 25
That "rotateList" function outputs this: 25 0 1 4 9 16

What do I fix so that the following outputs instead: 25 16 9 4 1 0
Thanks in advance!


Here's my 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>
#include <iomanip>
#include <stdlib.h>
#include "apvector.h"

using namespace std;

void printList (const apvector<int> &list);
void squareList (apvector<int> &list);
void rotateList (apvector<int> list);

const int MAX = 6;

int main()
{
	system ("cls");

	apvector<int> data(MAX);

	for (int i = 0; i < MAX; i++)
		data[i] = i;
	cout << "Array initialized: ";
    printList(data);
	system ("pause");

	squareList(data);
	cout << "Array after call of squareList: ";
	printList(data);
	system ("pause");

	rotateList(data);
	cout << "Array after call of roateList: ";
	printList(data);
	system ("pause");

	return 0;
}

void printList (const apvector<int> &list)
{
	for (int index = 0; index < MAX; index++)
		cout << list[index] << "  ";
	cout << endl << endl;
}

void squareList (apvector<int> &list)
{
	for (int index = 0; index < MAX; index++)
		list[index] = list[index] * list[index];
}

void rotateList (apvector<int> list)
{
	int temp = list[MAX - 1];
	for (int i = MAX - 1; i > 0; i--)
		list[i] = list[i - 1];
	list[0] = temp;
	cout << "Inside of rotateList: ";
	printList (list);
}
Last edited on
Is the output a typo? 25 16 9 4 0 1 isn't a rotate of 0 1 4 9 16 25. Its reversed with 0 and 1 swapped.
Is this the output you wanted?

$ ./a.out
Array initialized: 0  1  2  3  4  5  

Array after call of squareList: 0  1  4  9  16  25  

Inside of rotateList: 25  0  1  4  9  16  

Array after call of roateList: 25  0  1  4  9  16  

Yes, you just forgot to pass by reference for rotateList.
1
2
3
4
5
// New prototype
void rotateList (vector<int> &list);  // <-- You didn't have the &

// New function def
void rotateList (vector<int> &list)   // <-- You didn't have the & 

That's it.
You need to pass by ref. such that the modifications you made to list in the rotateList function are done to the data vector. Without the & you just rotated a local copy of data and that is why you didn't see the correct output when you printed in the main().
That's the thing, 25 0 1 4 9 16 is what outputs with that code up there ^ but the thing is, only 25 is moved to the front. Our teacher wants us to make our code so that the output ends up being 25 16 9 4 1 0 but I'm just stuck now, completely confused.
So, you just want to reverse the list?
0 1 4 9 16 25 reversed is what you want 25 16 9 4 1 0.
Do you have to do it a certain way?
Nope, any way is fine.
Is apvector derived from an STL vector?
I'm actually not sure, I just know apvector is a user defined class.
Last edited on
Honestly, it sounds like your instructor screwed up on the output requirements. rotateList does exactly what one would expect it to do (although it might be useful to be able to specifiy a direction,) but supplying a reverseList isn't difficult.

Set an index beg to 0. Set another index end to MAX-1. Then:
1
2
3
4
5
while beg < end
{
        swap the elements at those indexes 
        increment beg and decrement end.
}


Last edited on
Topic archived. No new replies allowed.