I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you.
MY CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char *str;
int freq;
struct node* left;
struct node* right;
};
typedef struct node node;
void insert(node** root, char *s) {
if (*root == NULL) {
*root = (node*) malloc(sizeof(node));
(*root)->str = (char*) malloc(sizeof(char) * (strlen(s) +
1));
strcpy((*root)->str, s);
(*root)->freq = 1;
(*root)->left = NULL;
(*root)->right = NULL;
} else {
int result = strcmp((*root)->str, s);
if (result == 0) {
(*root)->freq += 1;
} else if (result < 0) {
insert(&((*root)->right), s);
} else {
insert(&((*root)->left), s);
}
}
}
void deleteTree(node* root) {
if (root == NULL) return;
deleteTree(root->right);
deleteTree(root->left);
free(root->str);
free(root);
root = NULL;
}
int countNodes(node* root) {
if (root == NULL) return 0;
else return 1 + countNodes(root->left) +
countNodes(root->right);
}
int compare(const void *a, const void *b) {
node* nodeA = (node*) a;
node* nodeB = (node*) b;
if (nodeA->freq != nodeB->freq)
return nodeA->freq - nodeB->freq;
else
return strcmp(nodeA->str, nodeB->str);
}
void fillNodes(node* root, node* nodes[], int *k) {
if (root == NULL) return;
fillNodes(root->left, nodes, k);
nodes[*k] = root;
*k = *k + 1;
fillNodes(root->right, nodes, k);
}
int main(int argc, char** argv) {
if (argc < 2) {
printf("File name required!");
return 1;
}
FILE* fp = fopen(argv[1], "r");
node* root = NULL;
int i;
char s[100];
while (fgets(s, 100, fp)) {
char* formatted = (char*) malloc(sizeof(char) * (strlen(s) +
1));
int j = 0;
int k = 0;
while (s[j] != '\0') {
if (s[j] >= 'a' && s[j] <= 'z') formatted[k++] =
s[j];
else if (s[j] >= 'A' && s[j] <= 'Z') formatted[k++] =
s[j] - 'A' + 'a';
++j;
}
formatted[k++] = '\0';
insert(&root, formatted);
free(formatted);
}
int total_nodes = countNodes(root);
node** nodes = (node**) malloc(sizeof(node*) * total_nodes);
int index = 0;
fillNodes(root, nodes, &index);
qsort(nodes, total_nodes, sizeof(nodes[0]), compare);
for (i = 0; i < total_nodes; ++i) {
printf("%d %s\n", nodes[i]->freq, nodes[i]->str);
}
free(nodes);
deleteTree(root);
fclose(fp);
return 0;
}
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
I am having problems with the following assignment. It is done in the c language. The...
I am stuck on a data structure problem, I am just going off of Geeks for Geeks for help but I need to print 100 random numbers in the range of [1-200]. I can currently only print 5 numbers. Here is my code: #include <stdio.h> #include <stdlib.h> #include <limits.h> using namespace std; //binary tree has data & left and right child struct node{ int data; struct node *left; struct node *right; }; //create a new node struct node* newNode (int...
Hi, I need to make a program in C that reads the type of currency and organizes them into stacks based on currency which can be read back. This is what I have so far, can I get help finishing it? #include <stdio.h> #include <stdlib.h> const float POUND = 1.31; const float YEN = 0.0091; const float RUPEE = 0.014; const float EURO = 1.11; char c; int currValue; float exchangeValue; float finValue; int printValue; struct node { int...
Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...
I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX]; struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL) { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...
Modify the below code to fit the above requirements: struct node { char data; struct node *next; struct node *previous; } *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ; typedef struct node node; int Push(char input) { if(IsFull()==1) { printf("The queue is full. Enter the ‘^’ character to stop.\n"); return -1; } else if (IsFull()==-1) { node *MyNode=(node*)malloc(sizeof(node)); MyNode->data=input; rear->next=MyNode; MyNode->previous=rear; MyPointer=rear=MyNode; return 1; } else { node *MyNode=(node*)malloc(sizeof(node)); node *anchor=(node*)malloc(sizeof(node)); MyNode->data=input; MyPointer=rear=front=MyNode; MyNode->previous=NULL; MyNode->next=NULL; anchor->next=MyNode; return 0; } } char...
For this lab, you will need to write the following functions in table.cpp, and add function prototypes for them to table.h and invoke the functions in main.cpp. * int countNodes(node * root) Recursively count the number of the nodes in the tree. * int sumLeaves(node * root) Recursively sum all of the leaf nodes in the tree. You must use the functions with these exact function prototypes. ****************************************************************************** main.cpp #include "table.h" #include <iostream> using namespace std; int main() { node...
I am getting a seg fault with my huffman tree. I'm not sure if it's my deconstructors but also getting some memory leaks. Can some one help me find my seg fault? It's in c++, thanks! //#ifndef HUFFMAN_HPP //#define HUFFMAN_HPP #include<queue> #include<vector> #include<algorithm> #include<iostream> #include <string> #include <iostream> using namespace std; class Node { public: // constructor with left and right NULL nodes Node(char charTemp, int c) { ch = charTemp; count = c; left...
Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...
I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any help would be appreciated. /**** main.c ****/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #include <pthread.h> #include <string.h> #define WORD_LEN 6 #define TOP 10 char * delim = "\"\'.“”‘’?:;-,—*($%)! \t\n\x0A\r"; struct Word { char word[30]; int freq; }; int threadCount; int fileDescriptor; int fileSize; off_t chunk; struct Word* wordArray; int arrIndex = 0; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;...
Please write the complete code in C. Write a C function that prints the minimum spanning tree of a graph. At the end, print the weight of the spanning tree. A suggested report format is shown in the following example. Source Vertex To Vertex Weight A B 2 A C 4 B D 3 D E 1 Total weight of spanning tree: 10 Your main program will read a graph from DataIn file to an adjacency table before calling the...