Much apprecited. I was able to get it done.
v/r
Much apprecited. I was able to get it done.
v/r
Below is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
/////////////////////////////////////////////////////////////////////////
/*
// struct Edge
// Represent one edge in the graph. Fields contain
// two vertex numbers and a weight.
*/
struct Edge
{
int vertex_1, vertex_2, weight;
};
////////////////////////////////////////////////////////////////////////
/*
// struct Graph
// Represents a weighted graph. Fields contain the # of vertices,
// edges,an array of edges, along with it's physical size.
*/
struct Graph
{
// V-> Number of vertices, E-> Number of edges
int V, E;
// graph is represented as an array of edges. Since the graph is
// undirected, the edge from vertex_1 to vertex_2 is also edge from vertex_2
// to vertex_1. Both are counted as 1 edge here.
struct Edge* edge;
};
//////////////////////////////////////////////////////////////////////////
/*
// manifestGraph
// Represents a graph with vertices and no edges.
*/
struct Graph* manifestGraph(int V, int E)
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;
graph->E = E;
graph->edge = (struct Edge*) malloc(graph->E * sizeof( struct Edge));
return graph;
};
/////////////////////////////////////////////////////////////////////////
// A structure to represent a subset for union-find
struct subset
{
int parent;
int rank;
};
////////////////////////////////////////////////////////////////////////
// A utility function to find set of an element i
// (uses path compression technique)
int find(struct subset subsets[], int i)
{
// find root and make root as parent of i (path compression)
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}
///////////////////////////////////////////////////////////////////////
// A function that does union of two sets of …
Now I'm running into the issue of multiple definitions in code::blocks, but I think that comes down to how I'm linking the files (I'll figure that out). What do you think of this merge function:
/*
// void merge(int* R, int x, int y)
// Merges the equivalence classes of x and y in R.
*/
void merge(int* R, int x, int y)
{
if(!equivalent(R, x, y))
{
int xLeader = leader(R,x), yLeader = leader(R, y);
R[xLeader] = yLeader;
}
}
I'll definitely include the return false, thank you.
Actually the above is not the right direction. What do you think of this:
/*
// bool equivalent(int* R, int x, int y)
// Returns true if x and y are currently in
// the same equivalence class in equivalence relation R.
*/
bool equivalent(int* R, int x, int y)
{
if (leader(int* R, int x) == leader(int* R, int y)
{
return true;
}
}
Understood. I was looking over the equivalent function, below is what I came up with:
/*
// bool equivalent(int* R, int x, int y)
// Returns true if x and y are currently in
// the same equivalence class in equivalence relation R.
*/
bool equivalent(int* R, int x, int y)
{
bool testOf_x = false, testOf_y = false;
for(int index = 0; index < R[n-1]; index++)
{
if(R[index] == x)
{
testOf_x = true;
}
else if(R[index] == y)
{
testOf_y = true;
}
else if(testOf_x && testOf_y)
{
return true;
}
}
}
Do you believe I can impove on this function draft?
v/r
Understood. I decided to leave out the header file completely, and just focus on a file that only contains the function definitions. I'll use NoMachine to run all three files together, since I'm unable to do that via SOL Cpp site. Below is my current draft of what I came up with. I'm looking for examples on how to do the equivalent funtion, then the merge fuction. I believe the other functions are good to go.
#include "equiv.h"
#include <iostream>
#include <cstdio>
using namespace std;
/*
// int* newER(int n)
// Returns an array of n+1 integers.
*/
int* newER(int n)
{
int *R = new int[n + 1];
for(int index = 1; index <= n; index++)
{
R[index] = 0;
}
return R;
}
/*
// int leader(int* R, int x)
// Returns the leader of x in equivalence relation R.
*/
int leader(int* R, int x)
{
if (R[x] == 0)
{
return x;
}
else
{
return leader(R, R[x])
}
}
/*
// bool equivalent(int* R, int x, int y)
// Returns true if x and y are currently in
// the same equivalence class in equivalence relation R.
*/
bool equivalent(int* R, int x, int y)
{
for(int index = 0; index < "pending" ; index++)
{
if( (R[index] = x) == (R[index] = y))
{
return true;
}
}
return false;
}
/*
// void merge(int* R, int x, int y)
// Merges the equivalence classes of x and y in R.
*/
void …
Let re work the code in my last respone to this:
int* newER(int n)
{
int ER [n + 1];
int *R = ER;
for(int index = 1; index <= (n+1); index++)
{
ER[index] = index;
cout << index << " ________________"<< ER[index] << endl;
}
return R;
}
@AssertNull second response
Yea, I didn't know we could do that, I was going off of the examples in class. Also, should I leave the header alone, and put my definitions in another file. The header is really throwing me off. I see the typedef ER basically means int*
. I coded the following below.
ER newER(int n)
{
int* R = new int[n];
int index;
for(index = 1; index <= (n+1); index++)
{
R[index] = index;
cout << index << " ________________"<< R[index] << endl;
}
return R;
}
I put cout << index << " ________________"<< R[index] << endl;
here so I can track the ouput. The output is correct, based off of my understanding of him wanting an array that was n+1 in length. Currently I'm returning an address of this array. The function definition states it should return an array n+1 of integers. I believe for memory purposes, the address is what we are looking for. Not 100% sure, but giving it a try.
@ AssertNull first response:
Yea, I found out yesterday evening during his class that the header file can contain the function definitions. At first I thought that the header would be standalone (only containing the function prototypes), and that you would put the definitions in another file, but #include(header name) in that file. The functions I provided earlier are the functions he wants us to define. I have to keep the function names the way that he presented.
Or for the first funtion:
int newER(int n)
{
int* R = new int[n];
int temp = 1;
for (int index = 1; index == n; index++)
{
R[index] = (temp);
temp++;
}
return R;
Good afternoon,
I have to create a program to only be used as a tool (no main function). Below is the background information related to task:
Your goal for this assignment is to create a tool that manages an equivalence relation that can be changed by the program. The equivalence relation is always over a set of integers {1, 2, 3, …, n} for some n.
This tool is not a complete program. It is intended to be part of a larger program. It must not have a main function.
For this assignment, an equivalence relation has type ER. A module that uses this tool can create an equivalence relation called e by saying
You will not store the equivalence classes directly. Instead, you will store them implicitly, using the following ideas. You are required to implement an equivalence manager in this way. You will receive no credit for a module …
I was using pencil and paper. I translated what I wrote down to this post. I then used hand simulation afterwards to see if it would work through recursion. The only reason I see that I'm not getting the result needed from that function is if the variable 'steps' isn't carrying over, because that is my base line that the recursion is working towards. I'm trying here, and what I come up with makes sense to me. Yes, if there are certain concepts that I'm unaware of or missing, I understand that what I provide doesn't add up to you all. I'm doing what I can with the information my professor provides and this schedule. I don't believe programming is something can be learned in its entirety in such a short timeframe.
I'm at least trying to work on the issue on my own at first, and then come forward with a question if I feel I'm just missing something. An extra pair of eyes can potentially help highlight errors that may not be obvious to someone.
I tried to provide a background on what the function is suppose to do.
But overall, I appreciate you all's help so far.
v/r
Is the issue with the variable 'steps'?
v/r
Well with the factorial, I just keep ruturning 0. I decided to change my base line to 'steps'.
int initialcomp_Num(int n)
{
//Variable Declaration.
int m = n-1, steps = n;
//Guard.
if (n <= 1)
{
return 1;
}
//Base case.
if (steps > 1)
{
steps--;
}
if (sequence_Length(n) > sequence_Length(m))
{
return initialcomp_Num(n);
}
else if (sequence_Length(n) < sequence_Length(m))
{
return initialcomp_Num(m);
}
}
I keep running into the issue of keeping the camparsion starting value that has the hightest length, texting it, and did still returning it. Since I'm unable to change the variable's value, once it's been declared.
What do you all think about a factorial?
.........................blowing my mind is right. Knocked out my philosophy exam yesterday, 44 out of 50. Was stressing a bit over it all day yesterday. I emailed my professor and apparently there a two functions left from my original program I need to modify.
The one I'm currently working on is the int initialcomp_Num(int n) function. I'll place what my original functioned looked like with loops:
int initialcomp_Num(int n)
{
//Variable Declaration.
int count, initial, topValue, cal;
//Function will work as intended if 'n' is not less than 1.
if (n <= 1)
{
return n;
}
initial = sequence_Length(n);
topValue = n;
for (count = 1; count != n; count++)
{
cal = sequence_Length(count);
if (initial <= cal)
{
topValue = count;
}
else if (initial >= cal)
{
topValue = topValue;
}
}
return topValue;
I'll provide a background on my thought process (round 2) on what I believe I need to do:
Facts
Draft plan
You're example is very clear! Thank you for that.
I was doing a hand simulation. I just picked a random number to start off with.
I'm now doing hand simualtion for that function. I'm currently having an inception moment.
so n = 8
8 <=1, no
max is then (8, 4)
so n = 2
2 <= 1, no
max is then (4, 2) and so on (I believe that once it gets to one, then compares numbers at the point back to the first iteration)
I'm I looking at this correctly? Also, the base case would be in next(int n) right? If the base case was in sequence_Max function of n<=1, then you would only return 1 and not the max.
Wow, I didn't know I could call it like that. My problem (among other things) in regards to this situation, is not knowing what I'm unable to do and what I can do. I can't believe how simple this function coding is.
Not quite understanding. The function next(int n) isn't recursive.
Here is what I came up with:
int sequence_Max(int n)
{
//Variable Declaration.
int high = n, low = next(n);
if (n <= 1)
{
return 1;
}
//Base case.
else if (n != 1)
{
int result = max(low, high);
return sequence_Max(result);
}
}
I'm running into the problem of keeping this going by calling on the same function from within.
Good morning everyone,
The library will be home, 0800 till 2100. I've been working on trying my loops in my hailstone sequence into recursions. It seem recursion can easrier to do, instead of creating loops. I say seem, because now I have come to halt on my progress. The halt is in the form of my sequence_Max function:
/*
//sequence_Max(int n)
//Returns the largest value in the
//hailstone sequence starting at 'n'.
*/
int sequence_Max(int n)
{
//Variable Declaration.
int max = n, temp = n;
while (temp > 1)
{
temp = next(temp);
if(temp > max)
{
max = temp;
}
}
return max;
}
It was a pain in the neck coming up with a loop (I recieved a lot of help from individuals on this site) for it, but now I have to change the loop into a recursion. The only practice that I have up to this point with recursion is in the hailstone_Sequence and sequence_Length functions.
Below is my program thus far (I don't have to moditfy the the next or main functions):
// Tab stops: 4
//The program executes a number of functions to print their
//values with a list of sentences.
#include <iostream>
#include <cstdio>
using namespace std;
//Function Prototype Declaration
int next(int), sequence_Length(int), sequence_Max(int), sequence_Comparison(int),
initialcomp_Num(int);
void hailstone_Sequence(int);
int main (int argc, char** argv)
{
int user_Input;
cout << "What number shall I start with? ";
cin >> user_Input;
cout << user_Input << "\n";
cout << "The hailstone sequence …
Good evening everyone,
Does any one have a reference/videos/tutorials on how to use recursion effecitely in C++?
v/r
LOL understood @JamesCherrill
I figured it out. Also, is there a limit to accuracy of the sequence?
So I gave topValue and initial value of 0. Now the problem seems to be the if statement in the for loop. I have to figure out what to do with the topValue if the initial and cal are equal to one another. The previous function was way easier to deal with.
When entered 9, I got a huge number at the end. I was able to see in noMachine this error:
g++ -Wall -Wshadow -Wuninitialized -O -o hailstone hailstone.cpp
hailstone.cpp: In function ‘int initialcomp_Num(int)’:
hailstone.cpp:163:25: warning: ‘topValue’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int count, initial, topValue, cal;
^
I'm completely lost because it clearly shows topValue = count
@AssertNull I remember you pointing out that when you inputted a certain value, that the last function in my program would give you an incorrect number. I'm not sure what is going on here to the point that occurs for certain number, but not all. It has something to do with the 'topValue' variable. When I used code::blocks...I had no issue. Once I switched over to noMachine, thats when the issue came about. Not sure how the issue was covered up in code::blocks.
Appreciat the advice everyone. I'll keep the information in mind.
The professor doesn't like to see the starting parameters change value in the function (same with the noncomment lines requirement). I'm not sure if this is a real world thing, but thats what he wants. I'm still trying to wrap my head around it. I was under the impression that I could do whatever as well.....but, this limitations does kind of get to me. Have any of you came across a similar situation? What did the situation prepare you for?
Nevermind, I figured out the issue. For one, I need some sleep. Below is the recovery:
int sequence_Max(int n)
{
//Variable Declaration.
int max = n, temp = n;
//temp = n;
while (temp > 1)
{
temp = next(temp);
if(temp > max)
{
max = temp;
}
}
return max;
}
@AssertNull Yes the brackets do not count for non-comment line. When I entered 7, it worked fine. I reworked all of the contacts to explain only what the function does, and not how it does it. I went back to ensure the n value wasn't being changed in all of the functions. If I need n to change, I place it within a different variable
@JamesCherrill I got ride of the else {} from my functions that didn't need it.
Now the biggest issue I'm having is the max function. AssertNull, is there something I messed up here? I couldn't have 'n' = next(n) because it would be changing the parameter, so I just placed it in a different variable, but I can't even get my program to run passed that line now. Code::Blocks isn't showing what the issue is or even a hint to what is wrong.
#include <iostream>
#include <cstdio>
using namespace std;
//Function Prototype Declaration
int next(int), sequence_Length(int), sequence_Max(int), sequence_Comparison(int),
initialcomp_Num(int);
void hailstone_Sequence(int);
int main (int argc, char** argv)
{
int user_Input;
cout << "What number shall I start with? ";
cin >> user_Input;
cout << user_Input << "\n";
cout << "The hailstone sequence starting at " << user_Input << " is: " << endl;
hailstone_Sequence(user_Input);
cout << "\nThe length of the sequence is " << sequence_Length(user_Input);
cout << ".\n";
cout << "The largest number in the sequence is " << sequence_Max(user_Input);
cout << ".\n";
cout << "The longest hailstone sequence starting with a …
Oh wow, definitely a typo! I'll change that. I figured out what was wrong. Here is the code for the function below:
int initialcomp_Num(int n)
{
int count, initial, start;
//Function will work as intended if 'n' is not less than 1.
if (n <= 1)
{
return n;
}
else
{
initial = sequence_Length(n);
for (count = 1; count != n; count++)
{
if (initial < sequence_Length(count))
{
initial = sequence_Length(count);
start = count;
}
}
return start;
}
}
If you feel this isn't easy to read, then could you help with the overall look of the code, for instance, do you believe any programmer could come along and see this would be able to pick up what I'm tryin to accomplish here? I feel I may have made a mess of things, but I can only have 15 at the max noncomment lines for each body of my functions. I con't do more than one loop per function as well. With these requirements, do you feel I can clean this up a bit? I understand readable is a huge part of programming teams.
Not sure what I'm doing wrong, I get this output:
What number shall I start with? 8
The hailstone sequence starting at 8 is:
8 4 2 1
The length of the sequence is 4.
The largest number in the sequence is 8.
The longest hailstone sequence starting with a number up to 8 has length 17.
The longest hailstone sequence starting with a number up to 8 begins with 0. (should be 7 not 8).
Here is the last function, am i missing something?
int initialcomp_Num(int n)
{
int temp1 = 0, count, inital, inital2, start = 0;
//Function will work as intended if 'n' isn't less than 1.
if ( n <= 1)
{
return n;
}
else
{
inital = sequence_Length(n);
for (count = 1; count != n; count++)
{
if (inital < sequence_Length(count))
{
inital = sequence_Length(count);
inital2 = inital;
if (inital2 > sequence_Length(count))
{
start = count;
}
}
}
return start;
}
}
@AssertNull
Currently the sequence_Comparasion function works. I'll see what I can do about the start variable.
Last stretch before code contracts reveiw
Good aftnoon everyone,
I'm currently stuck on the last function needed for this program. Below is the intended function discription:
Takes an integer and returns the start value of the longest hailstone sequence that starts on a value from 1 to n.
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
//Function Prototype Declaration
int next(int), sequence_Length(int), sequence_Max(int), sequence_Comparasion(int),
initialcomp_Num(int);
void hailstone_Sequence(int);
int main (int argc, char** argv)
{
int user_Input, temp = 0;
cout << "What number shall I start with? "; cin >> user_Input; cout << user_Input << "\n";
cout << "The hailstone sequence starting at " << user_Input << " is: " << endl;
hailstone_Sequence(user_Input);
cout << "\nThe length of the sequence is " << sequence_Length(user_Input); cout << ".\n";
cout << "The largest number in the sequence is " << sequence_Max(user_Input); cout << ".\n";
cout << "The longest hailstone sequence starting with a number up to " << user_Input << " has length "
<< sequence_Comparasion(user_Input) << ".\n";
cout << "The longest hailstone sequence starting with a number up to " << user_Input << " begins with "
<< initialcomp_Num(user_Input) << ".\n";
//cout pending
}
/*
// The hailstone sequence computes only if 'n' is greater than 1 from the user. If
// 'n' is even, computes n/2. If n is odd, computes 3n+1. Returns a number
// that follows 'n' in the sequence.
*/
int next(int n)
{
if (n > 1)
{
if ((n % 2) == …
@AssertNull awesome code. Looking at my code compared to yours....I definitely live up to the beginner title. I apologize for the late reply. I pm you about my other classes I was neglecting because I just wanted to program, but I did well to get back on track witht them. I worked on an extra function on Friday, I'll post my updated code in a few. I have one more function to write which I'm kinda stuck on.
@JamesCherrill
Thank you for that review. At the time back when I was working soley on that function, I believe I was looking to have it do more that one thing. For instance, to not just print out the sequence, but to also print out the length of the sequence. I have trimed the fat out of that funcition. It ran successfully with the fat in there previously.
@AssertNull
The max function compares each number within the list of the sequence to determine the max. Currenlty the program works. Here is a link to my code:
If I can improve on the function, I'm open to learn.
Here is my max function. The was pretty tricky, but writing it out helped a lot.
/*
// Sequence_Max(int n)
// Takes an integer 'n' and returns the largest value in the
// hailstone sequence starting at 'n'.
*/
int sequence_Max(int n)
{
int max = 0, temp1 = 2, temp2;
temp2 = next(n);
while (temp1 > 1)
{
if (n > temp2)
{
max = n;
temp2 = next(temp2);
temp1 = temp2;
}
else if (n < temp2)
{
max = temp2;
temp2 = next(temp2);
temp1 = n;
n = max;
}
}
return max;
}
@JamesCherrill
I was able to come up with the following code yesterday afternoon in the library, what do you think?
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
//Function Prototype Declaration
int next(int), sequence_Lenght(int), sequence_Max(int);
void hailstone_Sequence(int);
int main (int argc, char** argv)
{
int user_Input, temp = 0;
cout << "What number would you like to start off with is? "; cin >> user_Input; cout << user_Input << "\n";
cout << "Your hailstone_Sequence starting at " << user_Input << " is: " << endl;
hailstone_Sequence(user_Input);
cout << "\nThe lenght of the hailstone sequence is " << sequence_Lenght(user_Input); cout << ".\n";
cout << "The largest number in the sequence is ";
}
/*
// The hailstone sequence computes only if 'n' is greater than 1 from the user. If
// 'n' is even, computes n/2. If n is odd, computes 3n+1. Returns a number
// that follows 'n' in the sequence.
*/
int next(int n)
{
if (n > 1)
{
if ((n % 2) == 0)
{
n = n / 2;
return n;
}
else
{
n = 3 * n + 1;
return n;
}
}
}
/*
// Hailstone_Seuence(int n)
// will take 'n' and will output the entire
// sequence beginning at 'n'.
*/
void hailstone_Sequence (int n)
{
int value = 0, temp = 0;
cout << n << " ";
while (temp != 1)
{
value = next(n);
temp = value;
cout << temp << " ";
n = temp;
} …
Update Number 2
I decided to scrap everything and start over. I'm going by function to function.
Current Issue is with function hailstone_Sequence(int n)
I'm currently getting a complication error for my output. The program doesn't highlight where the issue is coming from, but with my lack of experience, the issue isn't obvious for me. Below is my current code:
#include <iostream>
#include <cstdio>
using namespace std;
//Function Prototype Declaration
int next(int);
void hailstone_Sequence(int);
int main (int argc, char** argv)
{
int user_Input, temp = 0;
cout << "What number would you like to start off with is? "; cin >> user_Input; cout << user_Input << "\n";
cout << "Your hailstone_Sequence is as follows: " << endl;
cout << hailstone_Sequence(user_Input);
}
/* The hailstone sequence computes only if 'n' is greater than 1 from the user. If
// 'n' is even, computes n/2. If n is odd, computes 3n+1. Returns a number
// that follows 'n' in the sequence.
*/
int next(int n)
{
if (n > 1)
{
while (n != 1)
{
if ((n % 2) == 0)
{
// cout << n << " ";
n = n / 2;
return n;
}
else
{
n = 3 * n + 1;
return n;
}
//cout << n << " ";
}
}
}
/* Hailstone_Seuence(int n)
// will take 'n' and will output the entire
// sequence beginning at 'n'.
*/
void hailstone_Sequence (int n)
{
int count, value;
cout << n << " …
Update
Here is output for the code I provided in my original question:
What number shall I start with? 7
The hailstone of sequence starting at 7 is:
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
The lenght of the sequence is 17.
The largest number in the sequence is: (Problem line)
The longest hailstone sequence starting 7 with a number up to has length 0. (Problem line)
The longest hailstone sequence starting with a number up to 7 begins with 7
I thought I was getting somewhere until I ran into these problem spots. Now I'm really confused on how to generate the desired output. I'm thinking about coding an array, the size of the array will be depended on the calcuations from the hailstone_Sequence (which I would make into a void function). Do you all believe my current reach is plausible?
v/r
@rproffitt
I have spent days getting to where I am with the current code above. I would like to learn how to overcome my current issue without copying a completed program. Appreicate your response though nontheless.
v/r
Good afternoon everyone,
I'm currently working towards to provide the following output for my code:
What number shall i start with? 7
The hailstone sequence starting at 7 is:
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
The length of the sequence is 17.
The longest hailstone sequence starting with a number up to 7 has length 17
The longest hailstone sequence starting with a number up to 7 begins with 7
Below is what I've completed thus far:
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
//Function Variable Declaration.
int lengthof_Sequence(int x), max_Sequence (int m), hailstone_Sequence (int n), count, length;
int main(int argc, char** argv)
{
int user_Input, max_Input;
cout << "What number shall I start with? "; cin >> user_Input; cout << user_Input << endl;
cout << "\nThe length of the sequence is " << hailstone_Sequence (user_Input) << ".";
cout << "\nThe largest number in the sequence is: \n";
cout << "The longest hailstone sequence starting " << user_Input << " with a number up to has length " << count<< "." <<endl;
cout << "The longest hailstone sequence starting with a number up to " << user_Input << " begins with " << user_Input;
return 0;
}
/* The hailstone sequence takes 'n' that is greater than 1 from the user. If
// 'n' is even, computes n/2. If n is odd, computes 3n+1. Both are done till n = …