mikeregas 22 Newbie Poster

I got all the errors fixed but now it is crashing. it crashes when the program reads the dictionary file

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


//CONSTANTS 
#define wrdlen 48 
#define linelen 1024


// A struct representing a node in a binary search tree

struct BSTnode
{  
char word[wrdlen];// The contents of the node	
struct BSTnode* left;// Links to the node's left and right children
struct BSTnode* right;

};

// Adds a new node to the tree. Duplicates are disallowed. Returns 1 if a
// new node was added, returns 0 if newdata was already in the tree
int insert(struct BSTnode** root, char newword[wrdlen])
{
    // If we've reached the right place to insert, create a new node and add it in
	if( (*root) == NULL)
	{
		(*root) = (struct BSTnode*)malloc(sizeof(struct BSTnode));
        strcpy((*root)->word,newword);
		(*root)->left = NULL;
		(*root)->right = NULL;
		return 1;
	}
    // Otherwise, search for the correct place to insert

	if(strcmp(newword,(*root)->word)<0)
	{
		return insert( &((*root)->left), newword);
	}

	else if(strcmp(newword,(*root)->word)>0)
	{
		return insert( &((*root)->right), newword);
	}
// If the new data is neither less than nor greater than the the data at
// the current node, it must be equal, and duplicates are not allowed
	else
	return 0;
}

