C :: Reverse Function That Inserts And Prints Binary Function Correctly

Nov 3, 2013

I have written a function that inserts and prints a binary function correctly.

For example a tree like this [URL] ..... would print like this

Code:
node: 10
node: 7
node: 6
node: 8
node: 9

[Code] ....

Here is my print function

Code:
void prt_tree(struct node *tree) {
if (tree == NULL) {
printf("Null Tree
");
return;

[Code] .....

Could I just make some adjustments to my function to reverse it? and if so, how?

View 2 Replies


ADVERTISEMENT

C++ :: Develop A Function Which Prints A Binary Tree Using Post-order Traversal?

Feb 26, 2013

I am trying to develop a function which prints a binary tree using post-order traversal (left, right, root) but I am experiencing some troubles. The code is compiled but when I run the program it crashes right before printing the post-order traversal.

Below you can find my code and the output from debugging.

Code:
/**
Program which represents a Binary Search Tree and is modified with the following functions:
- smallest() - member function which searches for the smallest element in the tree.
- preorder() - member function which prints the tree nodes using pre-order traversal
- postorder() - member function which prints the tree nodes using post-order traversal (to be completed)
*/
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class TreeNode {
public:

[code]...

This is from the debugger log:

Child process PID: 5720
At C:Program Files (x86)CodeBlocksProjectsTestmain.cpp:316
At C:Program Files (x86)CodeBlocksProjectsTestmain.cpp:317
At C:Program Files (x86)CodeBlocksProjectsTestmain.cpp:318
At C:Program Files (x86)CodeBlocksProjectsTestmain.cpp:319
Program received signal SIGSEGV, Segmentation fault.
At C:Program Files (x86)CodeBlocksProjectsTestmain.cpp:279

View 9 Replies View Related

C :: How To Use Pow Function Correctly

Jan 23, 2015

I'm working on a school assignment that asks us to make code calculating the quadratic formula.

Code:

#include <stdio.h>
#include <math.h>
int main(void) {
double a = 0;
double b = 0;
double c = 0;
double discriminant = 0;
double solution = 0;
double solution2= 0;
}

[code]....

View 4 Replies View Related

C :: Counting Function - Prints All Input Lines That Are Longer Than 80 Characters

Jan 10, 2014

I'm doing an exercise that prints all input lines that are longer than 80 characters. I rather not use any libraries so I decided to write my own function that counts characters to use it in my main program. However when integrate things my function returns zero all the time.

Here is my full code:

/* Exercise 1-17 Write a program to print all input lines that are longer than 80 characters */

#include<stdio.h>
/* Declarations*/
#define MAX_STRING_LEN 1000
int count_characters(char S1[]);
int main() {

[Code] .....

So I was trying to debug my count_characters() function and this is the code if I was to run it seperately:

Code:
#include <stdio.h>
/* counts character of a string*/
main() {
int nc = 0;
int c;
for (nc = 0; (c = getchar()) != '
'; ++nc);
printf("Number of characters = %d
", nc);
}

which works...

View 1 Replies View Related

C :: Reverse A String - Function Is Not Getting Called

Aug 29, 2013

I am new to C and trying to reverse a string but the function is not getting called.. the console says :

Enter a stringabhi
bash: line 1: 229 Segmentation fault (core dumped) ./program

Code:
#include<stdio.h>
#include<math.h>
#include<string.h>
char* reverseString(char input[]);
void main()

[Code] ....

View 6 Replies View Related

C++ :: Value Change After Call Reverse Function

Jul 4, 2013

Which value change after call reverse function.

#include<iostream>
using namespace std;

static void reverse (int len, char *buf) {
char tmp[24];
int i;

[Code] ....

View 10 Replies View Related

C :: Write A Main Function That Parses A Text Document And Prints Out All Of Phone Numbers Found

Apr 21, 2014

You should implement the following function:

int is_phone_number(char* string)
This function will take in a string and return 1 if it looks like a phone number 0 otherwise. A phone number takes the form (xxx)-xxx-xxxx where the xs are digits 0-9. So for example (123)-456-7890 is a valid phone number while 123 456-7890 is not.

You should also write a main function that parses a text document and prints out all of the phone numbers found. Hint, look up the strtok function.

Sample input:
Please call me at (123)-456-789 sometime tonight.

Sample output:
Phone number : (123)-456-7890

View 5 Replies View Related

C++ :: Variadic Function - Parameters Get Passed In Reverse

Nov 17, 2014

I noticed that when using variadic functions, if I pass the va_arg() as parameter to a function, the parameters get passed in reverse. Is that expected?

For example, the following code outputs
Code:
1 2
2 1

Code:
#include <iostream>
#include <stdarg.h>

void foo_func(int v1, int v2)
{
std::cout << v1 << " " << v2 << std::endl;

[Code] .....

View 3 Replies View Related

C :: Reverse Array Of Characters Using Recursive Function?

Mar 15, 2014

Eg. User input : abcde
Program Output : edcba

I keep on getting weird ASCI symbol in return, I couldn't achieve what I need, and tried debugging for days,

Code: char ar[20];
int len,n=0;
printf("enter the string to be reversed :
");

[Code].....

View 5 Replies View Related

C++ :: String Reverse Function - Array Declaration

Jan 23, 2014

Here's my program: - Program which inputs a string from user, reverses it and displays it.

#include <iostream>
using namespace std;
void string_reverse (char [], const int);
int main() {
const int arraysize = 10;
char string[arraysize];

[Code] ....

In the string_reverse function, I have declared temp character type array but on line 38, the

compiler is throwing 3 errors: -

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'temp' : unknown size

I have declared a constant integer arraysize in line 35. Now I have no clue why is this happening because I think as I have declared it as a constant integer variable, this should not happen.

View 5 Replies View Related

C/C++ :: Strrchr Function - Search For A String From The Reverse

Feb 27, 2014

I understand that the strrchr function is supposed to search for a string from the reverse. Unfortunately, mine isn't doing that.

#include <stdio.h>
#include <conio.h>
int main() {
char userinput[100] = "null";
char searchchar;
char *s;
char try_Again;

[Code] ....

View 5 Replies View Related

C++ :: Conversion Of Longitude And Latitude - Reverse Function

Mar 9, 2015

I have code for conversion of longitude and latitude and I need to reverse the function so it will work in opposite direction:

Code:
float LongitudeDeg = argv[1]; // some longitude
float LatitudeDeg = argv[2]; // some latitude
uint32_t u,v,l,n;
l=15; // l range is 2-29

[Code] .....

On start of the code we know longitude,latitude and l. And the result is u and v. And I need function when I will know the u and v, possibly l too, to calculate longitude, latitude.

View 2 Replies View Related

C++ :: Implementing Recursive Function To Print Linked List In Reverse Order

Nov 29, 2014

Implement a recursive function named void printBack(DoublyLinkedNode<T>* node) for the class DoublyLinkedCircularList which will print out the elements in the list from back to front. The function is initially called with the first node in the list. You may not make use of the previous(prev) links

This is my solution where I got 2 out of a possible 3 marks:

template<class T>
void DoublyLinkedCircularList<T> :: printBack(DoublyLinkedNode<T>* node) {
if(node->next == NULL) //Correct- 1 mark
return 0;
else
printBack(node->next); //Correct - 1 mark
cout << current-> element << " ";
}

View 3 Replies View Related

C++ :: Change Making Program Prints Correct Values But Prints Them In Loop

Jan 27, 2015

I have a program that makes change from an amount. It works fine, but the output is the correct output looped over and over. I have tried everything, but it still doesn't work. For example, a amount of 98 should print

3 quarters
2 dimes
0 nickles
3 pennies
but instead it prints
3 quarters
2 dimes
0 nickels
3 pennies
0 quarters
2 dimes
0 nickels
3 pennies
0 quarters
2 dimes
0 nickels
3 pennies

Why it's doing this?

Code:
#include <iostream>
using namespace std;
int coinscount(int& amount, int value) {
int tracker = 0;
int amountdimes = amount;

[Code].....

View 3 Replies View Related

C++ :: Converting Int To String Using Istringstream Inserts Commas

Jan 23, 2014

So I have a following function that converts int to string however it inserts the commas for thousands, millions, and so on. e.g. 65432 -> "65,432".

How do I make the conversion but have a string without commas?

I know it has to with locale but I cannot find it how to prevent it. Also I know about std::stoi() and atoi() but I do not want them.

Code:
template <typename T>
std::string NumberToString ( T Number ) {
std::stringstream ss;
ss << Number;
return ss.str();
}

View 4 Replies View Related

C :: Function Max In Binary Tree

May 28, 2013

I have a problem with the C code . I created two functions, one that runs through the tree inorder, the other that returns the maximum value in the tree. The problem is when I use in the main the method "max", which goes in a loop and not print anything on the screen . If I remove the call to method "max" it works fine. Here's the code:

Code:

#include<stdio.h>
#include<stdlib.h>
#define bool int
/* A binary tree tNode has data, pointer to left child
and a pointer to right child */
struct tNode {

[Code]...

View 13 Replies View Related

C :: Positive Binary Output Function

Mar 27, 2013

I have written a function that takes in a positive decimal and returns its Binary equivalent; however, the output always adds an additional zero to the binary. What could I do to get rid of it?

If the number is 7, it outputs 0111 instead of 111.

Code:
#include <stdio.h>
void Dec(int n) {
if(n > 0)
Dec(n/2);
printf("%i", n%2);

[Code] ....

View 2 Replies View Related

C :: Binary Tree Insert Function

Mar 31, 2014

I am having trouble writing an insert function for a binary tree.

Here is the structure that I have to use.

I have the print function done, the insert function is the one I am having trouble with,

Code:

typedef struct bt_{
int value;
struct bt_ *right;
struct bt_ *left;
}BST;

[Code].....

View 10 Replies View Related

C++ :: Binary Search Delete Function

Nov 26, 2013

I am having an issue when i try to delete a node with 2 children it either doesn't delete anything, or wigs out in various manners deleting the wrong node or replacing a node with a various memory location. As follows, here is the delete function:

void BST::dele(){
bool found = false;//initialize a bool type to "find" the element to be deleted
if(root == NULL) return;//well if the tree's empty, nothing to be found right?
current = root;//set the current to the root to traverse the tree in search of the element
node* parent;//create a parent node for use once the node has been deleted
while(current != NULL){//traverse the tree

[Code] .....

View 1 Replies View Related

C++ :: Function To Convert Decimal To Binary

Jul 11, 2014

What is the difference between the two functions below? I created the function in the top and my friend created the function in the bottom. I just want to know why the function with the while loop prints the binary numbers backwards. And the recursive function prints the binary numbers correctly.

void findBinary(int num) {
int remainder = 0;
while ( num > 0) {
remainder = num % 2;
cout << remainder;
num = num / 2;

[Code] .....

View 3 Replies View Related

C++ :: Binary Function Compile Error

Mar 8, 2013

I wrote a binary function based on existing template:

template <class T> struct percentage : binary_function <T,T,double> {
double operator() (const T& x, const T& y) const
{return x*1.0/(x+y);}
};

I call it in form of percentage<int>() When I compile, it's indicated: error C2143: syntax error : missing ',' before '<' I use VC 2012.

View 5 Replies View Related

C++ :: Program Prints Correct Result But Prints It Over And Over

Feb 9, 2015

I have a program where the user inputs a line of integers, and then all unique ones are outputted. It works fine-almost. It prints the numbers correctly, but prints them more than once and I'm not sure why.

Code: #include <iostream>
using namespace std;
int main ( ) {

[Code]......

View 10 Replies View Related

C :: Search And Delete Function For Binary Trees

Apr 1, 2014

I had an assignment that I completed and it was just inserting values into a binary tree and then displaying it. I was wondering what the code would be if I wanted to delete a number in the binary tree, or if I wanted to search for a number. Here is my code for my assignment.

Code:
#include <stdio.h>
#include<stdlib.h>
typedef struct bt_{
int value;
struct bt_ *right;
struct bt_ *left;

[Code] ....

View 1 Replies View Related

C++ :: Binary Search Tree Remove Function

Feb 14, 2014

Im working on a BST remove function. I think I'm on the right track but I'm not sure. From what I understand there are 3 possible cases. A Node with no children, one child, or 2 children(this being the most complex).

void BST::remove(int x) {
TreeNode *n;
TreeNode *v;
n= root;
while(n != NULL && n->key != x){

[Code] ....

View 4 Replies View Related

C++ :: Binary Search Tree Print Function?

Nov 2, 2014

1) how come root node is printed when the first call to function goes to left subtree?

2) How it resets to root to go to right subtree from last left tree leaf?

void bst:: printNodes(hwareItem*& root){
if(root){
printNodes(root->left);
cout<<root->barcode<<endl;
cout<<root->description<<endl;
cout<<root->price_per_unit<<endl;
cout<<root->stock<<endl;
printNodes(root->right);}
}

View 19 Replies View Related

C/C++ :: Binary Operator Overloading Using Member Function

Dec 26, 2014

I want to create a program that shows the total of 2 subjects & percentage of student using binary operator overloading with member function in C++.

View 10 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved