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
mash [69]
3 years ago
10

In this lab you will write a C program to sort an array of strings. The program will: Create an array of 50 strings; each string

can hold up to 30 characters. Write a function to read strings from the file "strings.dat" into your array. Read until EOF and keep track of how many strings you read, as it may be less than 50. Then pass this variable to your print and sort functions so they know how many strings to print and sort. Use fopen to open the file and fgets to read each string. Write a function to sort the strings. Only sort the array entries that contain strings read from the file. See below for pseudocode for selection sort. Write a function to swap two strings, which will be called by your sort function. Pass the array and the subscripts of the two strings that need to be swapped. Write a function to print out the array entries that were read into the array. Use printf. Your main function should call the functions to read into the array, print the array, sort the array, and print the array again.
Computers and Technology
1 answer:
andrezito [222]3 years ago
6 0

Answer:

See explaination

Explanation:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

void swap(char *arr[], int i, int minIdx)

{

char temp[30];

strcpy(temp, arr[i]);

strcpy(arr[i], arr[minIdx]);

strcpy(arr[minIdx], temp);

}

void sort(char *arr[], int n)

{

for (int i = 0; i < n - 1; i++)

{

int minIdx = i;

int minV = 10000;

char min[30];

strcpy(min, arr[i]);

for (int j = i + 1; j < n; j++)

{

int a = strcmp(min, arr[j]);

if (a < 0)

{

strcpy(min, arr[i]);

minIdx = j;

}

}

swap(arr, i, minIdx);

}

}

void print(char *arr[], int n)

{

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

printf("%s", arr[i]);

}

void read(char *arr[], int *n)

{

FILE *file = fopen("strings.dat", "r");

char line[30];

*n = 0;

printf("Reading from file...\n");

while (fgets(line, 30, file))

{

strcpy(arr[(*n)++], line);

}

print(arr, *n);

}

int main(int argc, char const *argv[])

{

char *arr[50];

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

arr[i] = (char *)malloc(sizeof(char) * 30);

int len;

int n = 0;

read(arr, &n);

printf("\n-------------------------\nAfter sorting\n");

sort(arr, n);

print(arr, n);

}

You might be interested in
Assume a system has a TLB hit ratio of 90%. It requires 15 nanoseconds to access the TLB, and 85 nanoseconds to access main memo
andriy [413]

Answer:

310 ns

Explanation:

Given that

TLB hit ratio = 90%

TLB hit ratio = 90/100

TLB hit ratio = 0.9

Time needed to access TLB = 15 ns

Time needed to access main Memory = 85 ns

Effective memory access time = ?.

The formula for finding the effective memory access time is given by

The effective memory access time = [TLB Hit ratio (main memory access time + required time to access TLB) + [2 * (main memory access time + required time to access TLB)] * (2 - TLB hit ratio)]

On substituting the values given in the question to the equation, we have

The effective memory access time = [0.9 (85 + 15) + [2 * (85 + 15)] * (2 - 0.90)]

The effective memory access time =

[(0.9 * 100) + (2 * 100) * 1.1]

The effective memory access time =

(90 + (200 * 1.1))

The effective memory access time =

90 + 220

The effective memory access time =

310 ns

3 0
3 years ago
Read 2 more answers
Now, it’s time for you to begin coding your text-based adventure game!Starting at the top, convert each line of your pseudocode
alexira [117]

Answer:

what is your question? I can't answer this question haha

4 0
3 years ago
Read 2 more answers
What would the internet be like today if there was no World Wide Web?
Anvisha [2.4K]

Answer:

Non existent probably

Explanation:

3 0
3 years ago
One of the main reasons for copyright law is to encourage creators to share their work for the benefit of the general public. Pl
irina [24]

Answer:

ewatuqhfuyerhjf

Explanation:

true

8 0
3 years ago
Four possible skills a person could have
cluponka [151]

Answer:

Active listening skills.

Genuine interest in others.

Flexibility.

Good judgment.

Explanation:

4 0
4 years ago
Other questions:
  • Select the correct answer.
    7·1 answer
  • Working in a meat factory packaging and shipping the meat deliveries falls into which agricultural cluster?
    7·1 answer
  • QUESTION 1 _____ is a type of data encryption that enables users of the Internet to securely and privately exchange data through
    14·1 answer
  • Write a program that displays a menu allowing the user to select air water, or steel. After the user has made a selection, the n
    10·1 answer
  • Which is the least technically experienced technical support group?
    7·1 answer
  • Segregation based on laws is called _____
    11·2 answers
  • What is a current trend in ERP systems?
    12·1 answer
  • Which system is understood by the computer system​
    7·1 answer
  • Find the ratio of 24 to 36​
    5·2 answers
  • if prakash gives one of the marble from what he possessses to kamala then they will have equal number of marbles.if kamala gives
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!