i want to write a c++ program to build min heap which gets above values from user. remember this program should not alloduplicate values to enter. it should discard duplicate values.
I've been working on a homework assignment that randomly generates integers and populates them into an array (array1). The program is then supposed to:
1.) copy those values to a second empty array (array2)
2.) sort the values already in array1 (using an inline function)
3.) enqueue the unsorted integers from array2 into a heap vector
4.) a third empty array (array3) is supposed to be populated with those unsorted integers (by dequeuing them from the heap), sorted in reverse order.
But no matter what I do, I always get garbage values like these:
I've tried using both a standard random number generator:
array1[i] = rand()%100+1;
And the d_random.h file my instructor gave us, but nothing works.
Here's the code from all 3 files:
HeapTester.cpp
Code: #include <iostream> // Provides cin, cout #include <cstdlib> // Provides EXIT_SUCCESS, rand, srand #include "d_random.h"//Provides random number generator #include "Heap.h" using namespace std; // Use C++ Standard namespace //Elements in each array. const int arrayLength = 15;//100;
Problem is i dont know n so the solution must be recursive. The possible splits are 2^(n-1)-1. I am really stuck and most of the code is complete for cart and i really don't want to restrict it to binary values.
Well I've given myself a lil project and I can't seem to figure out why the numbers keep repeating. What is the cause of this repetition. the function that generate the numbers is below
Code:
void numGen(struct Ticket *pick){ int x; int y; int check = 0;
So it will generate numbers again and again as the loop goes on but it always repeat some numbers. My question is, how would you generate numbers without repeating? Somebody told me that i have to use auto increment, but i really have no idea about that.
QuoteWrite a program that reads in ten numbers and displays distinct numbers. If a number appears multiple times, it is displayed only once.For example: if user enters 1,1,2,3,4,4,5,1,0,9. You should output: 1,2,3,4,5,0,9. Order doesn't matter.
Steps:
a. Create an int[] to hold all the integers of user input and another int[] to store the distinct numbers.
b. Make a nested for-loops which the outer loop goes though the first array and inner loop to check if the value is already inside. Store the value to the second array if new number,otherwise do nothing.
c. Make a for loop to print out the elements of the second array
I am having a issues with an assignment in my class and don't really understand why. I am getting undeclared identifier errors even though I have declared and I am also getting an error. Here is the code:
Last time I came to you all with an error it was a simple brain fart on my part but I don't think this one is like that. I would love to tell you what the program is supposed to do but I still do not really know, which might be part of the problem. I guess it outputs different sized rectangles...
I have been given an assignment to make a code to read some text nd display all the words nd the number of times they appear in another file or as output without displaying the repeating words. I made the code but its not giving any output.
So I'm going through and trying to do a recursive implementation of a Heap. I keep getting access violations for some reason on my sifts (_siftUp) - even though I'm trying to insert into sub[0] (currSize = 0 in the constructor). I don't think either of my sifts are implemented correctly, are they?
If I have an array, and want to make a heap tree out of it using make heap and sort heap, how would I do it? I'm struggling because I didn't take any course in data structure.
I tried googling stuff and I got to the following:
I know that memory addresses in the stack can contain either values or references to other memory addresses, but do these memory addresses also contain methods or are the methods themselves located in the heap?
The confusion comes from the fact that in C# a delegate variable can be assigned either a method's identifier, an inline function, a lambda expression, or a new instance of the delegate type with the method's identifier passed as an argument to the constructor. My guess is that assigning the method's identifier directly to the delegate variable is just a simplified way of calling the delegate type's constructor with the method's identifier as an argument to the parameter, something that the compiler handles for you.
But even in this last case, the delegate variable is said to point toward the method itself. In that case, does it mean that methods are stored in the heap, just as reference type values are?
I am creating a class that has a private array on the heap with a constructor that takes the size of the array and initializes it on the heap. Later I have to make a deconstructor delete the space and print out free after.In my code, I was able to heap a private array and make a deconstructor, but I don't know how to take the size of the array and initialize it on the heap. My guess is this:
int* size = new int();
Also when you initialize size on the heap, don't you also have to delete it too? If so, where, in the code, do you do that? Here is my code so far.
Class Student { private: int size; int* array = new int[size]; public: Student(); // Constructor ~Student(); // Deconstructor
[code]....
How do you make a constructor that takes the size of the array and initializes it on the heap
I recently posted a question related to creating a heap template class. The ultimate goal is to create a series of classes that serve a purpose that was overlooked in the Qt library that I need for my current project.
The current "end goal" is a PriorityQueue template that uses a comparer class which is inherited from a "template interface". Basically a pure virtual class template. Perhaps that is the mistake to begin with but hopefully not. (Is this a valid approach?)The problem I am getting is that when I compile the code, it says my derived comparer class is abstract.
I will include all related classes here. I doubt it is relevant but the templates and the classes based off them are in different namespaces.Here is the comparer "template interface":
// in global namespace template<class T> class IIMQOBJECTS_EXPORT IQComparer { virtual int compare(T& a, T& b) = 0; virtual bool equals(T& a, T& b) = 0; virtual bool isGreaterThan(T& a, T& b) = 0; virtual bool isLessThan(T& a, T& b) = 0; };
Here is the class that is supposed to be non-abstract but isn't recognized as such:
// in the application namespace AND IN SAME project that has the NetEventInfo class // all functions ARE defined/implemented in a cpp file class APPCORE_EXPORT NetEventInfoComparer : ::IQComparer<NetEventInfo*> { public: NetEventInfoComparer(); ~NetEventInfoComparer();
I used a heap viewer to check for memory leaks. I have many of them and its hard to find out where it is not being freed. Is their a way to use the debugger to log the addresses of the data it allocated on the heap. This way I can trace it back. Or is their any other way to fix memory leaks properly.
I've got a VERY experimental function which takes data stored to a file and assigns it to a multidimensional array on the heap. It's designed for infinite dimensions by recalling itself with updated information but I don't think this is very safe.
The template function creates a heap array using a TYPE**, and recalls itself to create the new dimensions. I want to replace this with the much safer method of assigning just a single heap memory array and then only assign using the recalling method (unless I can find anything else).
To do this though I need to know how single dimensional arrays are stored on the heap, as well as multi-dimensional (for n dimensions). Where I can find this information?
btw I only need this for the Windows operating system, 32bit, I'm not exactly sure what 'C++ style' this is but I'm using Microsoft's Visual Studio Express 2012 as my IDE, so whatever that uses.
I discovered valgrind and started using it for my c code. But I get following error message at almost every malloc position, :
==19505== 40 errors in context 10 of 12: ==19505== Use of uninitialised value of size 8 ==19505== at 0x10000416E: my_method (main.c:662) ==19505== by 0x10000159E: main (main.c:182) ==19505== Uninitialised value was created by a heap allocation ==19505== at 0x47F1: malloc (vg_replace_malloc.c:302) ==19505== by 0x100001C21: my_method (main.c:333) ==19505== by 0x10000159E: main (main.c:182)
and I really don't understand what it means. I already googled it but I didn't find out what is my mistake.SO here i just put one example:
Code:
int main(int argc, char** argv) {
//i declare my variables at this position Uint *used, *forbidden_jumps, *forbidden_jumpsV, *forbidden_jump;
/*now i want to allocate one of them, this is my line 333 from the error message*/
//a_num is set during the execution of the program, ALLOC(used, Uint, a_num); }
[code].....
Is there any support page for the output of valgrind? I found it on the homepage.
This assignment is about Heap and PQ's to sort out jobs inside a printer. I'm far from finishing the assignment but the most important part isn't working. My issue is that nothing is getting stored inside the array. I keep getting crashes and at this point I'm not sure what to do. I notice that my destructor runs right after my "addJob" Function finishes, which is destroying the memory. Which might be why nothing gets stored inside OR I think my implementation of Heap/PQ is wrong.
Functions inside my test.cpp aren't properly done, they are made just to see if something is stored inside.
1. Check if I created the array correctly [PQtype.cpp / Heap.h/ PQType.h] 2. Am I even using/storing into the array. [Test.cpp "addJob" Function] 3. I'm also new to working with Class Templates.
I developed the following heap sort algorithm code, and for some reason anytime it goes above 4100 entries, the algorithm completely crashes. It works perfectly up until that point but I can't see why it would crash?
void heap_from_root(MVector &v, int i, int n) { int end=n,j=0; // Identify the lowest root and how many sons it has. If it only has one son, set j=1. if (n==1) { n = 0; j = 1; } else if ((n-2) % 2 == 0) { n = (n-2)/2; } else if ((n-1) % 2 == 0) { n = (n-1)/2; j=1; }
m_vRenderObjects.push_back(new Objects(mOperatorMesh)); /// this is never called ~Objects(void) { OutputDebugStringA("Cleanup Objects "); if (StateMachine != NULL)
I recently upgraded my operating system from Windows XP to Windows 7 SP1 64 bit. We are using Visual Studio 2008 Professional Edition and Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production.
When I try to execute this code I am getting the below exceptions
HTML Code: try { CDatabase *pDatabase = CDatabaseConnection::getDatabaseConnectionProcessLog(); ORSProcessLog rsProcessLog(pDatabase);
[Code] .....
Where rsProcessLog is the CRecordset object using a successfully connected database pointer pDatabase
In 32- bit Debug version I get a message box at rsProcessLog.Close(); with the below text Debug Error
Program: ......Test.exe
HEAP CORRUPTION DETECTED: after Normal block (#506) at 0x0087F628. CRT detected that the application wrote to memory after end of heap buffer.
Memory allocated at f:ddvctoolsvc7libsshipatlmfcsrcmfcdbcore.cpp(2626)
(Please Retry to debug the application)
In 32- bit Release version I get a message box at rsProcessLog.Close(); with the below text Windows has triggered a breakpoint in Test.exe
This may be due to a corruption of the heap, which indicates a bug in Test.exe or any of the DLLS it has loaded.
This may also be due to the user pressing F12 while Test.exe has focus.
The output window may have more diagnostic information.
The above code was a working code in Windows XP with the rest of the env remaining the same and it continues to run in Windows XP but not in Windows 7.