-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
88 lines (67 loc) · 2.63 KB
/
main.c
File metadata and controls
88 lines (67 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include "./include/generateRandomArray.h"
#include "./include/graphPlot.h"
#include "./include/executionTime.h"
#include "./algorithms/mergeSort.h"
#include "./algorithms/bubbleSort.h"
#include "./algorithms/quickSort.h"
#include "./algorithms/insertionSort.h"
#include "./algorithms/selectionSort.h"
// Global Config Pointers Array
int ConfigVar[6];
int *ConfigPointers = ConfigVar;
// Function to read the configuration file and assign values to global integer pointers
int ReadConfigFile()
{
// Open the file for reading
FILE *file;
file = fopen("config.txt", "r");
// Check if the file is successfully opened
if (file == NULL)
{
printf("main.c: Error opening the config.txt File.\n");
return 1; // Return an error code
}
// Read each line from the file
fscanf(file, "NumberofIterations: %d\n", &ConfigVar[0]);
fscanf(file, "StartIterationFromSize: %d\n", &ConfigVar[1]);
fscanf(file, "IterationGap: %d\n", &ConfigVar[2]);
fscanf(file, "EndIterationTillSize: %d\n", &ConfigVar[3]);
fscanf(file, "RandomArrayUpperBound: %d\n", &ConfigVar[4]);
fscanf(file, "RandomArrayLowerBound: %d\n", &ConfigVar[5]);
// Close the file
fclose(file);
return 0;
}
int main()
{
// Read the configuration file
ReadConfigFile();
int ArraySize = ((*(ConfigPointers + 3) - (*(ConfigPointers + 1))) / (*(ConfigPointers + 2)) + 1);
// Create an array to store the execution time of each iteration
double *ExecutionTimeArray = (double *)malloc(ArraySize * sizeof(double));
if (ExecutionTimeArray == NULL)
{
printf("main.c: Memory allocation failed.\n");
return 1; // Return an error code
}
printf("Sorting Algorithms Calculation and Graph Plotting Started!\n");
MergeSort(ConfigPointers, ExecutionTimeArray);
PlotGraph(ExecutionTimeArray, ConfigPointers, "MergeSort");
BubbleSort(ConfigPointers, ExecutionTimeArray);
PlotGraph(ExecutionTimeArray, ConfigPointers, "BubbleSort");
QuickSort(ConfigPointers, ExecutionTimeArray);
PlotGraph(ExecutionTimeArray, ConfigPointers, "QuickSort");
InsertionSort(ConfigPointers, ExecutionTimeArray);
PlotGraph(ExecutionTimeArray, ConfigPointers, "InsertionSort");
SelectionSort(ConfigPointers, ExecutionTimeArray);
PlotGraph(ExecutionTimeArray, ConfigPointers, "SelectionSort");
// Free the memory allocated to the ExecutionTimeArray
free(ExecutionTimeArray);
printf("\nSorting Algorithms Calculation and Graph Plotting Completed!\nPress any Key to Exit...");
getchar();
printf("\nSandesh!");
return 0;
}