Solved

C++ Fill Function

Posted on 2011-03-01
12
929 Views
Last Modified: 2012-05-11
Could somebody PLEASE tell me what I'm doing wrong?  I want the fill function to fill my array with a quantity of numbers (maxListSize)... it compiles and runs, but it doesn't add ANY numbers into the array.

Here it is:

#include <iostream>
#include <ctime>

#include "arrayListType.h"

using namespace std;

void printListInfo(arrayListType<int>& list);
void fill(arrayListType<int>& listInfo);

int main()
{
      arrayListType<int> intList;
      clock_t startTime, endTime;

      arrayListType<int> num;

      cout << "Enter numbers ending with -999" << endl;

      fill(num);

      printListInfo(intList);

      startTime = clock();
      intList.quickSort();
      endTime = clock();

      printListInfo(intList);

      cout << endl;
      cout<<"Running time: " << endTime - startTime <<endl;

      cout << endl;
      system("pause");
      return 0;
}

void printListInfo(arrayListType<int>& list)
{
      cout << "List Size: " << list.listSize() << endl;
      cout << "Max List Size: " << list.maxListSize() << endl;
}

void fill(arrayListType<int>& listInfo)
{
      int seed = 47;

      int multiplier = 2743;

      int addOn =      5923;

      int num = -999;

      while(listInfo.listSize() < listInfo.maxListSize())
      {
         listInfo.insertEnd(seed);
         seed = int(seed * multiplier + addOn);
      }

      if(listInfo.listSize() == listInfo.maxListSize())

            num = -999;

      cout<<endl;
}
0
Comment
Question by:Member_2_4213139
  • 7
  • 4
12 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 35009629
The implementation of arrayListType would be useful.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35009916
My guess is that listInfo.listSize() starts too high. Shouldn't you clear the list first or something?
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35009930
I would put in some debugging lines
cerr<<"Size is "<<listInfo.listSize()<<". Max is "<<listInfo.maxListSize()<<endl;
      while(listInfo.listSize() < listInfo.maxListSize())
      {
         listInfo.insertEnd(seed); 
cerr<<"Size is "<<listInfo.listSize()<<". Max is "<<listInfo.maxListSize()<<". Added "<<seed<<endl;
         seed = int(seed * multiplier + addOn);
      }

Open in new window

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:Member_2_4213139
ID: 35009965
Which part?  Which function?  The entire template???
0
 

Author Comment

by:Member_2_4213139
ID: 35009980
Tommy:  THAT could be my problem... let me clear it and try it again...
0
 

Author Comment

by:Member_2_4213139
ID: 35010131
The array is clearing at the end of the run... there just HAS to be something wrong with the fill function.
0
 

Author Comment

by:Member_2_4213139
ID: 35010303
Tommy:  Using your lines, the fill function is now generating the 50000 numbers, but it's not adding them to the array (or counting them as having been added) ... suggestions?

void fill(arrayListType<int>& listInfo)
{
      int seed = 47;

      int multiplier = 2743;

      int addOn =      5923;

      int num = -999;

      cerr<<"Size is "<<listInfo.listSize()<<". Max is "<<listInfo.maxListSize()<<endl;

      while(listInfo.listSize() < listInfo.maxListSize())
      {
         listInfo.insertEnd(seed);

      cerr<<"Size is "<<listInfo.listSize()<<". Max is "<<listInfo.maxListSize()<<". Added "<<seed<<endl;
         seed = int(seed * multiplier + addOn);
      }

      if(listInfo.listSize() == listInfo.maxListSize())

            num = -999;

      cout<<endl;
}
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35010387
What exactly did it output?
We'll need to see at least the insertEnd function definition.
0
 

Author Comment

by:Member_2_4213139
ID: 35010547
The last line (after generating 50000 is:

"Size is 50000. Max is 50000. Added 865598868."

Here is the insertEnd function:

template <class elemType>
void arrayListType<elemType>::insertEnd(const elemType& insertItem)
{

    if (length >= maxSize)  //the list is full
        cerr << "Cannot insert in a full list" << endl;
    else
    {
         list[length] = insertItem;   //insert the item at the end
         length++;   //increment the length
    }
} //end insertEnd

0
 

Author Comment

by:Member_2_4213139
ID: 35010948
Correction:  This is what the screen shows:

Size is 50000. Max is 50000. Added 865598868.

List Size: 0
Max List Size: 50000
list Size: 0
Max List Size: 50000

Running Time: 1
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 35011011
Wait. In your main you do
      fill(num);
      printListInfo(intList);

So you filled the list num then you display the list intList which has not been filled. Is this the problem? Try doing fill(intList).
0
 

Author Closing Comment

by:Member_2_4213139
ID: 35011246
BRILLIANT!  Thank you, Tommy!!!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question