Task1
Write a program that joins the two sorted files to an output file such that the resulting file is also sorted. Your program should read the names of the files to be joined from the command line, as well as the name of the file which will store the joined content, as shown below:
ProgramName filename1 filename2 filenameOut
The files that your program will join contain floating point numbers sorted in increasing order. Your program should read from the input files and write to the output file appropriately so that the resulting file will also be sorted in increasing order.
In case one of the input files are not sorted in increasing order, your program should complain and exit, displaying an appropriate message.
You can test your code with different data files that are supplied.
Task2
Write a program that reads a given file containing floating point numbers and appends a summary line to the end of the file showing the count of numbers, the maximum number, the minimum number and a flag showing whether the file is sorted or not.
For data1.txt, the line will be like:
# 4 6.229683 0.586399 increasing
For data2_us.txt, the line will be like:
# 7 12.158343 0.279828 unsorted
The program should check whether last line of the file starts with the ‘#’ sign and update the line, in case it exists. Thus, after running the program with the same input file multiple times, there should exist only one summary line at the end of the file that starts with the ‘#’ sign.
Code1
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
#include <stdio.h> #include <stdlib.h> int main(int argc, const char * argv[]) { const char *fileName , *fileName2 , *fileName3; FILE *fptr , *fptr2 ,*fptr3 ; int i = 0,k = 0,k1 = 0,q = 0; int x, y; float arr[20000]; float t0, temp; if (argc != 4) { printf ("3 Filename is not given please try again"); printf("Usage: %s filename\n", argv[0]); return EXIT_FAILURE; } fileName = argv [1]; fileName2 = argv [2]; fileName3 = argv [3]; fptr = fopen(fileName,"r"); if(fptr == NULL){ printf("Error!! Cannot open file: %s \n", fileName ); return EXIT_FAILURE;} else { printf("First file opened successfully : %s \n", fileName); while(!feof(fptr)) { if(1 != fscanf(fptr, "%f" ,&arr[i])) { break; } printf("%f\n",arr[i]); ++i; } } q=i; fptr2 = fopen(fileName2,"r"); if(fptr2 == NULL){ printf("Error!! Cannot open file: %s \n", fileName2 ); return EXIT_FAILURE;} else { printf("Second file opened successfully: %s \n", fileName2); while(!feof(fptr2)) { if(1 != fscanf(fptr2, "%f" ,&arr[i])){ break; }; printf("%f\n",arr[i]); ++i; } } for(x=0; x<q-1; x++) { if(arr[x]>arr[x+1]) k++; } for(x=q+1; x<i-1; x++) { if(arr[x]>arr[x+1]) k1++; } if(k!=0){ printf("First file is not sorted Filename: %s \n", fileName); printf("Program will sort this array\n"); } if(k1!=0){ printf("Second file is not sorted Filename: %s \n", fileName2); printf("Program will sort this array\n"); } for(x=0; x<i; x++) { for(y=x+1; y<i; y++) { if(arr[y] < arr[x]) { temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } } } fptr3 = fopen (fileName3, "w+"); for(x=0; x<i; x++) { fprintf(fptr3,"%f\n", arr[x]); } fclose(fptr3); fclose(fptr2); fclose(fptr); printf("\nElements of array in sorted check this file: %s ",fileName3); return 0; } |
Code2
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 89 90 91 92 93 94 95 |
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, const char * argv[]) { const char *fileName ; FILE *fptr, *freplace ; int i = 0; int x, k = 0, q = 0,y,z; float arr[20000]; float t0, temp; int c; char s[100]; float max = arr[0]; float min = arr[0]; fileName = argv[1]; if (argc =! 2) { printf("Usage: %s filename\n", argv[0]); return EXIT_FAILURE; } fptr = fopen(fileName,"r+"); if(fptr == NULL){ printf("Error!! Cannot open file: %s \n", fileName ); return 1; } else { printf("File opened successfully\n"); while((c = getc(fptr)) != '#' && c != EOF) { ungetc(c, fptr); if(1 != fscanf(fptr, "%f" ,&arr[i])){ q=1; } ++i; } } if (c == '#' || q == 1) {i=i-1; } for(x=0;x<=i-2;x++) { if(arr[x]>arr[x+1]) k++; } if (k==0) { strcat(s, "Sorted Increasing"); } else { strcat(s, "Unsorted"); } min = arr[0]; for (y = 0; y < i; y++) { if (arr[y] > max) { max = arr[y]; } else if (arr[y] < min) { min = arr[y]; } } if (c == '#' || q == 1) { freplace = fopen("reply.txt", "w"); for (z=0; z<=i-1; z++) { fprintf(freplace, "%f\n",arr[z]); } fprintf(freplace, "#Maximum value is:%f Minimum value is:%f Count of numbers:%i Numbers are %s",max,min,i,s); fclose(fptr); fclose(freplace); remove(fileName); rename("reply.txt", fileName); } else{ fprintf(fptr, "\n#Maximum value is:%f Minimum value is:%f Count of numbers:%i Numbers are %s",max,min,i,s); fclose(fptr); } return 0; } |