Question

3 Programming Question (45 points) 3.1 Instructions You need to write the code by yourself. Your implementation must use C/C++ and your code must run on the Linux machine general.asu.edu. Please refer to the programming guide (available under the Assignments folder on Blackboard) for using the general.asu.edu server, as well as compiling and running C/C++ code under Linux For this question, you need to provide a Makefile that compiles your program to an executable named a3 that runs on the Linux machine general.asu.edu. Our TA will write a script to compile and run all student submissions; therefore, executing the command make in the Code folder must produce the executable a3 also located in the Code folder 3.2 Requirements For this part, you need to first review Section 10.4, Representing rooted trees. The requirements are as follows. You will write a serial program that executes a sequence of commands that operate on individual files. Valid commands include: Start Name, where Name is a character string of maximum length 20 alphabetic characters representing the name of the data file (Name.txt). Name.txt contains a set of data entries (integer type, one per line). This command reads all the data entries into an array (dynamically allocated). The output of the Start command is: Processing data from: Name.txt Build-BST, which builds a BST for the data entries in the array by calling the TREE-INSERT algorithm (pseudo code can be found in the lecture notes) repeatedly to insert the numbers one by one and output Building BST . INORDER-TREE-WALK, which calls the INORDER-TREE-WALK algorithm (pseudo code can be found in the lecture notes). Note that if the BST has not been built yet, output Build BST first! Otherwise, the output of this step should be sorted. For example, if the user-provided file contains the following data entries (one per line): 9, 2, 10, 4, 5, the output of your INORDER-TREE-WALK algorithm should be 2, 4, 5, 9, 10 End Name, which indicates the end of the processing for the data in file Name.txt. The Start and End commands will always come in pairs with matching names. Any memory dynamically allocated for Name must be freed on an End command. The output of the End command is: End of processing data from: Name.txt . Exit, which indicates that there are no more commands to execute, i.e., the program terminates Here is a sample command sequence Start test 1 Build-BST INORDER-TREE-WALK End test1 Start test 2 INORDER-TREE-WALK

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<malloc.h>
#include<fcntl.h>
#include<string.h>
int num(char *num)
{
   int dec = 0, i, j, len;
   len = strlen(num);
   for(i=0; i<len; i++){
       dec = dec * 10 + ( num[i] - '0' );
   }
   return dec;
}
struct node
{
   int data;
   struct node *left;
   struct node *right;
};
struct node *tree;
void create(struct node *tree)
{
tree=NULL;
}
struct node *TREE_INSERT(struct node *tree,int val)
{
   struct node *ptr,*nodeptr,*parentptr;
   ptr=(struct node *)malloc(sizeof(struct node));
   ptr->data=val;
   ptr->left=NULL;
   ptr->right=NULL;
   if(tree==NULL)
   {
       tree=ptr;
       tree->left=NULL;
       tree->right=NULL;
   }
   else
   {
       parentptr=NULL;
       nodeptr=tree;
       while(nodeptr!=NULL)
       {
           parentptr=nodeptr;
           if(val<nodeptr->data)
               nodeptr=nodeptr->left;
           else
               nodeptr=nodeptr->right;
       }
       if(val<parentptr->data)
           parentptr->left=ptr;
       else
       parentptr->right=ptr;
   }
   return tree;
}
void INORDER_TREE_WALK(struct node *tree)
{
   if(tree!=NULL)
   {
       inorder(tree->left);
       printf("%d ",tree->data);
       inorder(tree->right);
   }
}
int main()
{
int n,i,count=0;
char buf[2];
int fd=open("name.txt",777);
while(read(fd,buf,1)!='\0')
{
   if(buf[0]=='\n')
   count++;
}
close(fd);
fd=open("name.txt",777);
int arr[count];
create(tree);
for(i=0;i<count;i++)
{
   read(fd,buf,1);
   int t=num(buf[0]);
   tree=TREE_INSERT(tree,t);
}  
INORDER_TREE_WALK(tree);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
3 Programming Question (45 points) 3.1 Instructions You need to write the code by yourself. Your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • All commands must be in the command line. The expense data file is separate. Programming Project...

    All commands must be in the command line. The expense data file is separate. Programming Project #2: Manage Spending Using Commands Objectives: • Understand how to create and manipulate list of objects using array or vector class • Handle input errors and invalid values • Design and create a well-structure program using C++ basic programming constructs and classes. Description: Each “expense” contains 2 values: the spending amount (double) and its description (string) Here is an example of the “expense” data...

  • Need this in c programming

    Question:Many files on our computers, such as executables and many music and video files, are binary files (in contrast to text files). The bytes in these files must be interpreted in ways that depend on the file format. In this exercise, we write a program data-extract to extract integers from a file and save them to an output file. The format of the binary files in this exercise is very simple. The file stores n integers (of type int). Each...

  • Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A...

    Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A compiler must examine tokens in a program and decide whether they are reserved words in the Java language, or identifiers defined by the user. Design a program that reads a Java program and makes a list of all the identifiers along with the number of occurrences of each identifier in the source code. To do this, you should make use of a dictionary. The...

  • I need help with this code, I'm stuck on it, please remember step 4, I'm very...

    I need help with this code, I'm stuck on it, please remember step 4, I'm very much stuck on that part. It says something about putting how many times it appears Assignment #1: Sorting with Binary Search Tree Through this programming assignment, the students will learn to do the following: Know how to process command line arguments. 1 Perform basic file I/O. 2. Use structs, pointers, and strings. Use dynamic memory. 3. 4. This assignment asks you to sort the...

  • #1 To execute a correctly written program, you first need to _____.                 highlight the output...

    #1 To execute a correctly written program, you first need to _____.                 highlight the output commands in the code                 print the code for reference                 compile it to machine language                 compile it from machine language into the programming language of choice #2 Adding capabilities to working software is called the _____ phase.                 coding                 maintenance                 debugging                 brainstorming #3 A rectangle in a flowchart represents which of the following?                 One or more I/Os                ...

  • Edit a C program based on the surface code(which is after the question's instruction.) that will...

    Edit a C program based on the surface code(which is after the question's instruction.) that will implement a customer waiting list that might be used by a restaurant. Use the base code to finish the project. When people want to be seated in the restaurant, they give their name and group size to the host/hostess and then wait until those in front of them have been seated. The program must use a linked list to implement the queue-like data structure....

  • n this programming assignment, you need to create 3 files. 1. DateType.h 2. DateType.cpp 3. A...

    n this programming assignment, you need to create 3 files. 1. DateType.h 2. DateType.cpp 3. A test driver for testing the class defined in the other 2 files. You can name your file in the way you like. Remember it must be a .cpp file. In DateType.h file, type these lines: // To declare a class for the Date ADT // This is the header file DateType.h class DateType { public: void Initialize(int newMonth, int newDay, int newYear); int GetYear()...

  • FIRST: Write code that prompts the user for the name of a text file, opens that...

    FIRST: Write code that prompts the user for the name of a text file, opens that file if it exists and reads the file one line at a time printing each line to the screen. You must implement the file read in a try/catch block rather than throwing the exception on the main method. NEXT: Modify your code so that the program takes the name of two files from the command line, opens the first file if it exists for...

  • CSIT 345 Lab 2 Process Programming Your lab requirement is to write program codes for 3.21 and sh...

    *Help Please with the code** CSIT 345 Lab 2 Process Programming Your lab requirement is to write program codes for 3.21 and shared memory program for producer and consumer as shown in the following. You can start with the code provided in the virtual machine in the virtual box you installed. The code can be found in /home/oscreader/osc9e-src/ch3 a. For 3.21, you can start with the newprocposix.c and modify the code to meet your requirement. Then type: gcc neypCOCROSİS.c to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT