Array Bug! PLEASE HELP me for my 1st Semester project.

Try to compile this code, there's a bug. When I input "10" as my "Limit", the array will exceed up to 11 and so on. And also, when I choose ascending, in the end, the "Invalid Order! " will show up. the third condition is supposed to pop-up when the user inputs "3 above or 0 below" in the "order". But whenever I input 2 at the order, in the end, the condition three will pop-up. Here's my code (sorry for my bad english. By the way, Im from philippines.

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
75
76
77
#include <iostream>
using namespace std;

int main()
{
    int sum=0,limit=0,array[limit],c,i,shift,order;
    
    cout << "How many integers will you sort? ";
    cin >> limit;
    for (c=0;c<limit;c++)
    {
        cout << "Enter number " << c+1 << ": ";
        cin >> array[c];
    }
    cout << "Choose order: [1]Ascending or [2]Descending ";
    cin >> order;
    if (order==1)
    {
        for (c=0;c<limit;c++)
        {
            for (i=c+1;i<limit;i++)
            {
                if (array[c]>array[i])
                {
                    shift=array[c];
                    array[c]=array[i];
                    array[i]=shift;
                }
            }
        }
        cout << "Numbers in Ascending: ";
        {
            for (c=0;c<limit;c++)
            {
                cout << array [c] << " ";
            }
        }
        cout << "\nTotal sum: ";
        for (c=0;c<limit;c++)
        {
            sum+=array[c];
        }
        cout << sum << " ";
    }
    if (order==2)
    {
        for (c=0;c<limit;c++)
        {
            for (i=c+1;i<limit;i++)
            {
                if (array[c]<array[i])
                {
                    shift=array[c];
                    array[c]=array[i];
                    array[i]=shift;
                }
            }
        }
        cout << "Numbers in Descending: ";
        {
            for (c=0;c<limit;c++)
            {
                cout << array [c] << " ";
            }
        }
        cout << "\nTotal sum: ";
        for (c=0;c<limit;c++)
        {
            sum+=array[c];
        }
        cout << sum << " ";
    }
    if (order>=3||order<=0)
    {
        cout << "Invalid Order! ";
    }
}
Last edited on
http://www.cplusplus.com/articles/z13hAqkS/


Please use code tags :+)

Code tags were actually there when you first made a post, how is it that they always manage to get deleted by beginners?

The code does not compile.

One cannot have a zero length array. And the size of the array has to be a compile time constant.

array is a bad name for a variable.
First, let's work on your indention style... and code tagging...

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
75
76
77
#include <iostream>
using namespace std;

int main()
{
    int sum=0,limit=0,array[limit],c,i,shift,order;

    cout << "How many integers will you sort? ";
    cin >> limit;
    for (c=0; c<limit; c++)
    {
        cout << "Enter number " << c+1 << ": ";
        cin >> array[c];
    }
    cout << "Choose order: [1]Ascending or [2]Descending ";
    cin >> order;
    if (order==1)
    {
        for (c=0; c<limit; c++)
        {
            for (i=c+1; i<limit; i++)
            {
                if (array[c]>array[i])
                {
                    shift=array[c];
                    array[c]=array[i];
                    array[i]=shift;
                }
            }
        }
        cout << "Numbers in Ascending: ";
        {
            for (c=0; c<limit; c++)
            {
                cout << array [c] << " ";
            }
        }
        cout << "\nTotal sum: ";
        for (c=0; c<limit; c++)
        {
            sum+=array[c];
        }
        cout << sum << " ";
    }
    if (order==2)
    {
        for (c=0; c<limit; c++)
        {
            for (i=c+1; i<limit; i++)
            {
                if (array[c]<array[i])
                {
                    shift=array[c];
                    array[c]=array[i];
                    array[i]=shift;
                }
            }
        }
        cout << "Numbers in Descending: ";
        {
            for (c=0; c<limit; c++)
            {
                cout << array [c] << " ";
            }
        }
        cout << "\nTotal sum: ";
        for (c=0; c<limit; c++)
        {
            sum+=array[c];
        }
        cout << sum << " ";
    }
    if (order>=3||order<=0)
    {
        cout << "Invalid Order! ";
    }
}



There we go...

I get no errors. It compiles fine in code blocks array is a VERY bad name for a variable.
I don't see the problem you mention.
Last edited on
It compiles, but doesn't run well. zero length array is UB.

