Solved

C++ Fill Function

Posted on 2011-03-01
12
935 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 88
C++ help/ Toy problem 19 69
Programming Language for Wordpress 7 86
MS Access VBA How To Properly Set Up A multipart/related HTTP Request 13 141
A short article about a problem I had getting the GPS LocationListener working.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

734 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