0

Error compiling C++ code: 'invalid types 'int[int]'

I'm having some trouble trying to figure out why my C++ program won't compile. This is what I've currently written so far: #include <iostream> using namespace std; int main(){ int numofrot, arr, inc, inc2; cin >> arr >> numofrot; int array[arr]; for (inc = 1; inc < arr + 1; inc++){ array[arr] = inc; } for (inc = 0; inc < numofrot; inc++){ for (inc2 = 0; inc2 < arr; inc2++){ if (arr[inc2 + 1] > arr){ arr [inc2] = 1;} else arr[inc2] +=1; } } } return 0; } The compiler returns some errors that I can fix pretty easily, but asides from those, the main errors that are causing me problems are the following: ..\Playground\: In function 'int main()': ..\Playground\:15:29: error: invalid types 'int[int]' for array subscript if (arr[inc2 + 1] > arr){ ^ ..\Playground\:16:26: error: invalid types 'int[int]' for array subscript arr [inc2] = 1;} ^ ..\Playground\:17:30: error: invalid types 'int[int]' for array subscript else arr[inc2] +=1; I understand why these errors are happening, but I do not have the knowledge on how to fix them. Any and all assistance will be greatly appreciated!

28th Nov 2017, 11:49 AM
Clearwater
Clearwater - avatar
8 Respostas
+ 6
Okay try this: #include <iostream> using namespace std; int main() { int elements, rotations; cout << "Number of array elements? "; cin >> elements; cout << elements; int arr[elements]; cout << "\nHow many rotations? "; cin >> rotations; cout << rotations; // Fill, and print the array (initial state) cout << "\n\nInitial state\n"; for(int i = 0; i < elements; i++) { arr[i] = (i + 1) * 10; cout << arr[i] << ' ' ; } // for swapping array element int tmp; // outer loop counts how many rotations // has been performed for(int i = 0; i < rotations; i++) { // copy the first element tmp = arr[0]; // inner loop does the rotation for(int j = 0; j < elements-1; j++) { arr[j] = arr[j+1]; } // put back the copy of the first // element at the end arr[elements-1] = tmp; // print the array (current state) cout << "\n\nRotation " << i+1 << '\n'; for(int k = 0; k < elements; k++) cout << arr[k] << ' ' ; } return 0; }
28th Nov 2017, 5:19 PM
Ipang
+ 5
I guess you should start with verifying whether or not your array contents are as you expected, by printing its elements, if you're sure the array contents are correct, then move on to the next step. Add this below your first loop. for(int i=0;i<arr;i++) cout << array[i] << endl; /* comment out the nested loop, temporarily */ return 0; See the elements' values, are they as you planned them to be? I'm asking you this because I see your first loop, where you fill the array, you did it by; array[arr] = inc; I'm not sure that's what you actually wanted to do, so I need clarification.
28th Nov 2017, 1:55 PM
Ipang
+ 5
@Clearwater, First, I apologize to have misunderstood your actual intention, anyway, could you please provide a simple example, maybe put some line of numbers, that shows the initial order, and how you want it in the end of rotation phase, I hope I can understand you better with a little illustration, if you don't mind. My idea: array order - step 1 [0] [1] [2] [3] [4] array order - step ..n ......................... Something like that mate...
28th Nov 2017, 4:21 PM
Ipang
+ 5
Okay mate, I'll see what I can do about it, hang on, I'll post back when I have something okay?
28th Nov 2017, 4:31 PM
Ipang
+ 4
@Clearwater, @kurwius has eyes of an eagle, I missed the points he spotted on your code, I would make a note on it, for further reference, and I hope you do too, it is a valuable feedback :)
28th Nov 2017, 7:27 PM
Ipang
+ 1
I forgot to mention what I'm trying to code as well; my bad. I'll explain: I'm trying to make an array rotator without simply using the vec::rotate function (As you can see I don't have it included). To do this, I declare a variable, read input from it, declare an array the size of the input, and have each position of the array numbered from 1 up to the max number of the array. Next, as you can see, I created a variable called 'numofrot'. This is meant to be the amount of times the array is to be rotated. In order to rotate it, what I tried doing is this: Create a for loop from 0 to the number of rotations desired, Create another for loop so the array can be 'scanned' multiple times and adjusted with each pass, Check each number in the variable of the array. If incrementing it by 1 will cause the number to be greater than the number of the variable size, set it to 1. If not, then increment it by 1. Once the end of the array has been reached, repeat it by the number of times that was declared in numofrot. Finally, output the contents of the rotated array. I hope this helps explain what I'm trying to achieve.
28th Nov 2017, 4:02 PM
Clearwater
Clearwater - avatar
+ 1
Okay, let's start with saying that I have an array size of 5, and I wanna rotate it twice. So the array at the start would be: 1 2 3 4 5 Now I wanna rotate it twice. In which case, I'd do something like this: 2 3 4 5 1 For a first rotation, and then for a second rotation, I'd obtain this: 3 4 5 1 2
28th Nov 2017, 4:27 PM
Clearwater
Clearwater - avatar
+ 1
Alright, I'll tinker about with the answers you gave me and see if I can get something working. Much obliged to both of you! ^^
28th Nov 2017, 6:05 PM
Clearwater
Clearwater - avatar