How many integers will you sort? 10 Enter number 1: 1 Enter number 2: 2 Choose order: [1]Ascending or [2]Descending 2 Numbers in Descending: 2 Total sum: 2  


How many integers will you sort? 5 Enter number 1: 2 Enter number 2: 8 Enter number 3: 6 Enter number 4: 4 Enter number 5: 23 Enter number 6: 25 Enter number 7: 84 Enter number 8:  
So, how will I gonna end up this problem @TheIdeasMan?
Anyway, thank you for the correction of the variable name "Array". I'll change it to my name. :+)
closed account (48T7M4Gy)
How many integers will you sort? 7
Enter number 1: 2
Enter number 2: 4
Enter number 3: 3
Enter number 4: 5
Choose order: [1]Ascending or [2]Descending 1
Numbers in Ascending: 1 2
Total sum: 3 Invalid Order!


It does compile but the output is suss.

OOPs, this duplicates TheIdeasMan findings. Well, nothing like confirmation I guess.
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
int main()
{
	int limit = 0, sum = 0, c, i, shift, order;

	cout << "How many integers will you sort? ";
	cin >> limit;

	int *array = new int[limit];

How many integers will you sort? 5
Enter number 1: 1
Enter number 2: 2
Enter number 3: 3
Enter number 4: 4
Enter number 5: 5
Choose order: [1]Ascending or [2]Descending 2
Numbers in Descending: 5 4 3 2 1 
Total sum: 15  
Exit code: 0 (normal program termination)
Last edited on
Thanks @kemort! It worked! I just edited my code and put your initial code to it and changed variable name "array" to "counter".Bytheway, what technique did you used in this line
int * array = new int[limit];
? My code now is,
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
75
76
77
78
79
#include <iostream>
using namespace std;

int main()
{
    int limit = 0, sum = 0, c, i, shift, order;

	cout << "How many integers will you sort? ";
	cin >> limit;

	int * counter = new int[limit];
    for (c=0; c<limit; c++)
    {
        cout << "Enter number " << c+1 << ": ";
        cin >> counter[c];
    }
    cout << "Choose order: [1]Ascending or [2]Descending ";
    cin >> order;
    if (order==1)
    {
        for (c=0; c<limit; c++)
        {
            for (i=c+1; i<limit; i++)
            {
                if (counter[c]>counter[i])
                {
                    shift=counter[c];
                    counter[c]=counter[i];
                    counter[i]=shift;
                }
            }
        }
        cout << "Numbers in Ascending: ";
        {
            for (c=0; c<limit; c++)
            {
                cout << counter[c] << " ";
            }
        }
        cout << "\nTotal sum: ";
        for (c=0; c<limit; c++)
        {
            sum+=counter[c];
        }
        cout << sum << " ";
    }
    if (order==2)
    {
        for (c=0; c<limit; c++)
        {
            for (i=c+1; i<limit; i++)
            {
                if (counter[c]<counter[i])
                {
                    shift=counter[c];
                    counter[c]=counter[i];
                    counter[i]=shift;
                }
            }
        }
        cout << "Numbers in Descending: ";
        {
            for (c=0; c<limit; c++)
            {
                cout << counter[c] << " ";
            }
        }
        cout << "\nTotal sum: ";
        for (c=0; c<limit; c++)
        {
            sum+=counter[c];
        }
        cout << sum << " ";
    }
    if (order>=3||order<=0)
    {
        cout << "Invalid Order! ";
    }
}
Last edited on
closed account (48T7M4Gy)
Cheers, you should thank TheIdeasMan too.

Hopefully dynamic array, zero lengths and compilers are a bit clearer beyond just getting it to run. We all hope you meet your deadline.

PS Pearlyman deserves thks too.
Last edited on
closed account (48T7M4Gy)
int * array = new int[limit];


Check out dynamic arrays when you get time. You can't declare an array the way you were trying.

http://www.cplusplus.com/doc/tutorial/dynamic/
Hi,

http://www.cplusplus.com/reference/new/operator%20new%5B%5D/


Btw did you know there is reference and tutorial info at the top left of this page?

As kemort was saying std::array allows one to use a variable to set the size of the array initially, it can't be changed afterwards.

Important to note you need a delete[] to go with the new in your code, after you have finished with the variable, otherwise you will be leaking memory. delete[] is for arrays, delete is for ordinary variables.

Before you get into what I mention in the next paragraph, it's best to learn about STL containers such as std::vector so you won't have worry about any of that.

Also note that that use of new and delete isn't really recommended (although there is no harm with it here), instead check out smart pointers as in std::unique_ptr

The reason for this is due to situations where delete is not called, such as when an exception is thrown, thereby still leaking memory.

Good Luck !!
closed account (48T7M4Gy)
The reason for this is due to situations where delete is not called, such as when an exception is thrown, thereby still leaking memory.


And such as when I forget, Gulp.
And such as when I forget, Gulp.


Well not to worry, if there was a global fund for coder's mistakes, it would have more than substantial funds - I reckon. I guess the key thing is how far the mistake goes before it is noticed and fixed :+)

Regards
Btw, the deadline of my project is on 11/5/15.
Last request/question guys. What code will I use to fix this error. Uhmm, I don't know how to state this problem because I'm not so good at english. Btw, try to compile it after you compiled it, try to input "number+alphabet or alphanumeric" like "3d" or "3;" on the first question and the program will say
Enter number 1: Invalid! Please try again.
and the program will just go suck its balls. Haha :) .How will I gonna fix that ?
My code is,
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <limits>
using namespace std;

