Question

Finish the code below (using bubble sort) to sort the datafile in ascending order. each number...

Finish the code below (using bubble sort) to sort the datafile in ascending order. each number should stop after "t"

#include

#include

#include

int main()

{

char letter[1400];

int length;

FILE *fptr;

if ((fptr = fopen("datafile1.txt","r")) == NULL)

{

printf("Error! opening file");

return (1);

}

if (fgets(letter,5000,fptr) != NULL)

{

printf("The string is in the file is\n\n");

puts(letter);

}

fclose(fptr);

printf("\n\n");

length = strlen(letter);

sortascending(length,letter);

puts(letter);

return 0;

}

***datafile1.txt***
1804289383t846930886t681692777t1714636915t1957747793tn424238335t719885386t1649760492t596516649t1189641421tn1025202362t1350490027t783368690t1102520059t2044897763tn1967513926t1365180540t1540383426t304089172t1303455736tn35005211t521595368t294702567t1726956429t336465782tn861021530t278722862t233665123t2145174067t468703135tn1101513929t1801979802t1315634022t635723058t1369133069tn1125898167t1059961393t2089018456t628175011t1656478042tn1131176229t1653377373t859484421t1914544919t608413784tn756898537t1734575198t1973594324t149798315t2038664370tn1129566413t184803526t412776091t1424268980t1911759956tn749241873t137806862t42999170t982906996t135497281tn511702305t2084420925t1937477084t1827336327t572660336tn1159126505t805750846t1632621729t1100661313t1433925857tn1141616124t84353895t939819582t2001100545t1998898814tn1548233367t610515434t1585990364t1374344043t760313750tn1477171087t356426808t945117276t1889947178t1780695788tn709393584t491705403t1918502651t752392754t1474612399tn2053999932t1264095060t1411549676t1843993368t943947739tn1984210012t855636226t1749698586t1469348094t1956297539tn

update** I'm supposed to write a code that scans a file (datafile1.txt) and sorts it in ascending order and the numbers have to be cut after every t. so far what I have is

#include <stdio.h>

#include <stdlib.h>

#include <stdio.h>

int main()

{

int array[100], n, c, d, swap, length;

char letter[1400];

FILE *fptr;

if ((fptr = fopen("datafile1.txt","r")) == NULL)

{

printf("Error! opening file");

return (1);

}

if (fgets(letter,5000,fptr) != NULL)

{

printf("The string is in the file is\n\n");

puts(letter);

}

fclose(fptr);

printf("\n\n");

length = strlen(letter);

sortascending(length,letter);

puts(letter);

for (c = 0; c < n; c++)

scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)

{

for (d = 0 ; d < n - c - 1; d++)

{

if (array[d] > array[d+1]) /* For decreasing order use < */

{

swap = array[d];

array[d] = array[d+1];

array[d+1] = swap;

}

}

}

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)

printf("%d\n", array[c]);

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main()

