Array Referencing Errors

Hello All. I'm new to C++ and am working on Arrays for the first time. I'm trying to create a program that takes input for an array, then orders the input (using swap function) in ascending then descending order.

I am getting a handful of errors I haven't been able to fix all day.

WARNING: My program may make the experts cringe. I apologize in advance.

All help greatly, greatly appreciated:

EDIT: I haven't added the descending order output yet. Will do that once I figure out the errors. Thank you in advance!

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
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

void InputArray (int[]);
void OutputArray (int[]);
void Swap (int[], int[]);
void SortIntegers(int[], int[], int[], int[], int[]);

const int ARRAY_MAX = 5; // Constant is 5 because we are accepting exactly 5 integers (0,1,2,3,4)

int main ()
{
    int intNumbersArray[ARRAY_MAX];
    InputArray(intNumbersArray);
    OutputArray(intNumbersArray);
    
    system("pause");
    return 0;  
}
  
void InputArray (int intNumbersArray[])
{

    for (int intCounter = 0; intCounter < ARRAY_MAX; intCounter++)
    {
        cout << "Enter an integer: " << endl;
        cin >> intNumbersArray[intCounter]; 
    }
  
}

void OutputArray(int intNumbersArray[])
{
    int intCounter = 0;
    
    for (int intCounter = 0; intCounter < ARRAY_MAX; intCounter++)
    {   
        cout << intNumbersArray[0] << intNumbersArray[1] << intNumbersArray[2] << intNumbersArray[3] << intNumbersArray[4] << endl;
    }
    
    cout << "You entered: " << endl;
    cout << intNumbersArray[intCounter] << endl;
    
    cout << "Integers in Ascending order (lowest-to-greatest)" << endl;
    
    
    
    SortIntegers (&intNumbersArray[0], &intNumbersArray[1],&intNumbersArray[2],&intNumbersArray[3],&intNumbersArray[4]);
    cout << intNumbersArray[0] << intNumbersArray[1] << intNumbersArray[2] << intNumbersArray[3] << intNumbersArray[4] << endl;

}

void Swap(int &intValueX, int &intValueY)
{     
     int intValueTemp = intValueX;
     
     intValueTemp = intValueY;
     intValueY = intValueX;
     intValueX = intValueTemp;
}

