subject

The picture above is a description of the C code below. Please modify the C below so that it uses FILE HANDLING (fopen, fputs, fgets, etc.). You may also be required to use command line parameters for the input file, thank you.

main. c:

#include "contacts. h"
#include
#include

int main(void) {
size_t size;
contact **c = alloc_contacts(MAX_CONTACTS);
size = get_contacts(c);
if (size == 0)
perror("get_contacts failed");
sort_contacts(c, size);
print_contacts(c, size);
free_alloc(c, size);

return 1;
}

contacts. c:

#include "contacts. h"
#include
#include

/* Allocate space for n number of contacts.
Then return a pointer to the array */
contact **alloc_contacts(size_t n) {
contact **c = malloc(n * sizeof(contact *));
if (c == NULL)
perror("Malloc failed");

return c;
}

/* Populate contact array with values from stdin */
size_t get_contacts(contact **c) {
char zip[64];
size_t size = 0;
for (size_t i = 0; i < MAX_CONTACTS; i++, size++) {
/* allocate contact struct as needed */
c[i] = malloc(sizeof(contact));
if (c[i] == NULL)
perror("Malloc failed");
fgets(c[i]->name, MAX_BUF, stdin);
fgets(c[i]->address, MAX_BUF, stdin);
fgets(c[i]->locale, MAX_BUF, stdin);
fgets(zip, sizeof(zip), stdin);
c[i]->zip = atoi(zip);
/* if EOF reached, break loop to avoid more allocs */
if (feof(stdin))
break;
}

return c ? size : 0;
}

void swap(contact *a, contact *b) {
contact temp = *b;
*b = *a;
*a = temp;
}

/* Bubble sort O(n^2) */
void sort_contacts(contact **c, size_t n) {
int i, j = n, s = 1;
while (s) {
s = 0;
for (i = 1; i < j; i++) {
if (c[i]->zip < c[i - 1]->zip) {
swap(c[i], c[i - 1]);
s = 1;
}
}
j--;
}
}

/* Loop over contact array printing each contact element */
void print_contacts(contact **c, size_t size) {
for (size_t i = 0; i < size; i++)
printf("%s%s%s%d\n", c[i]->name, c[i]->address, c[i]->locale, c[i]->zip);
}

void free_alloc(contact **c, size_t size) {
for (size_t i = 0; i < size; i++) {
free(c[i]);
}
free(c);
}

contacts. h:

#pragma once
#define MAX_CONTACTS 50
#define MAX_BUF 50

typedef struct {
char name[MAX_BUF];
char address[MAX_BUF];
char locale[MAX_BUF];
unsigned int zip;
} contact;

contact **alloc_contacts(size_t n);
size_t get_contacts(contact **c);
void print_contacts(contact **c, size_t size);
void sort_contacts(contact **c, size_t n);
void swap(contact *a, contact *b);
void free_alloc(contact **c, size_t size);


The picture above is a description of the C code below. Please modify the C below so that it uses F

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 13:00
"in program arrays12.java a swap method is called. does the swap method exchange parameter values x and y? "
Answers: 3
question
Computers and Technology, 22.06.2019 15:50
The file sales data.xlsx contains monthly sales amounts for 40 sales regions. write a sub that uses a for loop to color the interior of every other row (rows 3, 5, etc.) gray. color only the data area, columns a to m. (check the file colors in excel.xlsm to find a nice color of gray.)
Answers: 2
question
Computers and Technology, 23.06.2019 12:00
If you embed a word table into powerpoint, what happens when you make edits to the embedded data? a. edits made to embedded data change the data in the source file; however, edits made to the source file will not be reflected in the embedded data. b. edits made to embedded data will change the data in the source file, and edits made to the source file will be reflected in the embedded data. c. edits made to embedded data don't change the data in the source file, nor will edits made to the source file be reflected in the embedded data. d. edits made to embedded data don't change the data in the source file; however, edits made to the source file will be reflected in the embedded data.
Answers: 1
question
Computers and Technology, 23.06.2019 22:30
You draw two cards from a standard deck of 52 cards, but before you draw the second card, you put the first one back and reshuffle the deck. (a) are the outcomes on the two cards independent? why?
Answers: 3
You know the right answer?
The picture above is a description of the C code below. Please modify the C below so that it uses FI...
Questions
question
Business, 01.10.2019 04:40
question
Mathematics, 01.10.2019 04:40
question
Mathematics, 01.10.2019 04:40
Questions on the website: 13722367