Could someone please help fix the following C code? The program has to be done u
ID: 3813942 • Letter: C
Question
Could someone please help fix the following C code? The program has to be done using two functions (main and read_funct), and the read_funct() prototype is fixed, i.e. MUST BE: read_funct(int *data_num, double **data_vals). The code is meant to draw data from a file, where the first 4 bytes are the number of data points (data_num). The next (8*data_num) bytes contain the data values (data_vals). The program calls on read_funct(), which prints the relevant data out. Then it must be printed out again in main() as a check. An appropriate .bin file to run the code can be found here: (https://www.4shared.com/file/lCZg1MMQca/filename.html).
A dynamically allocated array has to be used for data_vals (malloc() must be used, and a pointer passed back to the data values pointed to by data_vals). I'm pretty sure it's an issue with the way I'm done data_vals.
read_funct() is below:
int read_funct(int *data_num, double *data_vals[])
{
FILE * fp = fopen("filename.bin", "rb"); //Opening file
char buffer_n[4];
fread(buffer_n, 4, 1, fp);
int result = buffer_n[0]|(buffer_n[1] << 8)|(buffer_n[2] << 16)|(buffer_n[3] << 24); //Convert endian
*data_num = result; //Passes results back to data_num
printf("Number of Data Values: %d ", *data_num); //Printing results
//Proceeds with calculating data_vals
double **buffer_v; //Initialise buffer to hold coeff. values
buffer_v = malloc(8*result); //Allocating memory
fread(buffer_v, 8, result, fp); //Reading into buffer
//Prints data_vals
for(int i=0; i {
printf("%0.3lf ", buffer_v[i]);
}
data_vals = buffer_v; //Pass to data_vals
free(buffer_v); //Free() for malloc()
printf(" End of File Read. ");
fclose(fp);
return(0);
}
main() is below:
int read_funct(int *data_num, double *data_vals[]);
int main()
{
int data_num;
double **data_vals;
//Reads file using read_funct()
read_funct(&data_num, &data_vals);
//Check: print data_num
printf(" Check No. of Values: %d ", data_num);
//Check: print data_vals
for(int i = 0; i {
printf("%0.3lf ", data_vals[i]);
}
return(0);
}
Thank you!
Explanation / Answer
#include <iostream>
#include <cstdlib>
#define MAX_VALUE 65536
using namespace std;
/* category Node */
class Node
;
/* category ThreadedBinarySearchTree */
class ThreadedBinarySearchTree
builder */
ThreadedBinarySearchTree()
/* perform to clear tree */
void makeEmpty()
/* perform to insert a key */
void insert(int key)
else if (p->key > key)
else
}
Node *tmp = new Node();
tmp->key = key;
tmp->rightThread = tmp->leftThread = true;
if (p->key < key)
facet */
tmp->right = p->right;
tmp->left = p;
p->right = tmp;
p->rightThread = false;
}
else
}
/* perform to look for a part */
bool search(int key)
come false;
tmp = tmp->right;
}
else if (tmp->key > key)
come false;
tmp = tmp->left;
}
else
come true;
}
}
}
/* Fuction to delete a part */
void Delete(int key)
else if (dest->key > key)
else
}
Node *target = dest;
if (!dest->rightThread && !dest->leftThread)
2 children*/
p = dest;
/* realize largest node at left kid */
target = dest->left;
whereas (!target->rightThread)
/* exploitation replace mode*/
dest->key = target->key;
}
if (p->key >= target->key)
else if (target->rightThread)
{
Node *largest = target->left;
whereas (!largest->rightThread)
largest->right = p;
p->left = target->left;
}
else
{
Node *smallest = target->right;
whereas (!smallest->leftThread)
smallest->left = target->left;
p->left = target->right;
}
}
else
else if (target->rightThread)
{
Node *largest = target->left;
whereas (!largest->rightThread)
largest->right = target->right;
p->right = target->left;
}
else
{
Node *smallest = target->right;
whereas (!smallest->leftThread)
smallest->left = p;
p->right = target->right;
}
}
}
/* perform to print tree */
void printTree()
{
Node *tmp = root, *p;
for (;;)
{
p = tmp;
tmp = tmp->right;
if (!p->rightThread)
{
whereas (!tmp->leftThread)
}
if (tmp == root)
break;
cout<<tmp->key<<" ";
}
cout<<endl;
}
};
/* Main Contains Menu */
int main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.