Question

In: Computer Science

Download “dict.h” and “dictionary.c” posted on blackboard. a) read both code to figure out what they...

Download “dict.h” and “dictionary.c” posted on blackboard.
a) read both code to figure out what they do.
b) supply am appropriate content to the main function in “dictionary.c” to demonstrate
that you understand the provided code by invoking those functions defined in
“dictionary.c”
Submit your source code and script files.

/* dict.h header for data dictionary routines. */

#include <stdio.h>

struct dict_elem{
   char d_name[15]; // name of dictionary member
   int d_start; //starting position in record
   int d_length; //length of field
   int d_type; //denotes type of data
};

#define ERROR (-1)
#define SUCCESS 0

//dictionary.c


#include "dict.h"

int writedict(const char *dictname, struct dict_elem *elist){

   int j;
   FILE *outf;

   if ((outf = fopen(dictname, "w")) == NULL){
       return ERROR;
   }

   //cculate length of the aray
   for (j = 0; elist[j].d_length != 0; j++)
       ;

   //write out list of dict_elem structures
   if (fwrite((void*)elist, sizeof(struct dict_elem), j, outf)<j){
       fclose(outf);
       return ERROR;
   }


   fclose(outf);
   return SUCCESS;
}

//**************************************************************************


struct dict_elem * readdict(const char *dictname,
               struct dict_elem *inlist, int maxlength)
{

   int i;
   FILE *inf;

   if ((inf = fopen(dictname, "r")) == NULL){
       return NULL;
   }
   //read in dict_elem structures from file
   for (i = 0; i < maxlength - 1; i++)
   {
       if (fread((void*)&inlist[i], sizeof(struct dict_elem), 1, inf)<1)
       {
           break;
       }
   }


   fclose(inf);

//mark end of list
   inlist[i].d_length = 0;

   return inlist;
}


// your main function goes here.
int main(void)
{

// struct dict_elem array[3] ={ {}, {},{} , {} }


}

Solutions

Expert Solution

/* dict.h header for data dictionary routines. */

#include <stdio.h>

struct dict_elem
{
char d_name[15]; // name of dictionary member
int d_start; //starting position in record
int d_length; //length of field
int d_type; //denotes type of data
};

#define ERROR (-1)
#define SUCCESS 0
---------------------------------------------------------------------

//dictionary.c

#include "dict.h"

int writedict(const char *dictname, struct dict_elem *elist){

int j;
FILE *outf;

if ((outf = fopen(dictname, "w")) == NULL){
return ERROR;
}

//cculate length of the aray
for (j = 0; elist[j].d_length != 0; j++)
;

//write out list of dict_elem structures
if (fwrite((void*)elist, sizeof(struct dict_elem), j, outf)<j){
fclose(outf);
return ERROR;
}


fclose(outf);
return SUCCESS;
}

//**************************************************************************


struct dict_elem * readdict(const char *dictname, struct dict_elem *inlist, int maxlength)
{

int i;
FILE *inf;

if ((inf = fopen(dictname, "r")) == NULL){

return NULL;
}

//read in dict_elem structures from file
for (i = 0; i < maxlength - 1; i++)
{
if (fread((void*)&inlist[i], sizeof(struct dict_elem), 1, inf) < 1)
{
break;
}
}


fclose(inf);

//mark end of list
inlist[i].d_length = 0;

return inlist;
}


// your main function goes here.
int main(void)
{
int c;
// Creates dictionary name
char *dictname = "MyDictionary";

// Creates an array of dict_elem
struct dict_elem array[3] ={ {"Animal", 0, 4, 1}, {"Book", 2, 5, 2}, {"Bird", 1, 5, 3}};

// Calls the function to read data and checks if not SUCCESS displays error message
if(writedict(dictname, array) != SUCCESS)
printf("\n Unable to write");

// Creates a temporary object of dict_elem
struct dict_elem temp;
// Creates a pointer object pointing to temp
struct dict_elem *inlist = &temp;

// Calls the function to read data from file
inlist = readdict(dictname, &inlist[0], 4);

// Checks if inlist is not null
if(inlist != NULL)
{
// Loops 3 times
for(c = 0; c < 3; c++)
// Displays current object information
printf("\n\n Name: %s \n Start: %d \n Length: %d \n Type: %d",
inlist[c].d_name, inlist[c].d_start, inlist[c].d_length, inlist[c].d_type);
}
// Otherwise displays error message
else
printf("\n Unable to redirect");
return 0;
}
Sample Output:

Name: Animal
Start: 0
Length: 4
Type: 1

Name: Book
Start: 2
Length: 5
Type: 2

Name: Bird
Start: 1
Length: 5
Type: 3


Related Solutions

