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
Name any extension of MS word.
Svetradugi [14.3K]
Name any extension of MS word.

.doc
8 0
3 years ago
Read 2 more answers
Cho dãy A gồm N số nguyên a1,...aN. Hãy đếm tất cả các số âm hoặc không chia hết cho 5 trong dãy
aleksandrvk [35]

Answer:

#include<iostream>

int main()

{

int count=0;

int so_phan_tu;

std::cout << "nhap so phan tu : \n";

std::cin >> so_phan_tu;

int* A = new int[so_phan_tu];

std::cout << "nhap cac phan tu : \n";

for (int i = 0; i < so_phan_tu; i++)

{

 std::cin >> A[i];

 if (A[i] % 5 == 0)

 {

  count++;

 }

}

std::cout << so_phan_tu - count;

system("pause");

return 0;

}

//cái này viết bằng C++ em nhé

Explanation:

3 0
2 years ago
Which role is delegated to personnel of the IT department and is responsible for maintaining the integrity and security of the d
enyata [817]

Correct question.

Which role is delegated to personnel of the IT department and how is responsible for maintaining the integrity and security of the data?

Answer:

<u>data custodian</u>

<u>Explanation:</u>

Remember, every organization generates data, and large organizations generate even larger data.

Hence, the role of a data custodian in an organization requires He implements measures to protect the organization's data, store and backup the data, as well as granting access to the data to authorized persons when needed.

3 0
2 years ago
Create a new data frame, first_south, by subsetting titanic to include instances where a passenger is in the first class cabin (
Virty [35]

The data frame first_south is created using first_south = (titanic['Pclass']==1) & (titanic['Embarked']=='S')

<h3>How to create the data frame?</h3>

To do this, we make the following assumptions:

  • The pandas module has been loaded as pd
  • The dataset has also been loaded as titanic

When pclass column is 1.

This is represented as:

titanic['pclass']==1

When the passenger boards from Southampton.

This is represented as:

titanic['Embarked']=='S'

So, we have:

first_south = (titanic['Pclass']==1) & (titanic['Embarked']=='S')

Read more about data frames at:

brainly.com/question/16524297

#SPJ1

4 0
1 year ago
Question #7
Elis [28]
The answer to this question is firewall
4 0
3 years ago
Other questions:
  • Tower defense is included under which genre of game
    15·2 answers
  • According to your textbook, which of the following is a consequence of the quick development of new technologies in the digital
    8·1 answer
  • In addition to training on the products and on company policy, it does not make sense to be prepared to speak about your company
    6·2 answers
  • Match the job titles to the tasks
    11·2 answers
  • How many generations of computer languages have there been since the middle of the 20th century?
    10·1 answer
  • You run the ipconfig command on your computer, and it reports an IP address of 169.254.75.10 on the Ethernet interface. Which de
    8·1 answer
  • Write a void method named myMethod which prints "This is a void method" (without the quotes). Your method should be declared pub
    5·1 answer
  • What would be the best tool to display the following information from a basketball game?
    6·1 answer
  • Hello i need help whats -5 = 500
    13·2 answers
  • In which generation microprocessor was developed short answer of computer science​
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!