Project Remake!

How will I fix the arrangement of numbers stored in an array?

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <limits>
using namespace std;

int limit=0, c, i, shift, order, sum=0;
int*counter = new int[limit];

int limitFunction()
{
    while (!(cin >> limit))
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(),'\n');
        cout << "Error! Please try again.\n";
        cout << "How many integers will you sort? ";
    }
    return (limit);
}

int counterFunction()
{
    while (!(cin >> counter[c]))
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(),'\n');
        cout << "Error! Please try again.\n";
        cout << "Enter no. " << c+1 << ": ";
    }
    return (limit);
}

int AscendingFunction()
{
    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 << " ";
    return (limit);
}

int DescendingFunction()
{
    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 << " ";
    return (limit);
}

int orderFunction()
{
    cout << "Choose Order: \n";
    cout << "[1]Ascending\n";
    cout << "[2]Descending\n";
    while(!(cin >> order))
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(),'\n');
        cout << "Error! Please try again.\n";
        cout << "Choose Order: \n";
        cout << "[1]Ascending\n";
        cout << "[2]Descending\n";
    }
    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;
                }
            }
        }
    AscendingFunction();
    }
    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;
                }
            }
        }
    DescendingFunction();
    }
    return (limit);
}

int main()
{
    cout << "How many integers will you sort? ";
    limit = limitFunction();
    
    for (c=0;c<limit;c++)
    {
        cout << "Enter no. " << c+1 << ": ";
        counter[c] = counterFunction();
    }
    orderFunction();
    delete[] counter;
    return (limit);
}


Sample output:
How many integers will you sort? 2
Enter no. 1: 1
Enter no. 2: 2
Choose Order:
[1]Ascending
[2]Descending
2

Numbers in Descending: 2 2
Exit code: 2
Last edited on
My first suggestion it that you git rid of all those global variables. Make the variables local to functions and pass the required values to and from your functions as required.
Line 6: How many ints do you think are being allocated here? Hint: limit is 0.

Line 32,50: Other than the prompts, what is the difference between these two functions? Hint: none. So why have two different functions?

Lines 82-95,98-113: This is where you should have different functions.

Line 89-93, 105-109: This code is identical. Write a swap function or use std::swap.
http://www.cplusplus.com/reference/algorithm/swap/

Lines 8-18,20-30,70-81: Your input logic is nearly identical, except for the prompt. Consider writing a single function that accepts a prompt as an argument and returns an int value.

Lines 5-6: Get rid of the globals.

Line 129: Why is main() returning limit? main should return 0 if successful. A non-zero value tells the operating system that the program was not successful.
Last edited on
Topic archived. No new replies allowed.