Please, read the code below so you can figure out what is the steps that are...
Please, read the code below so you can figure out what is the steps that are used. (Answer in words) import java.util.*; import java.io.*; / class WordHelper{ public static String vowels = "aeiouyAEIOUY";    //method to check whether a given character is vowel or not public static String[] sortByVowels(String[] words){ Integer[] noOfVowels = new Integer[words.length]; String[] newArray = new String[words.length];    newArray[i] = words[i]; int cnt = 0; for(int j = 0; j < words[i].length(); j++){ String temp = Character.toString(words[i].charAt(j));...
Both the SSTS and Circular 230 have been posted on Blackboard for your use in answering...
Both the SSTS and Circular 230 have been posted on Blackboard for your use in answering the following questions. Answer the following questions using the SSTS’s or Circular 230.   For each question cite the Standard or page number and section number of Circular 230 for which your answer is based upon. (Member means tax preparer.) (Disregard the page number) ______ 21.    A member may omit answering a question on a tax return if reasonable grounds exist for omitting the answer...
The observed genotype frequencies will be posted on blackboard as an announcement during class.
  Part 3 – Calculating allele frequency changes and Hardy-Weinberg proportions The observed genotype frequencies will be posted on blackboard as an announcement during class. The excel-based Hardy-Weinberg calculator (HWE.xls) will be helpful for filling out the table below. Focus on the dark colour soil population and calculate allele frequencies, frequencies of heterozygous individuals, etc. Simulation step #DD #Dd #dd Observed frequency of D allele Observed frequency of Dd individuals Expected frequency of Dd (assuming HWE) Chi-squared value* Starting conditions...
Question 2. Go to the Blackboard and download the MS excel file, ‘stock_return.xlsx’. It contains a...
Question 2. Go to the Blackboard and download the MS excel file, ‘stock_return.xlsx’. It contains a year of monthly stock price data of Amazon, Pfizer, and S&P 500 (Market Index). Using the data, answer the following questions. (50 points) (1) Compute the monthly return of Amazon and Pfizer. You should get 12 monthly returns for each. To get a monthly return, you need to use previous month’s stock price. For example, Amazon’s stock return of 2018-01 will be [(Stock price...
Download RegHousePrice.xlsx from the course Blackboard site. The first variable measures the price of the house,...
Download RegHousePrice.xlsx from the course Blackboard site. The first variable measures the price of the house, and this is followed by the number of bedrooms, the size of the house (in sq. ft.), and the size of the lot (i.e. yard; also in sq. ft.). a. Build and estimate a regression model to predict the selling price of a house. b. Evaluate the model: be sure to mention the goodness of fit, the sign of the coefficients, the statistical significance...
Download the dataset CARS1 from BlackBoard. a. Do not worry about outliers. Assume the data is...
Download the dataset CARS1 from BlackBoard. a. Do not worry about outliers. Assume the data is correct and any outliers will remain in the dataset. b. Do scatterplot and analyze the results. c. Test for correlation (correlation coefficient) d. Regress weight (column 2) against gas mileage in the city (column 1). Make sure you make gas mileage the dependent (Y) variable. e. Determine and fully explain R2 MPG City Weight 19 3545 23 2795 23 2600 19 3515 23 3245...
What does the top pressure gauge in the figure read?
What does the top pressure gauge in the figure read?
Read the NAQ article that was posted in the lesson. Knowing what you know from the...
Read the NAQ article that was posted in the lesson. Knowing what you know from the chapters in this unit (Chapters 10 through 14), discuss the recommendations presented in the article for the Ideal system: Simple, fast documentation with minimal redundancy. EHR coordination with patient and family needs and wishes. Central source of best practice forms and other resources in a free, vendor-neutral, and accessible library. Multidisciplinary collaboration on the design of EHR and CDS systems. Data: Standardized, actionable, and...
This is an assignment for python. Download the code “ GradeBook.py ” You will be modifying...
This is an assignment for python. Download the code “ GradeBook.py ” You will be modifying the code to do the following: 1) create an empty dictionary named “FinalAverages” 2) In one for loop you will zip all lists together and get their individual members on each iteration. You can name these what ever you want. 3) on each iteration: Calculate the WEIGHTED average using the weights provided, and then add a new dictionary value to the dictionary “ FinalAverages...
This situation in question is that shown in Figure 17.33 of Etkina, which is posted in...
This situation in question is that shown in Figure 17.33 of Etkina, which is posted in a PDF in BB. Please refer to that figure when answering questions. A) When a charged particle enters a region of uniform magnetic field, where the magnetic field is perpendicular to the velocity of the particle, the particle will execute uniform circular motion. There is a relationship between the radius, charge, mass, speed and magnetic field – you’ll find it at the top of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT