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
In 1-2 sentences, describe how to use the thesaurus in the Word Processor you have used.
jasenka [17]
Thesaurus is a tool use to find the synonym of the of a word.
Here's the way to use the thesaurus in the word processor.
=> highlight the word, then right-click, Navigate to synonyms and the words will  display.
3 0
3 years ago
Read 2 more answers
What are the layers in the internet protocol stack? what are the principle responsibilities of each of these layers?
Andrew [12]
1: Application
2: Presentation
3:Session
4: Transport
5:Network 
6: Data link 
7: Physical

Application is a Interface Layer and responsible Layer
<span />
3 0
3 years ago
Why is the keyboard calledQWERTY
taurus [48]
The standard QWERTY layout keyboard is called 'QWERTY' because on the top line of the keyboard the first 6 letters from chronological left to right order are; Q, W, E, R, T, and Y.

Or...if you're wondering why it's QWERTY and not ABCDEF, it's because when typing with the alphabetical format, many of the keys would clash with each other due to the arrangement of keys on the original typewriter. The QWERTY layout became so popular, it was the standardized layout for typewriters, and even keyboards today.
4 0
3 years ago
Read 2 more answers
Which terms describes Benito Mussolini’s form of government
slega [8]
The political system that describes Benito Mussolini’s form of government would be totalitarian.Hope this answers the question:)
5 0
4 years ago
Read 2 more answers
Guys, it took me 3 seconds to get banned on brainly, try to beat that, actually don't, unless you don't care about your account
Anarel [89]

How did u get banned?

Explanation:

6 0
3 years ago
Read 2 more answers
Other questions:
  • What of the following google tools support collaboration
    10·1 answer
  • Your company has recently been hired to install a smart security system for a large office building. The system will include sec
    12·1 answer
  • What type of data can we enter in a spreadsheet?
    11·1 answer
  • Please I need help with this !!!! <br> Complete the table given below.
    8·1 answer
  • What data discovery process, whereby objects are categorized into predetermined groups, is used in text mining?
    12·1 answer
  • Indicate the time efficiency classes of the three main operations (i.e., FindMax, DeleteMax, and Insert) of the priority queue i
    11·1 answer
  • Most operating systems today primarily use a ____.
    12·1 answer
  • Please help me on this coding problem :)
    6·1 answer
  • Write a program code which asks for 80 numbers between 100 and 1000 to be entered.
    7·1 answer
  • Suppose you have a string matching algorithm that can take in (linear)strings S and T and determine if S is a substring (contigu
    11·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!