{

int array[100], n, c, d,k, swap, length;

char letter[1400],temp[100];

FILE *fptr;
//unable to open the file
if ((fptr = fopen("d://datafile1.txt","r")) == NULL)

{

printf("Error! opening file");

return (1);

}

//read the data from file
while(fgets(letter,5000,fptr) != NULL)

{

printf("The string is in the file is\n\n");

puts(letter);

}
//close the file
fclose(fptr);

printf("\n\n");

d=0;//d will count the number of number stored in array
k=0;
//loop will execute till the end of letter
for(c=0;letter[c]!='\0';c++)
{
   if(letter[c]=='t') //condition for t
   {
       temp[k]='\0'; //assign null to the end of temp
       array[d]=atoi(temp);//store the temp by converting to integer
       d++; //increment the value of d
       //c++;
       strcpy(temp,"\0");//erase the value of temp
       k=0;//assign 0 to k for temp to store another word
       }
   else
   {
      temp[k]=letter[c]; //copy the characters to temp
      k++;//increase the index
   }  
  
}
//logic to store the last number
temp[k]='\0';
array[d]=atoi(temp);
d++;
//print the numbers stored in array
for (c = 0; c < d; c++)

printf("\n %d ", array[c]);
n=d;//assign the value of d to n as the sizeof array
//logic for bubble sort
for (c = 0 ; c < n-1; c++)

{

for (d = 0 ; d < n-c-1; d++)

{

if (array[d] > array[d+1]) /* For decreasing order use < */

{

swap = array[d];

array[d] = array[d+1];

array[d+1] = swap;

}

}

}
//print the sorted array
printf("\n\nSorted list in ascending order:\n");

for (c = 0; c < n; c++)

printf("%d\n", array[c]);

return 0;

}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Finish the code below (using bubble sort) to sort the datafile in ascending order. each number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In which line of the following code the first error is appeared (if any)? #include 2...

    In which line of the following code the first error is appeared (if any)? #include 2 int main() <stdio.h> 4 file p: 6 fopen("newname . txt", "rb"); *p if = (P NULL) 8 perror( "Error opening file") 9 else f while (fgetc(p)- eof) 12 13 if (feof(p)) 14 15 else 16 17 Fclose(p) 18 19 20 return e; 21 printf("%d\n", n); puts("End-of-File was not reached.) 23 O 10 0 17

  • Write a C Program that uses file handling operations of C language to copy the contents...

    Write a C Program that uses file handling operations of C language to copy the contents of a file called “original.dat” to another file called “copy2.dat”. Here, you will read and write from/to the files line by line with the help of fgets() and fputs() functions. Sample file “original.dat” has been provided. Please note that the new file “copy2.dat” should have exactly same contents as in “original.dat”. #include <stdio.h> int main() { FILE *fptr1, *fptr2; if((fptr1=fopen("original.dat","r"))==NULL) printf("\n Error opening File");...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • My question is listed the below please any help this assignment ; There is a skeleton...

    My question is listed the below please any help this assignment ; There is a skeleton code:  copy_file_01.c #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { char ch ; FILE *source , *target;    if(argc != 3){ printf ("Usage: copy file1 file2"); exit(EXIT_FAILURE); } source = fopen(argv[1], "r"); if (source == NULL) { printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } target = fopen(argv[2], "w"); if (target == NULL) { fclose(source); printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } while ((ch...

  • Hardware Inventory – Write a database to keep track of tools, their cost and number. Your...

    Hardware Inventory – Write a database to keep track of tools, their cost and number. Your program should initialize hardware.dat to 100 empty records, let the user input a record number, tool name, cost and number of that tool. Your program should let you delete and edit records in the database. The next run of the program must start with the data from the last session. This is my program so far, when I run the program I keep getting...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • C Programming I was given this code to display the following as the output but it...

    C Programming I was given this code to display the following as the output but it does not seem to work. what is wrong? or can someone guide me through the steps? thanks! I have created the txt file named myfile.txt but the program will not compile. please help. I am forced to use Microsoft visual studio. This program makes a duplicate copy of a file (reads input from a file and write output to another file) 1 #include <stdio.h>...

  • Convert this code from C to C++ include <stdio.h> include <locale.h> void filtre (double x. double yll, double yol, int , int N) int Nn-10000 int main) setlocale (LC ALL,") double...

    Convert this code from C to C++ include <stdio.h> include <locale.h> void filtre (double x. double yll, double yol, int , int N) int Nn-10000 int main) setlocale (LC ALL,") double x(Nm) , y [ Nm],yo Nml®(0.0); int m; FILE dosyal-fopen("D:/veri/673.txtr FILE dosya2-fopen("D:/veri/data2.txt int i-0 while( feof (dosyal)) fscanf (dosyal, " ",xi sytil) fclose (dosyal) int Nij printf("Pencere Genişligi scanf() 3 filtre(x,y.Yo,m,N) for(int k-0keNkt+)fprintf (dosya2, 10.41 10.411 0.41fn",x[k],y[k].yo[k]) fclose(dosya2) void filtre (double x double y. double yoll, int m, int...

  • #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <unistd.h> #include <pthread.h> pthread_mutex_t mtx; // used by each...

    #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <unistd.h> #include <pthread.h> pthread_mutex_t mtx; // used by each of the three threads to prevent other threads from accessing global_sum during their additions int global_sum = 0; typedef struct{ char* word; char* filename; }MyStruct; void *count(void*str) { MyStruct *struc; struc = (MyStruct*)str; const char *myfile = struc->filename; FILE *f; int count=0, j; char buf[50], read[100]; // myfile[strlen(myfile)-1]='\0'; if(!(f=fopen(myfile,"rt"))){ printf("Wrong file name"); } else printf("File opened successfully\n"); for(j=0; fgets(read, 10, f)!=NULL; j++){ if (strcmp(read[j],struc->word)==0)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT