Functions and Arrays

I keep getting an error message and i have no idea how to solve it. Here's my code and the error.

program3.cpp:51:1: error: reference to overloaded function could not be resolved; did you mean to call it?
endl--;
^~~~


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>
#include<iomanip>
#include<fstream>
#include<string>
#include<ostream>

static const int size = 100;
static const int row = 5;
int swapholder = -1;
int length = 100;
int end = 100;


using namespace std;



int main ()
{
    int array1[size];    // array of  100 rows and 5 columns


    fstream textfile;
    textfile.open("arrayfill.txt");         // my text file
    textfile << "writing this to a file.\n";
    textfile.close();


    for (int i =0; i< size; i++){
          array1[i]= rand()% 100;      // for random values 0-99
}

     for (int counter = length -1; counter > 0; counter--){


          for (int index =0; index < 10; index++){

               if (array1[index] > array1[index+1]){


                  swapholder = array1[index+1];
                  array1[index+1] = array1[index];              
                  array1[index] = swapholder;

  }
endl--;
}


    for (int index =0; index < 100; index++){


cout << array1[index] << ",";
}
cout << endl;

}



     int k = 1;
     for (int i =0; i < size; i++, k++){

     cout << array1[i] << " ";

     if (k == row ) {
     cout << endl;

     k =0;
   }
}


return 0;
}

On line 46.

endl--;

Tell me in words what you are trying to do, because this is something you can't do.
Last edited on
I meant to put end-- but the reason i put it is stop after every pass through the loop reduce the number so that it can put them in order. This is the new error-
program3.cpp:51:1: error: reference to 'end' is ambiguous
end--;
^
program3.cpp:16:5: note: candidate found by name lookup is 'end'
int end = 100;
^

Last edited on
This is exactly why one shouldnt use using namespace std; but rather explicity state for example std::cout There is a function in the std namespace called end, so when you have using namespace std, c++ doesn't know whether you are using the std::end or your integer end.

You should probably stop using the namespace, if you for some reason insist on using it, then change the variable name.

Also you shouldnt be using global variables, they're bad news. You can read about both these things on the googles.

Google why using namespace std is bad and why not to use global variables.
Well i'm a beginner and using namespace std: i the only way we've been shown to code.
Yeh they teach that at schools/universities, but the sooner you get out of that habbit the better.
OK so i changed the variable name and it complied but my out wrong. It put it in order but u have like 4 times the numbers I should have.



7,49,58,30,72,44,73,23,9,40,78,65,92,42,87,3,27,29,40,12,3,69,9,57,60,33,99,78,16,35,97,26,12,67,10,33,79,49,79,21,67,72,93,36,85,45,28,91,94,57,1,53,8,44,68,90,24,96,30,3,22,66,49,24,1,53,77,8,28,33,98,81,35,13,65,14,63,36,25,69,15,94,29,1,17,95,5,4,51,98,88,23,5,82,52,66,16,37,38,44,
7,49,30,58,44,72,23,9,40,73,78,65,92,42,87,3,27,29,40,12,3,69,9,57,60,33,99,78,16,35,97,26,12,67,10,33,79,49,79,21,67,72,93,36,85,45,28,91,94,57,1,53,8,44,68,90,24,96,30,3,22,66,49,24,1,53,77,8,28,33,98,81,35,13,65,14,63,36,25,69,15,94,29,1,17,95,5,4,51,98,88,23,5,82,52,66,16,37,38,44,
7,30,49,44,58,23,9,40,72,73,78,65,92,42,87,3,27,29,40,12,3,69,9,57,60,33,99,78,16,35,97,26,12,67,10,33,79,49,79,21,67,72,93,36,85,45,28,91,94,57,1,53,8,44,68,90,24,96,30,3,22,66,49,24,1,53,77,8,28,33,98,81,35,13,65,14,63,36,25,69,15,94,29,1,17,95,5,4,51,98,88,23,5,82,52,66,16,37,38,44,
7,30,44,49,23,9,40,58,72,73,78,65,92,42,87,3,27,29,40,12,3,69,9,57,60,33,99,78,16,35,97,26,12,67,10,33,79,49,79,21,67,72,93,36,85,45,28,91,94,57,1,53,8,44,68,90,24,96,30,3,22,66,49,24,1,53,77,8,28,33,98,81,35,13,65,14,63,36,25,69,15,94,29,1,17,95,5,4,51,98,88,23,5,82,52,66,16,37,38,44,
7,30,44,23,9,40,49,58,72,73,78,65,92,42,87,3,27,29,40,12,3,69,9,57,60,33,99,78,16,35,97,26,12,67,10,33,79,49,79,21,67,72,93,36,85,45,28,91,94,57,1,53,8,44,68,90,24,96,30,3,22,66,49,24,1,53,77,8,28,33,98,81,35,13,65,14,63,36,25,69,15,94,29,1,17,95,5,4,51,98,88,23,5,82,52,66,16,37,38,44,
7,30,23,9,40,44,49,58,72,73,78,65,92,42,87,3,27,29,40,12,3,69,9,57,60,33,99,78,16,35,97,26,12,67,10,33,79,49,79,21,67,72,93,36,85,45,28,91,94,57,1,53,8,44,68,90,24,96,30,3,22,66,49,24,1,53,77,8,28,33,98,81,35,13,65,14,63,36,25,69,15,94,29,1,17,95,5,4,51,98,88,23,5,82,52,66,16,37,38,44,
7,23,9,30,40,44,49,58,72,73,78,65,92,42,87,3,27,29,40,12,3,69,9,57,60,33,99,78,16,35,97,26,12,67,10,33,79,49,79,21,67,72,93,36,85,45,28,91,94,57,1,53,8,44,68,90,24,96,30,3,22,66,49,24,1,53,77,8,28,33,98,81,35,13,65,14,63,36,25,69,15,94,29,1,17,95,5,4,51,98,88,23,5,82,52,66,16,37,38,44,
7,9,23,30,40,44,49,58,72,73,78,65,92,42,87,3,27,29,40,12,3,69,9,57,60,33,99,78,16,35,97,26,12,67,10,33,79,49,79,21,67,72,93,36,85,45,28,91,94,57,1,53,8,44,68,90,24,96,30,3,22,66,49,24,1,53,77,8,28,33,98,81,35,13,65,14,63,36,25,69,15,94,29,1,17,95,5,4,51,98,88,23,5,82,52,66,16,37,38,44,
7,9,23,30,40,44,49,58,72,73,78,65,92,42,87,3,27,29,40,12,3,69,9,57,60,33,99,78,16,35,97,26,12,67,10,33,79,49,79,21,67,72,93,36,85,45,28,91,94,57,1,53,8,44,68,90,24,96,30,3,22,66,49,24,1,53,77,8,28,33,98,81,35,13,65,14,63,36,25,69,15,94,29,1,17,95,5,4,51,98,88,23,5,82,52,66,16,37,38,44,
7,9,23,30,40,44,49,58,72,73,78,65,92,42,87,3,27,29,40,12,3,69,9,57,60,33,99,78,16,35,97,26,12,67,10,33,79,49,79,21,67,72,93,36,85,45,28,91,94,57,1,53,8,44,68,90,24,96,30,3,22,66,49,24,1,53,77,8,28,33,98,81,35,13,65,14,63,36,25,69,15,94,29,1,17,95,5,4,51,98,88,23,5,82,52,66,16,37,38,44,
7,9,23,30,40,44,49,58,72,73,78,65,92,42,87,3,27,29,40,12,3,69,9,57,60,33,99,78,16,35,97,26,12,67,10,33,79,49,79,21,67,72,93,36,85,45,28,91,94,57,1,53,8,44,68,90,24,96,30,3,22,66,49,24,1,53,77,8,28,33,98,81,35,13,65,14,63,36,25,69,15,94,29,1,17,95,5,4,51,98,88,23,5,82,52,66,16,37,38,44,
7,9,23,30,40,44,49,58,72,73,78,65,92,42,87,3,27,29,40,12,3,69,9,57,60,33,99,78,16,35


7 9 23 30 40
44 49 58 72 73
78 65 92 42 87
3 27 29 40 12
3 69 9 57 60
33 99 78 16 35
97 26 12 67 10
33 79 49 79 21
67 72 93 36 85
45 28 91 94 57
1 53 8 44 68
90 24 96 30 3
22 66 49 24 1
53 77 8 28 33
98 81 35 13 65
14 63 36 25 69
15 94 29 1 17
95 5 4 51 98
88 23 5 82 52
66 16 37 38 44








Well then, you better learn how to debug your code. Debugging is just as important as coding, a tool you'll have to use all the time.

i get that I've been doing this for days now that's why I'm looking for help.
First of all, static const is kind of redundant in C++. Global variables are static by default, so you could've just done
1
2
const int size = 100;
const int row = 5;


Second, like TarikNeaj said, global variables aren't good practice. Constants are OK to be global but not the variables. Try to keep variables local and instead, use the appropriate parameters.

Third, my compiler also complained about end --; because of "using namespace std;". Basically, the compiler is confused whether I meant the variable end (which was created by you) or std::end. I am an university student as well and YES, my first intro programming class taught me to use using namespace std;. I've learned to get away from it though. It's OK to use "using ..." if you know when to use and when not to use it. But if you just make a habit of avoiding it, that works too.

Fourth, if you are using rand (), include <cstdlib> library. Somehow, it looks like you managed to compile your code without that library. But be aware that rand () function is from <cstdlib> and some compilers will complain if you don't include that library. If you are going to use rand (), be sure to provide seed so that way the number will be random EVERY time you run the program <http://www.cplusplus.com/reference/cstdlib/rand/?kw=rand>
1
2
3
4
5
6
7
8
9
10
11
#include <cstdlib>
#include <ctime>

int main ()
{
    int iSecret;

    srand (time(NULL));             // Initialize pseudo random number generator

    iSecret = rand() % 10 + 1;   // Then, call rand ()
}



DEBUGGING:

