Question

In: Statistics and Probability

Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define...

  • Use the functions.h header file with your program (please write in C code):

    • #ifndef FUNCTIONS_H
      #define FUNCTIONS_H

      typedef struct MyStruct
      {
      int value;
      char name[ 100 ];
      } MyStruct;

      void sortArray( MyStruct*, int );
      void printArray( MyStruct*, int );

      #endif

  • 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:
10 Epsilon
15 Eta
20 Beta
  • 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

Solutions

Expert Solution

CODE:

//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;
}

If you have any doubts please comment and please don't dislike.

PLEASE GIVE ME A LIKE. ITS VERY IMPORTANT FOR ME.


Related Solutions

This is the header file: #ifndef __HW2_H__ #define __HW2_H__ // Previous two lines are the start...
This is the header file: #ifndef __HW2_H__ #define __HW2_H__ // Previous two lines are the start of the marco guard // Try not to change this file #include <iostream> #include <cmath> using std::cout; using std::endl; using std::istream; using std::ostream; class Point { private:    double x, y, z; public:    // Constructors    Point();    Point(double inX, double inY, double inZ = 0);    Point(const Point& inPt);    // Get Functions    double getX() const;    double getY() const;   ...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
**C++ program** Please provide a header file(bubble_sort.h) and a program file(bubble_sort.cpp). Also include notes to explain...
**C++ program** Please provide a header file(bubble_sort.h) and a program file(bubble_sort.cpp). Also include notes to explain code and output screenshots. Please use Bubble Sort code provided. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers...
Please Write Code in C++ and include the correct #include <header> and not a catchall such...
Please Write Code in C++ and include the correct #include <header> and not a catchall such as bits/stdc: Write a recursive, string-valued function, replace, that accepts a string and returns a new string consisting of the original string with each blank replaced with an asterisk (*) Replacing the blanks in a string involves: Nothing if the string is empty Otherwise: If the first character is not a blank, simply concatenate it with the result of replacing the rest of the...
write in C++ as simple as possible please and thanks don't use header file <bits/stdc++.h> All...
write in C++ as simple as possible please and thanks don't use header file <bits/stdc++.h> All programs work with binary numbers which are powers of 2 (2, 4, 8, …).  Write a program that will have a user enter a number (n) less than 10,000. Then have the program put each power of 2 (int) into an array up to and possibly including the number n. This means you don't know at the start how large the array will be, so...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header containing your name and a brief description of the program Output prompting the user for the following inputs: Name as a string Length of a rectangle as a double Width of a rectangle as a double Length of a square as an int After accepting user input, the program outputs the following: User name Area of a rectangle with dimensions matching the inputs Area...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT