User Defined Array size

Hey Guys!, Beginner programmer here.

Ok so straight to the point, I'm building a simple bubble sort program and i am implementing a user defined array size. So, the code shown below compiles with no errors however, when i run the program it crashes when i go to enter the second element. Any help/tips would be apprecited. ( This is only the early stage of the code)




#include<iostream>

using namespace std;

int main()
{

int usersize;

cout << "Welcome to this sorting engine. Please enter in your array size" << endl;
cin >> usersize;
int* Numbers = new int[usersize];
cout << " Please enter in elements" << endl;
scanf("%d"), Numbers;



delete[] Numbers;
cout << Numbers << endl;







return 0;

}
you should scanf into numbers[someindex]

eg
for(int i = 0; i < usersize; i++)
scanf... blah blah numbers[i];

and consider cin instead of scanf:
cin >> numbers[i]

and, I think your scanf syntax is wrong. which is why to use cin. I think its scanf("%d", numbers[i]);

Last edited on
scanf("%d"), Numbers;
This line makes no sense.

Maybe it's meant to be something like this:

1
2
3
4
for (int i = 0; i < usersize; i++)
{
  scanf("%d", &(Numbers[i]));
}


or, this being C++

1
2
3
4
for (int i = 0; i < usersize; i++)
{
  cin >> Numbers[i];
}


Since you're a beginner, here's some beginner advice. Don't use arrays. They're not for beginners. Use a vector. A vector works how you expect it to.
Last edited on
Thanks guys!
Don't use arrays. They're not for beginners.


Solid advice here.
It would be worth your time to avoid the bubble sort too. Choose a selection sort or an insertion sort instead.

Or, if you really wanna learn something, choose a quicksort or a mergesort. Both are much more interesting than a bubble sort.


Get a list of numbers from the user the Right Way™, because dealing with input is Hard:

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
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

std::istream& operator >> ( std::istream& ins, char c )
{
  ins >> std::ws;
  if (ins.peek() == c) ins.get();
  return ins;
}

int main()
{
  std::vector <int> numbers;
  {
    std::cout << "Enter a list of whole numbers:\n";

    // Get user input as a string
    std::string s;
    getline( std::cin, s );

    // Convert the string into a list of integers
    std::istringstream ss( s );
    int x;
    while (ss >> x >> ',') 
      numbers.push_back( x );

    // Complain if the user entered something that is not an integer
    if (!ss.eof())
    {
      ss.clear();
      std::cerr << std::string( (int)ss.tellg(), ' ' ) << "^ not a whole number\n";
      return 1;
    }
  }

  // now you can sort the numbers
  ...
Enter a list of whole numbers:
1,2, 3 , 4 5 ,6 7
Enter a list of whole numbers:
1 2 3 -4 five 6,7
         ^ not a whole number
Enter a list of whole numbers:
1, 2, 3.14159, 4
       ^ not a whole number

Hope this helps.
I would argue that its about 5 extra lines to write shellsort which is almost as fast as the best sorts out there (its O(n^(x+1)/x) which is slower but generally comparable to NlgN up to fairly large values) yet the code is still very simple, no recursion or anything. Beats me why the profs won't teach it. Selection and insertion are pretty much as bad as bubble. I can't recall but the final pass of shell is insertion or selection, effectively. Its actually still in use in a number of high performance applications for small arrays.

Last edited on
Selection sort is the most natural of all sorting algorithms — it is how people tend to sort things normally. Insertion sort is almost as natural as selection sort, and also in common use by people knowing nothing about CS. Both are very, very easy to understand and implement.

The reason insertion sort remains prominent is because for lists small enough to fit in your L1 cache it is the fastest sorting algorithm bar none. That is why so many algorithms switch to insertion sort once the divide and conquer stage gets to fewer than some number of elements (usually between 30 and 60, depending on your author and any potential tuning).

Shell sort adds an odd complexity (by interleaving) that is not really easy to follow or justify in a beginner’s mind. Both quicksort and mergesort are easier to understand: split the list, repeat on each of the smaller lists, then join the lists back together. They are also stupendously easy to implement, even badly, and avoid having to handle the multiple indexing issues at the same time that you do in bubble and shell sorts.
Topic archived. No new replies allowed.