Solved

C++ Fill Function

Posted on 2011-03-01
12
937 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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.
Make the most of your online learning experience.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

695 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