The following code is suppose to: 1 - ask the user to enter a message to be transmitted…
- User
The following code is suppose to: 1 - ask the user to enter a message to be transmitted over the network;
2 - print the message;
3 - fragment the message so that each character represent a packet to be transmitted, remembering that every package must be associated with an identification number;
4 - for each packet, generate a random integer number between 1 and 10, which will determine the number of times the packet must be replicated;
5 - generate a new sequence of packets in which both the packets of the original message and the replicated packets are scrambled;
6 - print the new packet sequence;
7 - build the binary search tree from the new sequence of packages;
8 - print the tree;
9 - reconstruct the original message from the binary search tree.
10 - print the message; Here is the code. Please, fix it: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>
typedef struct node { int id; char letter; struct node* left; struct node* right; } Node;
Node* createNode(int id, char letter) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->id = id; newNode->letter = letter; newNode->left = NULL; newNode->right = NULL; return newNode; }
Node* insert(Node* root, int id, char letter) { if (root == NULL) { return createNode(id, letter); } else { if (id < root->id) { root->left = insert(root->left, id, letter); } else if (id > root->id) { root->right = insert(root->right, id, letter); } } return root; }
void inOrderTraversal(Node* root) { if (root != NULL) { inOrderTraversal(root->left); printf(“%c”, root->letter); inOrderTraversal(root->right); } }
void shuffle(int array, int size) { srand(time(NULL)); for (int i = size - 1; i > 0; i–) { int j = rand() % (i + 1); int temp = array[i]; array[i] = array[j]; array[j] = temp; } }
int main() { char message[256]; printf(“Enter a message to be transmitted: “); fgets(message, 256, stdin); printf(”\nMessage: %s\n”, message);
int n = strlen(message) - 1; int packets[10 * n]; int packet_index = 0; for (int i = 0; i < n; i++) { int replicates = rand() % 10 + 1; for (int j = 0; j < replicates; j++) { packets[packet_index++] = i; } } shuffle(packets, packet_index); printf(“New sequence of packets: “); for (int i = 0; i < packet_index; i++) { printf(”(%d, %c) “, packets[i], message[packets[i]]); } printf(”\n”); Node root = NULL; for (int i = 0; i < packet_index; i++) { root = insert(root, packets[i], message[packets[i]]); } printf(“Reconstructed message: “); inOrderTraversal(root); printf(”\n”); return 0; - Assistant
- User
Please, fix the issue pointed out in bullet point 6.
- Assistant