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
Choose the types of education an ISS professional
Viefleur [7K]

Answer:

It's A. a CompTIA certificate and D. a Microsoft-certified desktop support

certificate

Explanation:

I got it right.

7 0
3 years ago
Why is it important to know who reviews the information posted on a Web site?​
Aleks [24]

.bordered {    width: 200px;    height: 100px;    padding: 20px;    border-width: 6px;    border-color: pink;    border-style: dashed solid double;  }Why there is a need of concrete and coherent presentation ? ( class 9 ) {information technology}

5 0
3 years ago
Tor F: Deleting cell data is the same as clearing the contents of a cell.
Valentin [98]

Answer:

no it is not

Explanation:

it's not because of the cells in side the chip for the data that comes from the cell phone

4 0
3 years ago
I was logging into my origin account and this popped up. I can’t seem to click the NEXT button at the bottom. The blank spaces s
Valentin [98]

Answer:

Contact the company to find out what the problem is.

Explanation:

3 0
3 years ago
Read 2 more answers
For python how do I ask the user how many numbers they want to enter and then at the ending add those numbers up together?
Digiron [165]

I am not too familiar with the Python language, but the algorithm would be something like this:

1. create a variable for the sums of the number

2. read in how many numbers the user wants to enter (let's call it N)

and then create a for loop:

for N times

read in the next number

increase the sum variable by that number

Hopefully this helps!

8 0
3 years ago
Other questions:
  • In Outlook 2016, what are the three format options when sending an email message? Check all that apply.
    7·1 answer
  • 1. [2 points] Write a function to compute the volume of a sphere, given its radius. 2. [2 points] Write a recursive function to
    7·1 answer
  • Which type of computer network ensures high quality​
    9·1 answer
  • What does it mean when system ui has stopped?
    9·1 answer
  • Which of the following people was a member of FFA?
    7·1 answer
  • Please help explain this calculator code.
    15·1 answer
  • Ryo currently earns a monthly salary of $2200. She has been offered a raise of $250 per month. How much more will she earn per y
    14·1 answer
  • How can you prevent someone with access to a mobile phone from circumventing access controls for the entire device
    5·1 answer
  • Hypertext enables you to navigate through pieces of information by clicking the __________, that connect them.
    8·1 answer
  • identify the difficulties with the k-nearest neighbor algorithm. a. both a and b b. calculate the distance of the test case from
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!