Ascending and Descending order with n size

Hello. I am a beginner at C++ and I am trying to create an Ascending and Descending order with n size. My problem is that if input higher than 6 in the size the rest of the code will not work. I don't even know what is the problem but when I try using the const int size it works. Any help would be greatly appreciated!

Here is the 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<iomanip>
using namespace std;


int main(){
	

	int size;
	double *pt1, *pt2;
	pt1 = new double[size];
	pt2 = new double[size];

	int i, j, man = 0, temp;
	//executable
	
	
	do{
		cout << "How many numbers you would like to sort?";
		cin >> size;
		
		if (size >= 5 && size <= 20)
		{
			for(i = 0; i < size; i++)
			{
				cout <<"index" << "[" << i << "]" << setw(3);
				cin >> pt1[i];
			}
		}
		else
		{
			cout << "Accept only 5 to 20 input numbers for sorting!" << endl;
		}
	} while (i < size);
	
	for(j = 0; j < size; j++)
		for(i = 0; i < size; i++)
		{
			temp;
			if(pt1[i] > pt1[i+1])
			{
				temp = pt1[i];
				pt1[i] = pt1[i+1];
				pt1[i+1] = temp;
			}
		}
	cout << "\n\n";
	cout << "Ascending Order" << endl;
	for(i = 0; i < size; i++)
	{
		cout << pt1[i+1] << setw(3) << endl;
	}
	

	for(j = 0; j < size; j++)
		for(i = 0; i < size; i++)
		{
			temp;
			if(pt1[i] < pt1[i+1])
			{
				temp = pt1[i];
				pt1[i] = pt1[i+1];
				pt1[i+1] = temp;
			}
		}
	cout << "\n\n";
	cout << "Descending  Order" << endl;
	for(i = 0; i < size; i++)
	{
		cout << pt1[i] << setw(3)<<endl;
		
	}
}
close but you have an order of operations issue.
what is the value of size in your new statements on line 11/12 ?
it is not defined there!
you need to read in size, THEN call new. (move the news to line 23 or so).

your do-while is a little weird as well, based off i which is modifed in the for. I think it is correct, but its 'kind of odd' way to do it.
Last edited on
Oh I know see the problem, it works now! Thanks for the help! It
The sort loops have a buffer overflow issue as i is from 0 to size - 1, but i + 1 is used.

There is a memory leak as the allocated memory is not deleted.

the stl swap() can be used.

Also, unless this is an exercise in writing a very basic sort, there is also the std sort() function.

Consider:

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
#include <iostream>
#include <iomanip>
#include <utility>
#include <algorithm>
#include <functional>

using namespace std;

int main()
{
	int size {};

	do {
		cout << "How many numbers you would like to sort? ";
		cin >> size;

	} while ((size < 5 || size > 20) && (cout << "Accept only 5 to 20 input numbers for sorting!\n"));

	double* pt1 {new double[size]};

	for (size_t i = 0; i < size; ++i) {
		cout << "index" << "[" << i << "] ";
		cin >> pt1[i];
	}

	//sort(pt1, pt1 + size);

	for (size_t j = 0; j < size; ++j)
		for (size_t i = 0; i < size - 1; ++i)
			if (pt1[i] > pt1[i + 1])
				swap(pt1[i], pt1[i + 1]);


	cout << "\n\nAscending Order\n";
	for (size_t i = 0; i < size; ++i)
		cout << pt1[i] << '\n';

	//sort(pt1, pt1 + size, greater<double>());

	for (size_t j = 0; j < size; ++j)
		for (size_t i = 0; i < size - 1; ++i)
			if (pt1[i] < pt1[i + 1])
				swap(pt1[i], pt1[i + 1]);


	cout << "\n\nDescending Order\n";
	for (size_t i = 0; i < size; ++i)
		cout << pt1[i] << '\n';

	delete[] pt1;
}

Topic archived. No new replies allowed.