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

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...
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...
......C++ PROGRAM.... Teacher would like us to split this program into a header file(filename.h), and a...
......C++ PROGRAM.... Teacher would like us to split this program into a header file(filename.h), and a .cpp file(filename.cpp). He gave us all the code, we just need to split it up. I do not quite understand how to do it. Please send output screenshot to show validation. #include <iostream> using namespace std; //public class class Account{ public: //instance variables double amount;    //creates account and sets amount with users account set-up value Account(double a){ amount = a; }    //adds...
C++ code Write a program to illustrate how to use the temporary class. Your program must...
C++ code Write a program to illustrate how to use the temporary class. Your program must contain statements that would ask the user to enter data of an object and use the setters to initialize the object. Use three header files named main.cpp, temporary.h, and temporaryImp.cpp An example of the program is shown below: Enter object name (rectangle, circle, sphere, or cylinder: circle Enter object's dimensions: rectangle (length and width) circle (radius and 0) sphere (radius and 0) rectangle (base...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT