Solved

C++ Fill Function

Posted on 2011-03-01
12
926 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
Comment Utility
The implementation of arrayListType would be useful.
0
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
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
Comment Utility
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
Comment Utility
Which part?  Which function?  The entire template???
0
 

Author Comment

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

Author Comment

by:Member_2_4213139
Comment Utility
The array is clearing at the end of the run... there just HAS to be something wrong with the fill function.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Member_2_4213139
Comment Utility
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
Comment Utility
What exactly did it output?
We'll need to see at least the insertEnd function definition.
0
 

Author Comment

by:Member_2_4213139
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
BRILLIANT!  Thank you, Tommy!!!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 62
mapBully challenge 6 88
mapAB Challlenge 35 84
sorting efficency of sorting algorithm 30 78
This is about my first experience with programming Arduino.
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

11 Experts available now in Live!

Get 1:1 Help Now