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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
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
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;
}

steps

Step by step

Solved in 2 steps with 1 images

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