C++ :: Function Does Not Update Class Variables In Recursive Call
Dec 14, 2014
I'm trying to implement Tarjan's Strongly Connected Components Algorithm in C++. Here's how I gotten so far:
void tarjan(vector<Vertex>& G){
index = 0;
while (!S.empty()) S.pop();
[Code]....
Here's an example graph for the algorithm to run: [URL]
Here's the first part of the output of the program: [URL]
all the index & lowlink values of the nodes are set to -1 in the beginning. global index value is set to 0. My problem is, the algorithm starts running on Vertex X-4. It runs the instruction X-4.index=0 & X-4.lowlink=0 then it calls itself with the paramater of node X-1. it sets X-1's index & lowlink values to 1. then it calls itself for X2. then it checks whether node X-4 has the index value <0 or not. Even though we set the value of X-4 to 0 in the first run, it still sees X-4.index as -1.
Task: To create a recursive function to sort elements in an array of integers.
The function must start the sorting from the first element, and the recursion calls must go on until the last element in the array is sorted. In each step of recursion, the function must work only with the subset of array elements that have not been sorted yet.
Problem: I am getting a 'Segmentation fault: 11' in the recursive call to the function (please, see the code below).
Environment: Mac, OS X = Mavericks
Code: //////////////////////////////////////////////////////////////////////////////// ////////// Recursively sorting an array of ints. ////////// Arguments: array, array size, index from where to start sorting. void sort_incr_array_int_recursive(int a[], int size, int i){ int tmp, idx_small, small = a[i];
// Locating the smaller element in the array section.
I just wondering if a base class can call the overridden function from a Derived class?
Here's an example:
//Base Class H class BaseClass { public: BaseClass(); virtual ~BaseClass(); virtual void functionA();
[Code] ....
So basically, when I am creating a new object of Derived class, it will initialize BaseClass and the BaseClass will call functionA but I want it to call the function overridden by Derived class.
I know that if I call newObj->functionA it will call the overridden function. Right now I want the base class to call the overridden function "this->functionA(); in BaseClass" during its initialization. Is it possible to do that?
Basically, I have a base class called MainShop and it has 3 derived classes which are SwordShop, SpellBookShop and BowShop. I want the base class to be able to call a function from one of the derived classes but no matter what i do, it doesn't seem to work!
I have two other derived classes, but its basically the same concept. I have a function in one of the derived classes and i would like to call it from the base class. This is one my derived classes:
I'm having this pain in the ass error (No matching function for call to Data::Data() ) on this block of code:
Etapa::Etapa(string nm, float dist, string loc, Data dt) { nome = nm; distancia = dist; local = loc; data = dt; }
[Code].....
I guess this is happening because I'm trying to give Etapa an argument of Data type and I can't do it or put it in a variable since it has 3 parameteres (mes, dia, ano).
You need to write a permute class that will take first and second strings to rearrange letters in first, followed by second. For example, if the first is “CAT” string and second is “MAN” string, then the program would print the strings TACMAN, ATCMAN, CTAMAN, TCAMAN, ACTMAN, and CATMAN. The first and second strings can be any length of string or a null.
The permute class uses a Note class as link list note to link all letters arrangement. The permute class has Note pointers, firstNote and lastNote, to point to the beginning and ending Notes of the link list as private data members. There are three other private data members (total, firstString and secondString) to store the total possible number of arrangements and strings pass into the class.
Write a driver to test the permute class to pass in any two strings of any sizes.
Other than mention in the following, you can add more classes, functions, and private data members to this program.
Note class
The Note class needs to have two private data members and a constructor. The two private data members are data and p pointer. The data’s data type is string and p pointer is Note. The Note class constructor has two parameters, one is string and the other is Note pointer.
Permute class
The Permute class has five private data members (*firstNote, *lastNote, total, firstString and secondString). The firstNote and lastNote pointers are point to Note. The total has integer data type. The firstString and secondString have string data type.
There should have at least three public member functions, Permute, permutation and print. The Permute function is the constructor which takes strings to initialize the private data members. The permutation function does the recursive call to arrange the strings and setup the link list. The print function will print out the private data member information.
Driver file
The driver file should declare a Permute eight elements pointer array. Instantiate eight Permute object with the following eight set of data and assign the object to the pointer array. Use a repetition to call the object’s print function to print out the private data member information. If the total of the permute private data member is less than 100 then print out the permutated letters four in a row, otherwise print out 9 in a row.
first = "", second="", first = "", second ="CATMAN", first = "C", second ="ATMAN", first = "CA", second ="TMAN", first = "CAT", second ="MAN", first = "CATM", second ="AN", first = "CATMA", second ="N", first 1 = "CATMAN", second ="";
You need to write a permute class that will take first and second strings to rearrange letters in first, followed by second. For example, if the first is “CAT” string and second is “MAN” string, then the program would print the strings TACMAN, ATCMAN, CTAMAN, TCAMAN, ACTMAN, and CATMAN. The first and second strings can be any length of string or a null.
The permute class uses a Note class as link list note to link all letters arrangement. The permute class has Note pointers, firstNote and lastNote, to point to the beginning and ending Notes of the link list as private data members. There are three other private data members (total, firstString and secondString) to store the total possible number of arrangements and strings pass into the class.
Write a driver to test the permute class to pass in any two strings of any sizes.
Other than mention in the following, you can add more classes, functions, and private data members to this program.
Note class:The Note class needs to have two private data members and a constructor. The two private data members are data and p pointer. The data’s data type is string and p pointer is Note. The Note class constructor has two parameters, one is string and the other is Note pointer.
Permute class:The Permute class has five private data members (*firstNote, *lastNote, total, firstString and secondString). The firstNote and lastNote pointers are point to Note. The total has integer data type. The firstString and secondString have string data type.
There should have at least three public member functions, Permute, permutation and print. The Permute function is the constructor which takes strings to initialize the private data members. The permutation function does the recursive call to arrange the strings and setup the link list. The print function will print out the private data member information.
Driver file:The driver file should declare a Permute eight elements pointer array. Instantiate eight Permute object with the following eight set of data and assign the object to the pointer array. Use a repetition to call the object’s print function to print out the private data member information. If the total of the permute private data member is less than 100 then print out the permutated letters four in a row, otherwise print out 9 in a row.
It's my understanding that there are three ways to go about updating a control on a form from a class.
A) Instance the form, eg: Form1 Frm = new Form1(); The problems I have run into with this is that while writing text to a richtextbox using the instance Frm, the richtextbox never updates on the form.
B) Passing the object to the class. This method seems less used and can be very problematic. Haven't looked into it thoroughly.
C) Exposing the objects using classes within the Form. While making methods public they still do now show as available for use when calling from classes, unless I instance them but then I run into problem A. I've considered making the methods static so no instancing is required but when doing so the object(in this case richtextbox) becomes invalid due to itself being instanced by the automatically generated code when adding the object.
I know there is another way to pass values then force the object to update but that won't necessarily work with a richtextbox as you have the option to deal with color and font variations.
I imagine I'm missing something pretty simple here but I can't find any resources online about how to handle objects with more complex data than just straight text.
Example of problem A
//called from a class Form1 Chat = new Form1(); Chat.BeginChat(); Chat.AddChat(Color.Lime, "Testing"); Chat.EndChat();
I created class called students which suppose to store students names of in array but when I call the display function it display only the first name. but I want it to display names depending on the array size.
#include <iostream> #include <sstream> using namespace std; const int SIZE=5;
How do I call a class method which is defined in a .hxx file separately to a .cpp file? Is it any different from how we normally do it (using the scope resolution operator after the class name and then the method name with parameters) ?
The recursive function is bolded, i got feedback and was told that the static variable made the function seem a lot like a iterative function but he did not say why.
Code: #define MAX 100 #include <string.h> #include <stdio.h> int checkPalindrome(char string[MAX]); int checkRecPalindrome(char string[MAX]);
the functions checks if the word is a palindrome like"level" "madam" etc. but with input "dfdfdfdffdfd" my recursive function fails.
Code:
/* main.c - Created on: Nov 9, 2013 - Author: Kaj P. Madsen*/ #define MAX 100 #include <string.h> #include <stdio.h> int checkPalindrome(char checkString[MAX]); int checkRecPalindrome(char checkString[MAX], int strLgt, int a); }
[code]....
results from "dfdfdfdffdfd" added some print to see that its the variables a and strLgt not functioning properly
Code:
dfdfdfdffdfd. The word is not a palindrome(iterative) strLgt: 11 a: 0 a: d strLgt: dstrLgt: 10 a: 1 a: f strLgt: fstrLgt: 9 a: 2 a: d strLgt: dstrLgt: 8 a: 3 a: f strLgt: fstrLgt: 7 a: 4