ARRAYS: putting numbers in order

i dont know why this is doing this, but when i have the user input 4 numbers, there is no output.

PROBLEM: no output
OUTPUT: should be in order (From low to high)

is anyone able to see what my issue is here?

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

int main()
{
    // allows user inputs
  const int SIZE = 4;
  int a[SIZE];
  int i;
  for (i = 0; i < SIZE; i++)
  {
    cout << "Enter score [" << i << "]: ";
    cin >> a[i];
    cin.ignore(1000, 10);
  }
    
  
  int size;
  cin >> size;
  cin.ignore(1000, 10);
  int* score = new int[SIZE];
  
  
  for (i = 0; i < size; i++)
    cout << score[i] << ' ';
  cout << endl;
  
  delete [] score;
  
  cin.ignore();
  cin.get();
  return 0;
}
Last edited on
Nowhere are you actually sorting the numbers, which are stored in a[].

What is the purpose of lines 19-22? It simply asks for the user to input a number and then
creates an array with that many (uninitialized) elements. Then lines 25-27 output the
uninitialized array.

i recreated the program. now my issue is how to sort the numbers. considering that they are sorted in a[], should i create subprograms that has parameters to make this happen? or is there a simpler way to do so?

my current program looks similar, but just deleted things here and there...

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

int main()
{
    // allows user inputs
  const int SIZE = 4;
  int a[SIZE];
  int i;
  for (i = 0; i < SIZE; i++)
  {
    cout << "Enter score a[" << i << "]: ";
    cin >> a[i];
    cin.ignore(1000, 10);
  }
    
  
  int size;
  
  for (i = 0; i < SIZE; i++)
    cout << a[i] << ' ';
    cout << endl;
  
  cin >> size;
  cin.ignore(1000, 10);
 
    
  

  cin.ignore();
  cin.get();
  return 0;
}
all you have to do is to search on google for sorting alghoritms - you will find a whole bunch of various alghoritms including examples. It's worth to look through them all, to learn something about complexity, which may be helpful in the (near) future
Greets
JK
The standard library has some sorting algorithms: http://www.cplusplus.com/reference/algorithm/sort/
Last edited on
thank you for the help you guys! after reading a LOT of links yall helped me with, i went for the QSORT think. so thank you. =)
Topic archived. No new replies allowed.