1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Kruka [31]
3 years ago
5

The picture above is a description of the C code below. Please modify the C below so that it uses FILE HANDLING (fopen, fputs, f

gets, 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);

Computers and Technology
1 answer:
siniylev [52]3 years ago
3 0

Answer: i just learned something new.

Explanation:

You might be interested in
Analyze the following code.
denis-greek [22]

Answer:

C

Explanation:

No explanation, self-explanatory. I used class main instead...

4 0
3 years ago
How do you find the exterior angle measure of any regular polygon? (The degree in which to turn the turtle to draw a shape)
Diano4ka-milaya [45]

Answer:

360/number of sides

Explanation:

all the exterior angles add up to 360°

so to find each angle mesure, divide the 360 by the number of sides the figure has.

7 0
3 years ago
Drag each tile to the correct box. Arrange the statements in sequence to correctly explain what the field of science includes. a
erastova [34]

These refer to the elementary steps of the scientific method.

The correct order is as follows:

  • Keen observation of natural phenomena.

  • The use of imagination and creativity to test understanding and predictions.

  • An attempt to verify the understanding of observed natural occurrences establishing verifiable scientific knowledge

  • Continued observance and testing to revise scientific knowledge.

  • An understanding of how or why these natural phenomena occur.

Learn more about the scientific methods at the link below:

brainly.com/question/497944

3 0
2 years ago
Chelsea wants to know about the requirements for being a lab technician. Where could she find that information?
7nadin3 [17]
On a job application.
5 0
2 years ago
Suppose you have two RAID arrays, one implementing RAID 3, the other RAID 5. Each has 9 disk drives in its array. If the RAID 5
Snezhnost [94]

Answer:

48 is the answer

Explanation:

16×3 is the explanation for your question

8 0
3 years ago
Other questions:
  • EBay buyers voluntarily comment to other users and sellers on the quality of service, promptness of shipping, and their general
    5·1 answer
  • The ____ function sums the numbers in the specified range and then divides the sum by the number of cells with numeric values in
    14·1 answer
  • I analyze data, as a consultant, for companies making important business decisions. In order to get my point across, which of th
    11·1 answer
  • A digital system has been developed to measure temperatures in the range -10˚C to 100˚C. If the accuracy of the system is such t
    11·1 answer
  • Write a program, named NumDaysLastNameFirstName.java, which prompts the user to enter a number for the month and a number for th
    13·1 answer
  • What are some examples and non-examples of digital security?
    6·1 answer
  • Please help ASAP!
    7·2 answers
  • How many voltage values can be represented with a 10-bit binary code?
    15·1 answer
  • Create a text file content.txt and copy-paste following text (taken from Wikipedia) into it: A single-tasking system can only ru
    7·1 answer
  • ABC company have lots of computer running window 7. But they are not thinking to upgrade a higher version of window. One of the
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!