Lastly, I'd like to help you debug it but there is very little information as to how the program should behave. You gave us the code and you told us the error messages. But, you haven't told us how the program should run. For example, what's the expected output result when you run the program. What is the purpose of the program. What are the specifications?

I will still try to help you with this. But, I will be making some assumptions.

I can tell that at the beginning of the program, you opened a text file and saved a sentence in the file and then immediately closed the file. I created the text file on my computer and made sure that this portion of the code works. Then, you saved random numbers in array1. After line 32, i commented out the rest of the code and included the following debugging line.
1
2
    for ( int iter = 0 ; iter < size ; iter ++ )
        std::cerr << array1 [iter] << std::endl;

As expected, a random number was stored in each element of array1. Those numbers were exactly same each time I ran the program and that's because you did not provide seed for the engine.

I un-commented the next portion of your code, which is Line 32 ~ Line 48. counter variable is initialized to 99 in the for-loop and it runs as long as counter > 0. counter is decremented after each iteration. swapholder is used to temporarily hold a value for swapping purpose. So, is there any particular reason why you initialized swapholder to -1? And why couldn't swapholder be inside the main function? Looks like the purpose of this for-loop is to sort the first 10 elements of array1 from smallest to largest. Am I correct? You also have another for-loop for printing out the result, but that for-loop for printing is INSIDE the for-loop for sorting. Each time the sort for-loop runs, you print the elements. I guess this is your first mistake. Take the print for-loop out of the sort for-loop. Print the sorted elements after sorting is finished. Again, because of Line 36, you are sorting the first 10 elements of the array1.

I un-commented the last portion of your code, which is Line 60 ~ Line 72. The purpose of this portion is to display 5 elements in each row. It seems to work as intended.


EDIT: Like TarikNeaj said, you better learn how to debug your code. But, I've tried to help since you said you spent days trying to fix it. I've been through that experience myself when I was working on school assignments and I felt the same pain and desperation.
Last edited on
This is the final code after I debugged it based on my own assumptions. See if this is what YOU expected. If it is not, you tell us what the program is supposed to do and we will do our best to help.

Program Specifications:
1. Create an array to store 100 elements
2. Store a random number in each slot in the array
3. Sort the first 10 elements from smallest to largest
4. Print all the elements after sorting
5. Print all elements with 5 elements in each row (20 Rows Total).

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <ostream>
#include <cstdlib>
#include <ctime>

const int size = 100;
const int row = 5;

using namespace std;

int main ()
{
    int swapholder;  // This will be used for swapping two variables. No need to initialize this really
    int length = 100;

    int array1[size];    // array of  100 rows and 5 columns

    fstream textfile;
    textfile.open("arrayfill.txt");         // my text file
    textfile << "writing this to a file.\n";
    textfile.close();

    srand ( time( 0 ) );    // time (0) used as seed

    // Store a random number in each element of the array
    for ( int i = 0 ; i < size ; i ++ ){
          array1[i] = rand () % 100;      // for random values 0-99
    }

    // Sort the first 10 elements of the array from smallest to largest
    for ( int iter = 0 ; iter < 10 ; iter ++ ) {
        for (int index = iter + 1 ; index < 10 ; index ++ ){
            if ( array1 [iter] > array1 [index] ){

                swapholder = array1 [iter];
                array1 [iter] = array1 [index];
                array1 [index] = swapholder;
            }
        }
    }

    // Print each element in the array
    for (int index =0; index < 100; index++){
        cout << array1[index] << " ";
    }

    cout << endl << endl;

    int k = 1;

    // Print each element, 5 in each row.
    for (int i = 0 ; i < size ; i ++, k ++ ){

        cout << array1 [i] << " ";

        if ( k == row ) {
            cout << endl;
            k = 0;
        }
    }

    return 0;
}


OUTPUT:
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
6 16 29 37 45 52 54 62 64 89 16 92 57 5 76 70 3 72 55 55 54 94 23 26 70 9 89 93
30 79 24 83 26 27 44 19 10 9 79 47 78 58 55 17 29 79 79 48 7 89 18 18 57 81 1 29
 7 14 68 18 65 13 81 79 82 84 54 4 25 85 11 28 56 41 59 90 63 0 55 48 81 37 30 9
0 10 47 67 56 4 29 76 74 88 27 17 80 27 49 85 36

6 16 29 37 45
52 54 62 64 89
16 92 57 5 76
70 3 72 55 55
54 94 23 26 70
9 89 93 30 79
24 83 26 27 44
19 10 9 79 47
78 58 55 17 29
79 79 48 7 89
18 18 57 81 1
29 7 14 68 18
65 13 81 79 82
84 54 4 25 85
11 28 56 41 59
90 63 0 55 48
81 37 30 90 10
47 67 56 4 29
76 74 88 27 17
80 27 49 85 36

Process returned 0 (0x0)   execution time : 0.265 s
Press any key to continue.



EDIT: Included some comments in the code
Last edited on
As a beginner, I can get some new ideas and clarify my doubts here.
Topic archived. No new replies allowed.