only works if I include "&", but not suppose to.

Here is my beautiful WORKING code:
This is a tester, sort of...

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <cstdlib>
#include <iostream>
#include "DynamicList.h"
#include <time.h>


using namespace std;

//creates a list of random numbers.
void createRandomList (DynamicList &aList, int size);
//sorts a list in increasing or decreasing order.
void sortList(DynamicList &aList, bool decrease);
//does the inner loop for the sortList function.
bool sort( DynamicList &aList, bool decrease);
//prints a list to the screen.
void printList (DynamicList &aList);
//counts how many times a numbers occurs in a list.
int getCount (DynamicList &aList, int element);
//creates a list that contains the number of times a number occurred for another list.
void makeCountList (DynamicList &aList, DynamicList &bList, DynamicList &cList);
//prints out two corresponding lists.
void printsTwoLists (DynamicList &aList, DynamicList &bList);
//Creates a second list from a first, which only contains every number once.
void shortenList(DynamicList &aList, DynamicList &bList);


//Sorts a list of numbers in decreasing or increasing order.
void sortList(DynamicList &aList, bool decrease)
{
     
     int go = 0;
     bool switched = true;
     while (switched)
     {
           switched = false;
           go = go + 1;
           //calls on the function that does the inner loop for sorting.
           switched = sort( aList, decrease);     
     }
                               
}

//Does the inner loop for sorting a list of numbers in decreasing or increasing order.
bool sort( DynamicList &aList, bool decrease)
{
     int go = 0;
     int temp;
     bool switched = false;

     for (int i = 0; i < (aList.getSize()-1) - go; i++)
           {
           //If 1 is chosen for the Boolean value decrease, the list will order 
           //in decreasing order. So if a number is greater than the next, they 
           //will switch. If 0 is chosen the list will order in increasing
           //order. So if a number is less than the next, they will switch.
           if ((decrease && aList.get(i) > aList.get(i + 1)) || 
                                (!decrease && aList.get(i) < aList.get(i + 1)) )
               {
                           //temp holds the value of a number on the list.
                           temp = aList.get(i);
                           //Then that value is replaced by the number of the next.
                           aList.set(i, aList.get(i+1));
                           //Then the second number is replaced by the first
                           //that was being kept by temp.
                           aList.set(i+1, temp);
                           switched = true; 
               }     
           } 
           return switched;    
                                  
}

//Prints out the data of a list.
void printList (DynamicList &aList)
{
    for (int i = 0; i < aList.getSize(); i++)
         {
             //every passage of the loop prints to the screen the 
             //number that's in the list at that passage.
             cout << aList.get(i) << endl;
         }
    cout << "" << endl;
}

//Uses indexOf and removeEntry to count how many times a numbers occurs in a list.
int getCount (DynamicList &aList, int element)
{
    int count = 0;
    
    while(aList.indexOf(element) != -1)//While there is still a number in the list.
    { 
      aList.removeEntry(element);//remove that number.
      count = count + 1; //and add one to count.
    }
    return count;
}

//Creates a counts list, a list corresponding with another 
//that contains how many times the same number occurred in it.
void makeCountList (DynamicList &aList, DynamicList &bList, DynamicList &cList)
{   
     for(int i = 0; i < aList.getSize(); i++)
         { 
                 //Uses the function getCount to see how 
                 //many times a number occurred in a list,
                 int count = getCount(bList, aList.get(i));
                 //Then adds that number to a different list to keep 
                 //track of the count of the first list.
                 cList.add(count);
         } 
}

//Prints out two corresponding lists with them staying alongside each other.
void printsTwoLists (DynamicList &aList, DynamicList &bList)
{
    for (int i = 0; i < aList.getSize(); i++)
         {
             //Every passage of the loop prints out the number both lists contains.
             cout << aList.get(i) << " appears " << bList.get(i) << " times." <<endl;
         }
    cout << "" << endl;
}

//Creates a second list that only contains each 
//one of the numbers from the first list one time.
void shortenList(DynamicList &aList, DynamicList &bList)
{
          //loops through all of the numbers in the first list.
          for (int i = 0; i < (aList.getSize()); i++)
         {
              //If the second list does not already contain the number 
              //the first list has on this passage of the loop,
              if(!bList.contains(aList.get(i)))
              {
                   //the second list adds the number to its list.                            
                   bList.add(aList.get(i));
              }
         }

}


//Creates a list that contains random numbers.
void createRandomList (DynamicList &aList, int size)
{
     srand(time(NULL));
     for(int i = 0; i < size; i++)
     {
        //every passage of the loop adds a random number to a list.
        //Until the list is the size indicated by size.     
        aList.add(rand());
     }
    
}


int main()
{
    
   
  DynamicList baseList, countList, newBaseList;
  bool decrease;
  int base;
  
    
  cout << "Chose a number from 1,000 to 10,000." << endl;
  cout << "> ";
  cin >> base;//a number chosen by the user that determines 
              //how long the list of random number will be.
  
  //creates a list called baseList that contains random numbers.
  //it's size depending on what ever base is chosen to be.
  createRandomList (baseList, base);

  cout << "The random numbers:" << endl;
  //prints the original list of random numbers. 
  printList (baseList);
   
  //sorts the base list in increasing order.
  sortList(baseList, 1);
   
   cout << "The random numbers in increasing order is:" << endl;
  //prints the baseList to the screan.
  printList (baseList);
   
   cout << "The lowest random number is:" << endl;
  //outputs the lowest random number in the base list.
  cout << baseList.get(0) << endl;
  cout << endl;
  
  //sorts the baseList in decreasing order.
  sortList(baseList, 0);
   
   cout << "The highest random number is:" << endl;
  //outputs the highest random number in the base list.
  cout << baseList.get(0) << endl;
  cout << endl;
  
  //checks that only one of each random number is in the list and creates 
  //a second list called newBaseList that contains those numbers.
  shortenList(baseList, newBaseList);  
   
  //creates a count list that contains how many 
  //times a number in newBaseList occurred.  
  makeCountList (newBaseList, baseList, countList);  
  
  cout << "The count of the random number is:" << endl;
  //prints out the newBaseList with the countList to 
  //show how many times a number did occur.
  printsTwoLists (newBaseList, countList);
    
    
  system("PAUSE");
  return EXIT_SUCCESS;
  
  
  
}


The problem is that my teacher says if he sees one more "&" on my Hwk, he's going to stop reading and just give me a zero. The thing is, the only reason I started to use them in the first place is b/c I can't get the code to work w/o them. If I just take away one of them and run, the screen comes up along with a message that says my project has stopped working.

What am I obviously missing? I haven't had time to visit the concepts from my past C++ course, and I'm thinking it's something simple that's gonna cause me to smack myself when someone points it out here. I just really could use some fresh eyes on this please?

Let me know if you need me to include my header and cpp file that goes along with this.

And thanks so so much guys, it's to late for this Hwk, but I want to get this worked out before my next is assigned.
Where exactly are these "&" that he is complaining about?

If he is complaining about you passing your lists by reference, then he is retarded and shouldn't be teaching the class. (IMNSHO)

The only thing I would complain about is that none of those functions are members of your class.
That's may be exactly what your teacher meant. To create a class instead of using references.

Here is the "reference" way of doing things:
1
2
3
4
5
6
7
struct S {
  int x;
};

void operation(S& s) {
  s.x = 5;
}

and here is the OO (object oriented) way of doing things:
1
2
3
4
5
6
7
8
9
10
class S {
public:
  void operation();
private:
  int x;
};

void S::operation() {
  x = 5;
}


Regards
edit: ^ what he said ^

Unless you were specifically instructed not to (according to the specs) most if not all of those functions should be public member functions of your DynamicList class. Then they could manipulate their object directly without the argument (DynamicList &aList). The functions which manipulate multiple DynamicList objects, should also be member functions and call for a single DynamicList object. Functions which call a second object but dont modify it could use const references.
Last edited on
Okay, I wish this thing allowed pictures so that could show the exact Hwk. I'm gonna type it exactly how he wrote it:

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
1. Specify the class DynamicList

2. Implement the class DynamicList

3. Write a tester class for the DynamicList. Call it DynamicListTest.

4. Write a program that reads a positive integer from the user, call it base, from 1,000...10,000 and produces a list of 
base random numbers greater or equal to 1. The program then produces the following report on that list, after the list 
is printed on the screen in increasing order:
a. lowest random.
b. highest random.
c. number of repetitions in the list of random numbers.

For the generation of random numbers use the function rand after including the library <cstdlib>. Invoking this func-
tion will generate a random number in the range 0 RAND_MAX, whose value is 32767.

What to submit:
1. Hard copy of DynamicList.h, DynamicList.cpp, DynamicListTest.cpp, and a hard copy of the
solution to 4.
2. Hard copy of the execution of 4.
3. Submit to blackboard a zip files with all the files in 1. (DynamicList.h, DynamicList.cpp, DynamicListTest.h,  
DynamicListTest.cpp, and a hard copy of the solution to 4.

Points:120
Due date: February 26, 2011.


So, I did everything and the code I showed before was basically part 4. I did like someone here suggested and just made the functions part of my DynamicList class, and the code works fine. No more "&"'s yea!....BUT do you see how my teacher keeps talking about part 4 in the assignment? Like it's not suppose to be a part of the rest of my files? Thats what lead me to believed I should do it the way I did. Any comments, thoughts, or suggestions?
It's hard to say what exactly your teacher intended. It is also hard to say what he has against ampersands. Does he hate them when used for references (, because it is not OO,) or when used for taking the address of object (, because he thinks working with pointers is unsafe.)

For what it's worth, you could make the DynamicListTest class implement all test-related operations. This class will have as fields all lists that you create in main. The constructor takes a single argument and creates randomly generated sorted list and then creates the other lists. Then you can implement a printResults method in DynamicListTest or something like that.

So, for example, main reads from the user the number "base". Then main creates DynamicListTest object and passes the value "base" to its constructor as argument. Then main request from DynamicListTest to print the results or smth. Alternatively, there can be many methods for printing different sections from the results.

You see, the primary reason that you have amps in the code is that main creates something and then passes it around. With the above scheme I try to make so, that all data that a method needs is inside the object to which the method belongs. The test needs the lists in main, so we put them in the DynamicListTest object. This way main does not have to pass them to the methods. I'm not saying this is good practice, but just trying to follow the nearly impossible requirements.

Also, didn't you use the address-of operator (which is also an amp) in the implementation of your dynamic list. You can not avoid it there.

Use rand() + 1, not rand(), because supposedly the items must be >= 1.

Regards

EDIT: Small clarification. If you have some operations that you don't want (conceptually) as methods in the DynamicListTest class, but you still need them for the implementation and you want them to refer to the lists inside DynamicListClass, you can make put them in the private section. They will not be used from the outside, and in a sense they wont exist for any purpose, but to serve in implementing some other method.
Last edited on
Topic archived. No new replies allowed.