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
When does personal information often need to be entered online?
soldi70 [24.7K]

Answer:

Like when your doing an application for a job or anything important lol

Explanation:

3 0
3 years ago
By default, the windows password policy requires a minimum password of what length?​ select one:
kramer
<span>7 characters are the default for WIN7/8/10. This can, of course, be changed many ways via the registry or via a GPO.</span>
7 0
3 years ago
Which statement best describes an advantage of using the Subtotal dialog box?
SCORPION-xisa [38]
I think it's 2 i'm not sure
3 0
4 years ago
Read 2 more answers
What will be the output of the statement?<br> System.out.println(2*4*(8+3)-9%7);
Lapatulllka [165]

Answer:

87.37

Explanation:

(2*4*(8+3)-9%7)

(8*(8+3)-9%7)

8*(11)-9%7

88-8%7

88-0.63

87.3

7 0
4 years ago
11:25
avanturin [10]
Starting address of the stack
6 0
2 years ago
Other questions:
  • This device transmits data to all the workstations on a network. gateway hub router switch
    11·2 answers
  • It is important to ____ the line in the code editing window in the exact location where you want to insert a code snippet to pro
    10·1 answer
  • How to add running head and page number in word?
    5·1 answer
  • Web pages are accessed through a software program called a _____.A) ​web crawlerB) ​web browserC) ​web serverD) ​web app drawer
    5·1 answer
  • What allows you to navigate up, down, left and right in a spreadsheet?
    12·1 answer
  • Which of the following choices best completes the above flowchart?
    11·1 answer
  • Consider the following code segment.
    11·1 answer
  • Where does Reiner take eren after they have a fight?
    7·2 answers
  • 30 POINTS FOR THE CORRECT ANSWERS
    12·1 answer
  • recursive function that accepts a stack of integers as a parameter and replaces every even value in the stack with two occurrenc
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!