In: Statistics and Probability
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:
10 Epsilon 15 Eta 20 Beta
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.