Help with a beginner's sorting array error?

Hello everyone,
I am having some trouble with this code here, for some reason it refuses to run, claiming "array type int[n] is not assignable. This project started out as an attempt to try to make working functions that I’d reference in the main code, however I was having enough trouble I decided to just try to make a normal project work first before trying to make it translate into a more complex form. I compared my layout to another example online that supposedly works, it is nearly a carbon copy unintentionally. I’m not certain what is wrong with it. That being said, I am a beginner and teaching myself from a textbook and then creating assignments for myself to try to practice creating unique projects myself.
Thank you all so much, don’t hold back if you see any glaring issues, or poor programming style/etiquette. I tend to do commenting at the end of a project when it is finally working, so I do recognize that is something I should probably have already. I’ll post the code in here

#include <iostream> #include <cmath> using namespace std; int main() { int j; double arry[j]; cout << "What is the size of the array?" << endl; cin >> j; for (int i = 0; i < j; i++) { cout << "Enter the value for spot #" << i << "in the array" << endl; cin >> arry[i]; } for (int i = 0; i < j; i++) { for (int a = i + 1; a < j; a++) { if (arry[a] < arry[i]) { int temp = arry[i]; arry[i] = arry[a]; arry[a] = temp; } } } cout << "The sorted array: "; for (int i = 0; i < j; i++) { cout << arry[i]; } system("PAUSE"); return 0; }

Hi! Welcome to Codecademy forums.

The problem is that you are initializing the array with the length j before j having a value, that’s not permitted to do so. Try changing orders and set j’s value before declaring the array.

Fantastic thank you so much!

Always! Arrays in C++ can get confusing, good luck on your journey!

So I did give a shot rearranging the order and declaring the value of j, in addition to making sure “arry” and “j” had the same value type, as i previously was intermixing double and integer. Sadly that didn’t seem to work still, unless if I’m missing something. I included an image of the error list and also the new code. I may have also botched up what you advised I do.

#include <iostream> #include <cmath> using namespace std; int main() { int j = 0; cout << "What is the size of the array?" << endl; cin >> j; int arry[j]; for (int i = 0; i < j; i++) { cout << "Enter the value for spot #" << i << "in the array" << endl; cin >> arry[i]; } for (int i = 0; i < j; i++) { for (int a = i + 1; a < j; a++) { if (arry[a] < arry[i]) { int temp = arry[i]; arry[i] = arry[a]; arry[a] = temp; } } } cout << "The sorted array: "; for (int i = 0; i < j; i++) { cout << arry[i]; } system("PAUSE"); return 0; }

Ok, so arrays in C++ works somewhat different from many languages. The array you want is a dynamic array ie its length can be determined after compiling (the user can choose the length) but the initialization you use is one for a fixed array. Fixed array’s lengths has to be known before compilation.

Dynamic arrays are somewhat advanced for a beginner and you need more stuff the learn before like pointers. Especially pointers. You can refer here to read more https://www.learncpp.com/cpp-tutorial/dynamically-allocating-arrays/

Edit: I forgot to mention some compilers will actually compile and work the code will work the intended way but it’s not a good practice and will cause problems like one you are dealing. That would explain the working example you saw. I tried an online compiler and it worked as expected with just the change of order like:

1 Like

Ok thank you for double checking on that for me, it’s very much appreciated. I definitely will take your advice and program this properly in a way that works on visual studio, just seems like it would develop better habits. I may also consult you on an example of code that I saw in a book I was reading that didn’t work when I typed it in verbatim. It is “C++ without fear” by Brian Overland. He had an example piece of code relating to sorting arrays, where he changed the variable name halfway through the code.