int main()
{
    int limit = 0, sum = 0, c, i, shift, order;

	cout << "How many integers will you sort? ";
	cin >> limit;
    while (!cin >> limit)
    {
        cin.clear();
        cin.ignore(numeric_limits<int>::max(), '\n');
        cout << "Invalid! Please try again. \n";
        cout << "How many integers will you sort? ";
	    cin >> limit;
    }
	int * counter = new int[limit];
    for (c=0; c<limit; c++)
    {
        cout << "Enter number " << c+1 << ": ";
        cin >> counter[c];
        while (!cin >> counter[c])
        {
        cin.clear();
        cin.ignore(numeric_limits<int>::max(), '\n');
        cout << "Invalid! Please try again. \n";
        cout << "Enter number " << c+1 << ": ";
        cin >> counter[c];
        }
    }
    cout << "Choose order: [1]Ascending or [2]Descending ";
    cin >> order;
    while (!cin >> order||order>=3||order<=0)
    {
        cin.clear();
        cin.ignore(numeric_limits<int>::max(), '\n');
        cout << "Invalid! Please try again. \n";
        cout << "Choose order: [1]Ascending or [2]Descending ";
        cin >> order;
    }
    if (order==1)
    {
        for (c=0; c<limit; c++)
        {
            for (i=c+1; i<limit; i++)
            {
                if (counter[c]>counter[i])
                {
                    shift=counter[c];
                    counter[c]=counter[i];
                    counter[i]=shift;
                }
            }
        }
        cout << "Numbers in Ascending: ";
        {
            for (c=0; c<limit; c++)
            {
                cout << counter[c] << " ";
            }
        }
        cout << "\nTotal sum: ";
        for (c=0; c<limit; c++)
        {
            sum+=counter[c];
        }
        cout << sum << " ";
    }
    if (order==2)
    {
        for (c=0; c<limit; c++)
        {
            for (i=c+1; i<limit; i++)
            {
                if (counter[c]<counter[i])
                {
                    shift=counter[c];
                    counter[c]=counter[i];
                    counter[i]=shift;
                }
            }
        }
        cout << "Numbers in Descending: ";
        {
            for (c=0; c<limit; c++)
            {
                cout << counter[c] << " ";
            }
        }
        cout << "\nTotal sum: ";
        for (c=0; c<limit; c++)
        {
            sum+=counter[c];
        }
        cout << sum << " ";
    }
    delete[] counter;
}


The program will ignore alphabet or alphabet but with number on it, like "number+alphabet or alphabet", it will suck. Hoping that you can help me :)
Thanks in advance guys! :)
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
 for (c=0; c<limit; c++)
    {
        cout << "Enter number " << c+1 << ": ";
        cin >> counter[c];
        while (!cin ) //<--
        {
        cin.clear();
        cin.ignore(numeric_limits<int>::max(), '\n');
        cout << "Invalid! Please try again. \n";
        cout << "Enter number " << c+1 << ": ";
        cin >> counter[c];
        }
    }


1. You need to use blank lines to break up your code into blocks to make it easier to read/debug
2. The problem lies in the block I have extracted above
Last edited on
Topic archived. No new replies allowed.