Question

In C only Please! This lab is to write a program that will sort an array...

In C only Please!

This lab is to write a program that will sort an array of structs.

  • Use the functions.h header file with your program.

  • Create a source file named functions.c with the following:

    • A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort as they are the easiest to implement. You will sort on the integer data member.
    • A printing function named printArray. It takes an array of MyStruct's and the length of that array. It returns nothing. It will simply print the integer data member and the string data member separated by a space with each array element on a separate line. Here is an example of the output:

 
  • Create a main.c source file with the main() that will do the following:
    • Prompt the user for the number of elements they want in the array
    • Dynamically allocate an array of MyStruct's of that size the user entered
    • Prompt the user for an integer and string to input into each element of the array
    • Call sortArray() on the array to sort it.
    • Call printArray() on the array to print out the values

Example output:

10 Epsilon

15 Eta

20 Beta

0 0
Add a comment Improve this question Transcribed image text
Answer #1

functions.h functions.c [/header file: functions.h #ifndef FUNCTIONS_H #define FUNCTIONS_H 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1


//header file: functions.h

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

// declare the structure
struct MyStruct{
int integer;
char string[100]; // 100 is maximum number of characters expected in the string
};

// declare the functions

void sortArray(struct MyStruct array[], int length);

void printArray(struct MyStruct array[], int length);

#endif


___________________________

#include<stdio.h>

#include "functions.h" // include the header file for functions

// implement the function to print the array
void printArray(struct MyStruct array[], int length){
   for(int i = 0; i < length; i++)
       printf("\n%d\t%s", array[i].integer, array[i].string);
}

// implement implement the function to [bubble] sort the array
void sortArray(struct MyStruct array[], int length){
   for(int i = 0; i < length-1; i++)
       for(int j = 0; j < length-i-1; j++)
           if(array[j].integer > array[j+1].integer){
           // swap the structure elements
               struct MyStruct temp = array[j];
               array[j] = array[j+1];
               array[j+1] = temp;
           }
}

int main(){
  
// get number of elements (length) from user
   int length;
   printf("Enter number of elements: ");
   scanf("%d",&length);
  
// dynamically allocate memory to the array for the length input by user
   struct MyStruct *array = malloc(length * sizeof(struct MyStruct));

// get array content from user
   for(int i = 0; i < length; i++){

   printf("\n---Element%d--- ",i+1);

       printf("\nEnter integer: ");
       scanf("%d",&array[i].integer);

       printf("Enter string: ");
       scanf("%s",array[i].string);
   }
  
// call the functions to sort and then print the contents of the array
   sortArray(array,length);
   printArray(array,length);

   return 0;
}


------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~

functions.h functions.c 1 #include<stdio.h> 2 #include functions.h // include the header file for functions // implement thint main(){ Il get number of elements (length) from user int length; printf(Enter number of elements: ); scanf(%d,&lengthEnter number of elements: 3 ---Element1--- Enter integer: 15 Enter string: Eta ---Element2--- Enter integer: 20 Enter string:

Add a comment
Know the answer?
Add Answer to:
In C only Please! This lab is to write a program that will sort an array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java 2. Array Exercise ( 10 points) Write a Java program that will prompt the...

    In Java 2. Array Exercise ( 10 points) Write a Java program that will prompt the user to input a size of an array. Create an array of type int. Ask a user to fill the array. Create a functions sortArray() and mostFrequency(). The sortArray() function will sort number into incrementing order. The sorting function must be implemented from scratch and it must not use any function from the library. Feel free to use any sorting algorithm. The program will...

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • C++ Create an array-based implementation of a stack. Each element of the stack should store a...

    C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...

  • Write a program that will do the following. The main function should ask the user how...

    Write a program that will do the following. The main function should ask the user how many numbers it will read in. It will then create an array of that size. Next, it will call a function that will read in the numbers and fill the array (fillArray). Pass the array that was made in themain function as a parameter to this function. Use a loop that will read numbers from the keyboard and store them in the array. This...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and...

    Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and returns the index of the "last occurrence" of the largest element in the array. Include another function to print the array. Also, write a program to test your function. [HINTS) Create an array of size 15 in the main function and initialize it with the values shown in the below sample output. Pass the array and its size to function lastLargestindex; function lastLargestindex returns...

  • Write C programs for the following: 1. Modify the sorting program you wrote in Lab 2...

    Write C programs for the following: 1. Modify the sorting program you wrote in Lab 2 so that it is modular. Write a function called sort that accepts an integer array, sorts it, and returns the sorted array to the main function. Call this function from the main program. In addition, write a subroutine called print_array that accepts an integer array and prints it out in this format: The array is [01 2 3456 7 8 9] Use this function...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT