• Breaking News

    Monday, November 22, 2021

    Why is my C++ program stuck in an infinite loop? Ask Programming

    Why is my C++ program stuck in an infinite loop? Ask Programming


    Why is my C++ program stuck in an infinite loop?

    Posted: 21 Nov 2021 07:47 PM PST

    I am losing a node and not sure what to modify to correct it:

    main.cpp

    #include <fstream> #include <iostream> #include <cmath> #include <time.h> #include <stack> #include <queue> #include "AVLTree.h" using namespace std; int main() { AVLTree* tree1Root = new AVLTree(50, nullptr); srand(time(NULL)); uint32_t numNodes = 10; for (uint32_t i=1; i < numNodes; i++ ) { tree1Root = tree1Root->insert(( rand() % 10000)); //Uncomment to help debug lost nodes if (tree1Root->countNodes() != i+1) { std::cout<<"Lost node "<<std::endl; return 1; } //uncomment to help debug unbalanced trees // tree1Root->updateHeight(); // if ( ! tree1Root->isBalanced() ) { // std::cout<<"Tree1Root balanced: FAILED at node insertion "<<i<<std::endl; // return 1; // } } if (tree1Root->countNodes() == numNodes) { std::cout<<"tree1Root lost Nodes: PASSED"<<std::endl; } else { std::cout<<"tree1Root lost Nodes: FAILED expected: 100 actual: "<<tree1Root->countNodes()<<std::endl; } tree1Root->updateHeight(); float expectedHeight = log2(numNodes) * 1.5; if (tree1Root->getHeight() < expectedHeight) { std::cout<<"tree1Root height: PASSED"<<std::endl; } else { std::cout<<"tree1Root height: FAILED expected: <" <<expectedHeight<<" actual: "<<tree1Root->getHeight()<<std::endl; } if ( tree1Root->isBalanced()) { std::cout<<"Tree1Root is balanced: PASSED"<<std::endl; } else { std::cout<<"Tree1Root is balanced: FAILED"<<std::endl; } } 

    AVLTree.cpp

    #include "AVLTree.h" #include <cmath> #include <iostream> using namespace std; //************** already implemented helper functions AVLTree::AVLTree(int t_data, AVLTree* t_parent, AVLTree* t_left, AVLTree* t_right) { data = t_data; height = 0; parent = t_parent; left = t_left; right = t_right; } bool AVLTree::isLeaf() { //insert code here return ((left == nullptr) and (right == nullptr)); } uint32_t AVLTree::getHeight() { return height; } //****************************************************** int AVLTree::getBalance() { int i = (left == nullptr ? 0 : left->height) - (right == nullptr ? 0 : right->height); int j = abs(i); return j; } AVLTree* AVLTree::rotateRight() { AVLTree* u = left; left = u->right; u->right = this; return u; } AVLTree* AVLTree::rotateLeft() { AVLTree* u = right; right = u->left; u->left = this; return u; } AVLTree* AVLTree::rebalance() { if (isLeaf()) { return this; } if (left != nullptr) { left = left-> rebalance(); } if (right != nullptr) { right = right-> rebalance(); } int isBal = isBalanced(); if (!isBal) { // Rebalance this (me) if ((left == nullptr ? 0 : left -> getHeight()) < (right == nullptr ? 0 : right-> getHeight())) { if (right-> left != nullptr) { right = right-> rotateRight(); AVLTree *t = rotateLeft(); return t; } else if(right-> right != nullptr) { AVLTree *t = rotateLeft(); return t; } } else if ((left == nullptr ? 0 : left -> getHeight()) > (right == nullptr ? 0 : right -> getHeight())) { if (left-> right != nullptr) { left = left -> rotateLeft(); AVLTree *t = rotateRight(); return t; } else if(left-> left != nullptr) { AVLTree *t = rotateRight(); return t; } } } return this; } AVLTree* AVLTree::insert(int new_data) { AVLTree *root = this; if (new_data < data) { if (this->left != nullptr) { left = left-> insert(new_data); } else { this->left = new AVLTree(new_data, this); } } else if (new_data > data) { if (this->right != nullptr) { right = right-> insert(new_data); } else { this-> right = new AVLTree(new_data, this); } } root-> updateHeight(); bool isBal = isBalanced(); if (isBal == false) { AVLTree *rtn = rebalance(); rtn-> updateHeight(); return rtn; } return root; } //*************************** //Do not edit code below here uint32_t AVLTree::countNodes() { //insert code here if (isLeaf()) { return 1; } if (left != nullptr) { if (right != nullptr) { return 1 + left->countNodes() + right->countNodes(); } return 1+ left->countNodes(); } return 1 + right->countNodes(); } void AVLTree::updateHeight() { //insert code here if (isLeaf()) { height = 0; return; } if (left != nullptr) { left->updateHeight(); if (right != nullptr) { right->updateHeight(); height = (1 + max(left->getHeight(), right->getHeight())); return; } height = 1 + left->getHeight(); return; } right->updateHeight(); height = 1 + right->getHeight(); return; } bool AVLTree::isBalanced() { if ( isLeaf() ) { return true; } if (left == nullptr) { return ( right->getHeight() < 1 ); } if (right == nullptr) { return ( left->getHeight() < 1 ); } return ( left->isBalanced() and right->isBalanced() and abs(getBalance() < 2) ); } 

    AVLTree.h

    #include <iostream> class AVLTree { public: int data; uint32_t height; AVLTree* parent; AVLTree* left; AVLTree* right; //base functions defined for you AVLTree(int data, AVLTree* parent=nullptr, AVLTree* left=nullptr, AVLTree* right=nullptr); bool isLeaf(); uint32_t getHeight(); //******************* //functions you need to define //insert a node and rebalance tree to maintain AVL balanced tree //return new root of tree AVLTree* insert(int data); //computes a node's balance factor by subtracting the right subtree height from the left subtree height. int getBalance(); //checks for imbalance and rebalances if neccessary //return new root of tree AVLTree* rebalance(); //implement a right rotate //return new root of tree AVLTree* rotateRight(); //implement a left rotate //return new root of tree AVLTree* rotateLeft(); //Do not edit these three functions bool isBalanced(); uint32_t countNodes(); void updateHeight(); }; 
    submitted by /u/constantintervals
    [link] [comments]

    What is the best interpereter/IDE for python basic programming?

    Posted: 22 Nov 2021 12:30 AM PST

    What is the best IDE out there? It should be easy to use. Not as easy as online editors though.

    submitted by /u/dumdaramastmast
    [link] [comments]

    C# or C++ first?

    Posted: 21 Nov 2021 07:43 PM PST

    I have done somewhat in C# , but im forwading to learn C++ too. Which should do first?

    submitted by /u/Ok_Assumption_5852
    [link] [comments]

    (Python) How could I deal with very large lists?

    Posted: 21 Nov 2021 09:48 PM PST

    Hi,

    I'm trying to create a smartphone app in Python. As part of the app, I need to use 9 very large lists. Of the exact size I am not sure, but they probably each exceed a million items. Currently I've just hard coded the lists in, but testing the app on my desktop reveals that it takes about 20 minutes for it to boot because of these huge lists. So I'm trying to find an alternative to hard-coding them.

    The elements of the 9 lists are lists themselves, and the elements of these lists are strings. I need to read the data from one of the 9 lists at a time, occasionally two of the 9 lists at a time. The strings are dictionary keys to a hard-coded dictionary (the dictionary is much smaller though, only 300 items or so), so I might need to sift through them.

    Is there a more efficient way to store the lists and reduce boot time? Ideally the storage option wouldn't take up tons of space if it's local because I don't want the app to be too big, and if possible, I'd rather it didn't require an internet connection to access.

    PS: I'm not a developer by stretch of imagination, I just know how to use Python. But that means that I'm not familiar at all with what probably would be taught in a standard CS course, such as using databases and data structures. Hence why I'm asking here, hopefully someone more knowledgeable in such matters would be able to provide an appropriate solution.

    submitted by /u/deathmyself
    [link] [comments]

    JSON utils alternative?

    Posted: 22 Nov 2021 01:26 AM PST

    There used to be a site http://www.jsonutils.com/ which has disappeared of late, which was very useful - you could, amongst other things, submit some JSON and it'd create classes (C#, or other langauges) into which the data could be deserialised and back (using Newtonsoft or the like).

    Just wondering if anyone knows what happened to that site - has it moved to a different domian? - or of an alternative that does the same thing...?

    submitted by /u/ISayISayISay
    [link] [comments]

    zoom in/out while recording in recordrtc

    Posted: 22 Nov 2021 12:00 AM PST

    what solution can you suggest if I want to add zoom in/out functionality while recording using recordrtc?

    submitted by /u/budolbudol3
    [link] [comments]

    How do i make this app?

    Posted: 21 Nov 2021 10:49 PM PST

    so i wanted to make this android app which reads id from rfid tag and use this id to search from a particular website and display that info ! any tips? should i use flutter or react?? how do i do this?

    submitted by /u/trashbug21
    [link] [comments]

    What would it take to make a clone of this app?

    Posted: 21 Nov 2021 10:33 PM PST

    Hello, I am a new programmer and am looking for a project to help me learn.

    I would link to know what programming languages/skills would be required to make a clone of the app called untappd?

    Is it even feasibly possible for someone new to programming to build something like this?

    App dev website.

    submitted by /u/SovereignDeFi
    [link] [comments]

    What database should i use with node

    Posted: 21 Nov 2021 09:34 PM PST

    First of all, I have used node and mongo it's good but I found out too late that it's not good fit for crm application. I do love node tho, it's really awesome. I am looking for a good SQL solution with Node.

    From what I found out from some web search is that PostgreSQL is good choice when it comes to relational database with node.

    I have used sql in past and I'm really good with query language. I just need someone to suggest me a good stack along with node.

    And I've also been hearing about something called sequelize and knex. I'm not entirely sure what they are but i think it's client to connect with PostgreSQL.

    Please suggest me something that i should be working with.

    submitted by /u/Aiabiacia
    [link] [comments]

    Is learning AI and Machine Learning a good choice??

    Posted: 21 Nov 2021 07:47 PM PST

    Been thinking in this programming niche as I see the world gets more automatic from finance sector to household things is it a good choice to learn that??

    submitted by /u/SoooSus
    [link] [comments]

    Opening google without seeing it on screen

    Posted: 21 Nov 2021 07:24 PM PST

    So I want to open google and then a website.After that I want some data from the website but I want all of this to happen in background or without anything happening on the screen.

    submitted by /u/Striking-Courage-182
    [link] [comments]

    No comments:

    Post a Comment