// Returns 1 if target is in the tree and 0 otherwise
int search(struct BSTnode* root, char target[wrdlen])
{
    // An empty tree contains nothing, much less target
	if(root == NULL)
		return 0;
	// If the current node is what we're looking for, we've found it 

	if(strcmp(root->word,target) == 0)
		return …
mikeregas 22 Newbie Poster

Here is the new code I have got it down to just three errors. yea! I can still use some help with it though

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


//CONSTANTS 
#define wrdlen 48 
#define linelen 1024


// A struct representing a node in a binary search tree

struct BSTnode
{  
char word[wrdlen];// The contents of the node	
struct BSTnode* left;// Links to the node's left and right children
struct BSTnode* right;
};

// Adds a new node to the tree. Duplicates are disallowed. Returns 1 if a
// new node was added, returns 0 if newdata was already in the tree
int insert(struct BSTnode** root, char newword[wrdlen])
{
    // If we've reached the right place to insert, create a new node and add it in
	if( (*root) == NULL)
	{
		(*root) = (struct BSTnode*)malloc(sizeof(struct BSTnode));
        strcpy((*root)->word,newword);
		(*root)->left = NULL;
		(*root)->right = NULL;
		return 1;
	}
    // Otherwise, search for the correct place to insert

	if(strcmp(newword,(*root)->word)<0)
	{
		return insert( &((*root)->left), newword);
	}

	else if(strcmp(newword,(*root)->word)>0)
	{
		return insert( &((*root)->right), newword);
	}
// If the new data is neither less than nor greater than the the data at
// the current node, it must be equal, and duplicates are not allowed
	else
	return 0;
}

// Returns 1 if target is in the tree and 0 otherwise
int search(struct BSTnode* root, char target[wrdlen])
{
    // An empty tree contains nothing, much less target
	if(root == NULL)
		return 0;
	// If the current node is what we're looking for, we've found it …
mikeregas 22 Newbie Poster

This program is suppose to be a spell checking program and I just can not get it to work can someone please take a look and let me know what I need to do to fix it

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


//CONSTANTS 
#define wrdlen [48] 
#define linelen [1024]


// A struct representing a node in a binary search tree

struct BSTnode
{  
char word[wrdlen];// The contents of the node	
struct BSTnode* left;// Links to the node's left and right children
struct BSTnode* right;
};

// Adds a new node to the tree. Duplicates are disallowed. Returns 1 if a
// new node was added, returns 0 if newdata was already in the tree
int insert(struct BSTnode** root, char newword[wrdlen])
{
    // If we've reached the right place to insert, create a new node and add it in
	if( (*root) == NULL)
	{
		(*root) = (struct BSTnode*)malloc(sizeof(struct BSTnode));
        strcpy((*root)->word,newword);
		(*root)->left = NULL;
		(*root)->right = NULL;
		return 1;
	}
    // Otherwise, search for the correct place to insert

	if(strcmp(newword,(*root)->word)<0)
	{
		return insert( &((*root)->left), newword);
	}

	else if(strcmp(newword,(*root)->word)>0)
	{
		return insert( &((*root)->right), newword);
	}
// If the new data is neither less than nor greater than the the data at
// the current node, it must be equal, and duplicates are not allowed
	else
	return 0;
}

// Returns 1 if target is in the tree and 0 otherwise
int search(struct BSTnode* root, char target[wrdlen])
{
    // An empty tree contains nothing, much less target
	if(root == NULL)
		return 0; …
mikeregas 22 Newbie Poster

I found a recursive code in c++, but I do need it in C, if there is anyone out there that is willing to convert it that would be great. thanks

#include <iostream>  
      
    using namespace std;  
      
    bool recursivePalindrome(std::string str, unsigned int index = 0) {  
        if (index > str.length()/2) return true;  
        if (str[index] == str[str.length()-index-1])  
            return recursivePalindrome(str, ++index);  
        else return false;  
   }  
     
   int main () {  
       cout << recursivePalindrome("yehey");  
       cout << recursivePalindrome("w00t");  
     
       cin.get();  
       return EXIT_SUCCESS;  
   }
mikeregas 22 Newbie Poster

I need this to been done recursively, it works but I was told that it is not recursive does anyone have any ideas or suggestions

#include <stdio.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

main () 
{
	char decision;
	char str[30];
	int isPalindrome = TRUE;
	int choice = TRUE;
	int i,j;


	
	printf("Enter a word: ");
	gets(str);
	j = strlen(str) - 1;
	i = 0;


	while(i <= j && isPalindrome)
		{

			if(str[i] != str[j]) 
			{
				isPalindrome = FALSE;
			}
				i++;
				j--;
		}

	if(isPalindrome)
		{
			printf("%s is a palindrome!\n", str);
		}
	else
		{
			printf("%s is not a palindrome\n", str);
		}

	printf("Would you like to enter another word? y or n \n");
	scanf("%c", &decision);
	if (decision == 'y') choice = TRUE;
	else choice = FALSE;
	}
}
mikeregas 22 Newbie Poster

man I have been struggling with this recursion thing for days now and I have not gotten it yet, but the way that you put it was the easiest explination that I have recieved. How you would make this program a recursive one?

mikeregas 22 Newbie Poster

my question here is two fold I am new to recursion and I am not going to lie. I dont get it at all, well at least the code part. is this considered recursive? and the second question is how do I make a loop that would let the user select y to enter another palindrome or n to quit. I know the laast part is easy I have been trying a do while loop but to no avail.

#include <stdio.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

main () 
{

	char str[30];
	int isPalindrome = TRUE;
	int i,j;


	printf("Enter a word: ");
	gets(str);
	j = strlen(str) - 1;
	i = 0;


	while(i <= j && isPalindrome)
		{

			if(str[i] != str[j]) 
			{
				isPalindrome = FALSE;
			}
				i++;
				j--;
		}

	if(isPalindrome)
		{
			printf("%s is a palindrome!\n", str);
		}
	else
		{
			printf("%s is not a palindrome\n", str);
		}

}
mikeregas 22 Newbie Poster

I really do appreciatte you walking me through this, now that I have implemented all your suggestions it is return the sq root of 4.000 is 1.000, do you have any ideas. I think that it is in the math part: root = (low + high)/2. I think that it needs to be this formula written recursively in code

mikeregas 22 Newbie Poster

what is your suggestion to solving this, I am lost completely and the more that I try to understand it the more confused that I get. I dont understand what you mean by my lack of {} if you could please elaborate on this I would really appreciate it.
When I enter a number to get the square root of it returns that number not the sq root

mikeregas 22 Newbie Poster

I took the commit out


mid = (high + low) / 2;

//x(n+1) = 1/2( x(n) + a / x(n) )// I know this is the formula that makes this work I just dont know how to impliment it

mikeregas 22 Newbie Poster

here is the code that I have so far and I can not seem to get it to work properly it is return 1 for any number that is entered. Any help that you can give me would be greatly appreciated. Thanks

#include <stdio.h>



double square_root(double low, double high);

int main(void)
{
    float number;
    double root;
    
    printf("Enter a number to compute the square root of.\n");
    scanf("%f", &number);
    
    root = square_root(1, number);
    
    printf("The square root of %.3f is about %.3lf\n", number, root);
    
    system("PAUSE");
    return 0;
}

	double square_root(double low, double high)
{
    float mid;
    
    mid = (high + low) / 2;
	//x(n+1) = 1/2( x(n) + a / x(n) )

    //Base Case
    if((mid*mid) == high)
        return mid;

     //Stops when the high and low numbers are within 0.001 of one another.   
    if((high - low) < 0.001)
        return mid;
    
    if((mid*mid) > high)
        //return square_root(low, mid);
        
    return square_root(mid, high);
}
Salem commented: Congrats on using code tags correctly on your first post +22