Solved

C++ Fill Function

Posted on 2011-03-01
12
928 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now