Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question

Write in C Language.

Make any program regarding data input, save data, read data, search data and sort data (file, sorting and searching)

Expert Solution
Check Mark
Solution with detail explanation using comments


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{

// write data input numbers to file
FILE* wfptr;

wfptr = fopen("file.txt", "w");

if (wfptr == NULL) {
printf("ERROR");
exit(1);
}

for (int i = 0; i < 5; i++) {

int val;
scanf("%d", &val);
// storing data to file
fprintf(wfptr, "%d ", val);
}

// closing the file
fclose(wfptr);

// data type for calculating time
clock_t starttime, endtime;

// variable for calculating total time of execution
double totaltime;
int i = 0, j, n = 0, min, index;

// declaring array
int arr[5];


// declaring file pointer
FILE* fptr;

// opening the integer file.
fptr = fopen("file.txt", "r");


// scanning integer from file to array
while (fscanf(fptr, "%d", &arr[i]) == 1)
{
// count elements from file //
n++;
i++;
}


starttime = clock();
printf("start time to sort : %f\n", (float)starttime);

// sort data
for (i = 0; i < n - 1; i++) {
min = arr[i];
for (j = i + 1; j < n; j++) {
if (arr[j] < min) {
min = arr[j];
index = j;
}
}

int temp = arr[i];
arr[i] = min;
arr[index] = temp;
}
// selection sort logic ends here

// calculating clock when sorting ends
endtime = clock();
printf("%f\n", (float)endtime);

totaltime = ((double)(endtime - starttime)) / CLOCKS_PER_SEC;

// printing the sorted array...
for (i = 0; i < n; i++)
printf("%d ", arr[i]);

printf("\n\nendtime after sort: %f\n", (float)endtime);
printf("\n\ntotal time of execution = %f", totaltime);

return 0;
}

Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY