THE CODE MUST BE IN C. I FINISHED SOME OF THE CODE STEP THATS WHY ITS MISSING. I AM GOING TO ATTTECHED MY CODE PLEASE ADD OR MODIFY WITH NEW CODE ON THAT.
THE CODE MUST BE IN C. I FINISHED SOME OF THE CODE STEP THATS WHY ITS MISSING. I AM GOING TO ATTTECHED MY CODE PLEASE ADD OR MODIFY WITH NEW CODE ON THAT.
THANKS.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct node
{
int number;
struct node *next_ptr;
}
NODE;
void AddNodeToLL(int Number, NODE **LinkedListHead)
{
NODE *New_Node = malloc(sizeof(NODE));
New_Node -> number = Number;
New_Node -> next_ptr = NULL;
if (*LinkedListHead == NULL)
{
*LinkedListHead = New_Node;
}
else
{
NODE *Temp_Node = *LinkedListHead;
while (Temp_Node -> next_ptr != NULL)
{
Temp_Node = Temp_Node -> next_ptr;
}
Temp_Node -> next_ptr = New_Node;
}
}
void ReadFileIntoLL(int argc, char *argv[], NODE **LLH)
{
if (argc < 2)
{
printf("File must be provided on Command Line...exiting\n");
exit(0);
}
FILE *fptr = fopen(argv[1], "r");
if (fptr == NULL)
{
printf("File can not read\n");
exit(0);
}
char read_str [50];
int count = 0;
int sum = 0;
while (fgets(read_str, 50, fptr) != NULL)
{
int number = atoi(read_str);
count++;
sum += number;
AddNodeToLL(number, LLH);
}
printf("\n %d records were read for a total sum of %d\n", count, sum);
}
void PrintLL(NODE *LLH)
{
if (LLH == NULL)
{
printf("The file has NO Linked List.\n");
return;
}
NODE *Temp_Node = LLH -> next_ptr;
while (Temp_Node != NULL)
{
Temp_Node = Temp_Node -> next_ptr;
}
}
void FreeLL(NODE **LLH)
{
if (*LLH == NULL)
return;
NODE *Temp_Node = *LLH;
NODE *Delete_Node = NULL;
while (Temp_Node != NULL)
{
Delete_Node = Temp_Node;
Temp_Node = Temp_Node -> next_ptr;
if (Delete_Node != NULL)
{
free(Delete_Node);
}
}
}
int main(int argc, char *argv[])
{
NODE *LLH = NULL;
/* capture the clock in a start time */
clock_t start, end;
start = clock();
ReadFileIntoLL(argc, argv, &LLH);
/* capture the clock in end time */
end = clock();
printf("\n %ld tics to write the file into the linked list\n", end - start);
/* capture the clock in a start time */
start = clock();
PrintLL(LLH);
/* capture the clock in an end time */
end = clock();
printf("\n %ld tics to print the linked list\n", end - start);
/* capture the clock in a start time */
start = clock();
FreeLL(&LLH);
/* capture the clock in an end time */
end = clock();
printf("\n %ld tics to free the linked list\n", end - start);
return 0;
}
Step 1 Create a program than can run Insertion Sort on a hardcoded small array. You can copy the code from the lecture slides or copy it from an Internet source. The code MUST BE in C. If you are copying from an Internet source, make sure you are getting INSERTION SORT and not some other version of sort. Your assignment will not be graded and you will be assigned a grade of 0 if the code you submit is not in C and is not insertion sort.
Use malloc() with the number of lines you counted in the file to dynamically create an int array on the heap. Now read through the file again and put each line (each line is a number) in an array element. This process will allow you to create arrays of various sizes. You will be running Insertion Sort on this array and then you will free () the memory since you used malloc() to reserve the memory (don't cause a memory leak). Step 6 Add a function to print the array. Here is a suggested version - you are not required to use this exact code but your version must output the same - one number per line. void PrintArray (int ArrayToPrint[], int SizeAP) { } int i; for (i = 0; i < SizeAP; i++) printf("%d\n", ArrayToPrint); Add conditional compile statements around the call to this function in main () where it is called before and after sorting the array.
#ifdef PRINTARRAY PrintArray (AP, elements); #endif Using TestFile.txt, run your new version and confirm that it sorts correctly and uses the file as input by compiling your code with the conditional compile. gcc Code2_xxxxxxxxxx.c -D PRINTARRAY
Sample output The numbers shown in this SAMPLE output are just examples - you should not worry about getting the exact same numbers. gcc Code2 1000074079.c -D PRINTARRAY ./a.out TestFile.txt 74079 21259 59758 25398 4381 62060
4381 21162 21259 25398 32541 59743 59758 61981 62060 74079 Processed 10 records Insertion Sort Tics = 2 Notice that the array is printed BEFORE and AFTER the insertion sort runs.
Compile the program without the array printing turned on and running it with a file containing 1,000,000 records. gcc Code2 1000074079.c ./a.out N1000000.txt Insertion Sort = 694247174 Tics
THE CODE MUST BE IN C. I FINISHED SOME OF THE CODE STEP THATS WHY ITS MISSING. I AM GOING TO ATTTECHED MY CODE PLEASE AD
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am