// Function to sort integers in ascending order (lowest to greatest)
void SortIntegers(int &intNumbersArray[0],int &intNumbersArray[1],int &intNumbersArray[2],int &intNumbersArray[3],int &intNumbersArray[4])
{
     while (true)     // Sort Ascending Conditions
     {   
           if (intNumbersArray[0] > intNumbersArray[1])
           {
              Swap(intNumbersArray[0],intNumbersArray[1])           
           }
           
           if (intNumbersArray[1] > intNumbersArray[2])
           {
              Swap(intNumbersArray[1],intNumbersArray[2])           
           }
     
           if (intNumbersArray[2] > intNumbersArray[3])
           {
              Swap(intNumbersArray[2], intNumbersArray[3]);
           }
           
           if (intNumbersArray[3] > intNumbersArray[4])
           {
              Swap(intNumbersArray[3], intNumbersArray[4]);
           }
           
           if (intNumbersArray[1] > intNumbersArray[4])
           {
              Swap(intNumbersArray[1],intNumbersArray[4])           
           }
       
           if ((intNumbersArray[0] < intNumbersArray[1]) && (intNumbersArray[1] < intNumbersArray[2]) && (intNumbersArray[2] < intNumbersArray[3]) &&
               (intNumbersArray[3] < intNumbersArray[4) && (intNumbersArray[1] < intNumbersArray[4]))
           {
               break;
           }
     }  
     
     while (true)    // Sort Descending Conditions
     {   
           if (intNumbersArray[0] < intNumbersArray[1])
           {
              Swap(intNumbersArray[0],intNumbersArray[1])           
           }
           
           if (intNumbersArray[1] < intNumbersArray[2])
           {
              Swap(intNumbersArray[1],intNumbersArray[2])           
           }
     
           if (intNumbersArray[2] < intNumbersArray[3])
           {
              Swap(intNumbersArray[2], intNumbersArray[3]);
           }
           
           if (intNumbersArray[3] < intNumbersArray[4])
           {
              Swap(intNumbersArray[3], intNumbersArray[4]);
           }
           
           if (intNumbersArray[1] < intNumbersArray[4])
           {
              Swap(intNumbersArray[1],intNumbersArray[4])           
           }

           if ((intNumbersArray[0] > intNumbersArray[1]) && (intNumbersArray[1] > intNumbersArray[2]) && (intNumbersArray[2] > intNumbersArray[3]) &&
               (intNumbersArray[3] > intNumbersArray[4) && (intNumbersArray[1] > intNumbersArray[4]))
           {
               break;
           }
     }
     
     
}
Last edited on
closed account (zb0S216C)
It seems the parameter list of ::SortIntegers() is causing problems. You've named all parameters the same, which means the compiler cannot disambiguate the parameters. Also, you've made them references, and the compiler doesn't allow an array of references. Instead, you'll have to rename the accordingly. For instance:

 
void Sort_integers(int pArray_1[], int pArray_2, ...)


Finally, why do each of the parameters have lengths? If the length of an array is known, make it a reference to an array. Like so:

 
void Sort_integers(int(&pArray_1)[10], int(&pArray_2)[4], ...)

Wazzak
Last edited on
I wasn't trying to make them have different lengths, I was actually trying to pull the value of each part of the array... I was under the impression that intNumbersArray[0] would pull the first value the user entered, intNumbersArray[1] would pull the second, etc.

So that is determining the length, not the value entered of the array?
closed account (zb0S216C)
crumbhead wrote:
I wasn't trying to make them have different lengths

The compiler sees your code differently. It's one thing to write code, but the hard part is knowing whether the compiler agrees with you or not.

crumbhead wrote:
So that is determining the length, not the value entered of the array?

Yeah'p.

Wazzak
Last edited on
Ok, thank you. I guess the reason I'm confused is because I tried to simply do :
cout<< intNumbersArray[3] << endl;

Without the rest of the functions, and it outputs the 4th integer that the user entered. So why is it different when trying to pass the value that occupies intNumberArray[3] to a function?

I'm sure there is some fundamental flaw with my logic here, I'm just trying to figure out what it is exactly.

EDIT: And what would replace intNumbersArray[x] throughout my code to make the functions pass properly?
Last edited on
Because in a parameter list you are basically defining a variable; writing [3] when defining an array specifies length in both those cases. If you want to pass a specific element, just pass the element directly by making the parameter the element type.
when defining an array specifies length in both those cases.


How? When I output [3], it outputs the value of that block in the array. So why is it different?

How should this function be properly written if that isn't the case?:

void SortIntegers(int &intNumbersArray[0],int &intNumbersArray[1],int &intNumbersArray[2],int &intNumbersArray[3],int &intNumbersArray[4])

EDIT: I've read the tutorials, but I still can't figure it out. From what I read, the function should read:

void SortIntegers(int &intNumbersArray[]

.. but this doesn't run and I imagine wouldn't pass the array values properly.
Last edited on
Any additional help would be greatly appreciated. Thank you to everyone who has helped me so far as well, I really appreciate it.
Using loops will greatly reduce your code and make it easier to read/maintain. Just a simple suggestion.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if (intNumbersArray[0] > intNumbersArray[1])
{
  Swap(intNumbersArray[0],intNumbersArray[1])           
}
if (intNumbersArray[1] > intNumbersArray[2])
{
  Swap(intNumbersArray[1],intNumbersArray[2])           
}
if (intNumbersArray[2] > intNumbersArray[3])
{
  Swap(intNumbersArray[2], intNumbersArray[3]);
}
if (intNumbersArray[3] > intNumbersArray[4])
{
  Swap(intNumbersArray[3], intNumbersArray[4]);
}
if (intNumbersArray[1] > intNumbersArray[4])
{
 Swap(intNumbersArray[1],intNumbersArray[4])           
}


Can be changed to something (like)...
1
2
3
4
5
for(int i = 0; i < ARRAY_MAX - 1; ++i)
{
   if (intNumbersArray[i] > intNumbersArray[i+1])
     Swap(intNumbersArray[i], intNumbersArray[i+1]);
}
Thank you for the suggestion. I will try to add that once I can get the program to compile. What is the proper way to write the SortIntegers function? I don't understand why what I am doing is incorrect.

Thanks!
Topic archived. No new replies allowed.