Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/*This program uses queueing to grab 20 random fortunes from a .txt file Fortune

ID: 3636185 • Letter: #

Question

/*This program uses queueing to grab 20 random fortunes from a .txt file FortuneCookies.txt.
*It also traverses the fortunes both forwards and backwards.
*This program is a recursive binary search tree with hashing.
*For: Jack Cole
*By: Radhika Chhibbar
*Date: December 19, 2011*/

//Inclusive lirbaries
#include<stdio.h>
#include<windows.h>
#include<tchar.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>

//define how many elements are in the message array
#define CHAR_NUM 140
#define pLEFT pLeft
#define pRIGHT pRight

//structure of chars ints and shorts
typedef struct
{
char msg[CHAR_NUM];
short Tx;
short Rx;
char Priority;
int seq_num;
char later[25];
}msg;

//point link to struct node
typedef struct node *link;

//structure of the linking in the .txt file
struct node
{
link pRight, pLeft;
int key;
msg msg;
};

//type the Node as a node type
typedef struct node Node;

//create pROOT as a pointer to Node
Node *pROOT;

//Prototypes
Node* POPULATEtree(Node *pNode);
Node* INtree(Node *pNode, Node *pNext);
Node* SEARCHtree(Node *pNode, int key);
int COUNTtree(Node *pNode);
int Hash( char *v, int M );
int Fortune(Node *ptr);


//Main function
int main()
{
//Variable declarations
Node *pNode;
int got;
int need = 10;
int x=0;
char *letter;
int mod=8;
Node *pROOT;

printf("check 1");

POPULATEtree(pROOT);

//loop until you work through the number of fortunes you need; in this case 20
for(got=0 ; got<need ; ++got)
{
INtree(pNode, pROOT);
Hash(letter, mod);
}

printf("check 2");

//loop until you get to 20 fortunes
for(x=1;x<=need;++x)
{
SEARCHtree(pROOT, Hash(letter, mod));

printf(" %d: ", x); //number the list
printf("%140.140s ", pNode -> msg.msg); //print the first 140 characters in the current fortune
free(pNode); //free the node
}

printf("check 3");

return(0); //end program
}

// Implements a hash function for strings.
// Using 127 as multiplier helps avoid anomalies

int Hash( char *v, int M )
{
int h = 0;
int a = 127;

for ( ;*v != NULL; ++v)
h = (a*h + *v) % M;

printf("check 4");

return(h);
}

int COUNTtree(Node *pNode)
{
int val;

if (!pNode)
{
pNode = pROOT;
}

if (pNode->pLeft)
{
val += COUNTtree(pNode->pLeft);
}

if (pNode->pRight)
{
val += COUNTtree(pNode->pRight);
}

printf("check 5");

return(val);
}

Node* SEARCHtree(Node *pNODE, int key)
{
if(pNODE->key == key)
{
printf("check 6.1");
return(pROOT);
}

if(pNODE->pLeft == 0 && pNODE->pRight == 0)
{
printf("check 6.2");
return(0);
}

if(pNODE->key < key)
{
printf("check 6.3");
return(SEARCHtree(pNODE->pLeft, key));
}

if(pNODE->key > key)
{
printf("check 6.4");
return(SEARCHtree(pNODE->pRight, key));
}
}

Node* INtree(Node *pNode, Node *pNext) //pNode rep node to instert, pNext will initaly point to tree root
{
if((pNode -> key) < (pNext -> key))
{
if(pNext -> pLEFT == NULL)
{
pNext -> pLeft = pNode;

printf("check 7.1");

return(0);
}

else
{
printf("check 7.2");

return(INtree(pNode, pNext -> pLEFT));
}
}



if((pNode -> key)>(pNext -> key))
{
if(pNext -> pRIGHT == NULL)
{
pNext -> pRight = pNode;

printf("check 7.3");

return(0);
}

else
{
printf("check 7.4");

return(INtree(pNode, pNext -> pRight));
}
}
}

//allocate the space in memory you need for each fortune
//if the fortune you're allocating for is NULL then return 0
//if you manage to malloc space then return the pNode
Node* POPULATEtree(Node *pNode)
{
pNode = (Node*) malloc(sizeof(Node));

if (pNode == NULL)
{
return (0);
}

return(pNode);
}

//open and go thru my fortunes file
//do an error check before readng through
//read each fortune in a look
//read char by char
int Fortune()
{
//Variable declarations
FILE *fo;
Node *ptr;
char y;
char t;
long length_fortune;
int i;
char buffer[CHAR_NUM];

//Open file
fo=fopen("FortuneCookies.txt","r");

//Sanity check for if the file isn't found
if (fo == NULL)
{
printf("Stuff is NULL. ");

printf("Hit y to continue. ");
scanf("%c", & y);

if (y == 'y')
{
strcpy(ptr->msg.msg, "Error" );

printf("check 8.1");

return(1);
}
}

else
{
//Seek the end of the file
fseek(fo,(long)0,SEEK_END);
//Tell the program how long the fortune is
length_fortune = ftell(fo);

//Seek the beginning of the fortune you're on
fseek(fo,length_fortune,SEEK_SET);

// scan forward for %% watching for EOF
for ( ; ; )
{
int in;
// read a character
in = fgetc(fo);
if ( in == EOF )
{
printf("END OF FILE! ");
}
// is it a percent sign?
if ( in == '%' )
{
// yes - read next character and check for % sign
in = fgetc(fo);
if ( in == EOF )
{
printf("END OF FILE! ");
}
if ( in == '%' )
{
// yes - break out of loop
break;
}
}
}

// initialize the buffer with nulls
for ( i=0; i < CHAR_NUM; ++i )
ptr->msg.msg[i] = '';

// read a fortune from this spot
for (i=0; ;++i)
{
//string is null terminated when at end of buffer
if(i>= CHAR_NUM)
{
ptr->msg.msg[i]='';
break;
}

// yes - read next character
y = fgetc(fo);

//if you reach the end of the file...
if ( y == EOF )
{
ptr->msg.msg[i]=''; //at the end of the file set the last element of the array to NULL
printf("END OF FILE! "); //sanity print
break;
}

//check if you are at the end of the fortune
if ( y == '%' )
{
// yes - break out of loop
ptr->msg.msg[i]='';
break;
}

else
{
ptr->msg.msg[i] = y; //print the fortune one char at a time

INtree(pROOT, ptr);
}
}

// close the file
fclose(fo);
}

printf("check 8.2");

return(0);
}

Explanation / Answer

So, when u run the program, it gives u the exception: pROOT used without being initialised. Node *pROOT;