Write a C program that takes as input two lists of integers (in increasing order
ID: 3619993 • Letter: W
Question
Write a C program that takes as input two lists of integers (in
increasing order) and creates combines the numbers in third list
of ordered numbers. The two input lists shall be read from
separate text files and the combined list shall be written to an
output file.
Specifications:
1. The files that the program uses will have the following names:
“Data1.txt”, “Data2.txt”, and “Merged.txt”. Note: Your
program will be used with the grader’s data files. If your
program does not use the correct file names, then it will not
run successfully.
2. The program will check for failures opening the files and will
provide a graceful exit if a failure exists.
3. The program will have appropriate programmer-defined
functions so that the main function provides a highlight of the
program.
4. The program will have a comment block at the top of the file
with your name, the course number, the date the program
was completed, and a brief description of the program.
Note: You are not allowed to read all of the data into a single array
and use a sorting function. Try to think of a more efficient approach.
this is what I have so far. i created data1 and data2 and put some random number in them. the program will merge the 2 files but wont put the numbers in order. any help please?!
#include <stdio.h>
#include <stdlib.h>
void file_merge(FILE *,FILE *,FILE *);
int main()
{/*Name:
Course Number:
Date:
Program description:
*/
int i,j;
FILE *data1,*data2,*merged;
data1 = fopen("Data1.txt","r");
if(data1 == NULL)
{printf(" Input file data1 did not open, please check it. ");
getch();
}
data2 = fopen("Data2.txt","r");
if(data2 == NULL)
{printf(" Input file data2 did not open, please check it. ");
getch();
}
merged=fopen("Merged.txt","w");
if(merged == NULL)
{printf(" Input file merged did not open, program terminating. ");
exit(1);
}
file_merge(data1,data2,merged);
fclose(data1);
fclose(merged);
fclose(data2);
exit(1);
return 0;
}
void file_merge(FILE *data1, FILE *data2, FILE *merged)
{int> int n,m,x;
fscanf(data1,"%d",&n);
fscanf(data2,"%d",&m);
while(one+two==2)
{
if(n<m)
{fprintf(merged,"%d ",n);
x=fscanf(data1,"%d",&n);
if(x<0)
> }
else
{fprintf(merged,"%d ",m);
x=fscanf(data2,"%d",&m);
if(x<0)
two=0;
}
}
if(two==1)
{ fprintf(merged,"%d ",m);
x=fscanf(data2,"%d",&m);
while(x>0)
{fprintf(merged,"%d ",m);
x=fscanf(data2,"%d",&m);
}
}
else
{ fprintf(merged,"%d ",n);
x=fscanf(data1,"%d",&n);
while(x>0)
{fprintf(merged,"%d ",n);
x=fscanf(data1,"%d",&n);
}
}
}
Explanation / Answer
#include #include #include void file_merge(FILE *,FILE *,FILE *); struct node { int num; struct node *next; }*first1,*first2; int main() {/*Name: Course Number: Date: Program description: */ int i,j; FILE *data1,*data2,*merged; data1 = fopen("Data1.txt","r"); if(data1 == NULL) { printf(" Input file data1 did not open, please check it. "); return -2; } data2 = fopen("Data2.txt","r"); if(data2 == NULL) {printf(" Input file data2 did not open, please check it. "); return -1; } merged=fopen("Merged.txt","w"); if(merged == NULL) {printf(" Input file merged did not open, program terminating. "); exit(1); } file_merge(data1,data2,merged); fclose(data1); fclose(merged); fclose(data2); exit(1); return 0; } void insert(struct node **first,int n) { struct node *ptr,*prev,*ptr1; if(*first == NULL) { *first = (struct node *) malloc(sizeof(struct node)); ptr = *first; ptr->num = n; ptr->next = NULL; return; } ptr1= (struct node *) malloc(sizeof(struct node)); ptr1 -> num = n; ptr1 -> next = NULL; prev = ptr = *first; if(ptr -> num > n) { ptr1-> next = *first; *first = ptr1; return; } while(ptr != NULL && ptr->num next; } if(ptr == NULL) { prev->next = ptr1; return; }else { prev->next = ptr1; ptr1->next = ptr; return; } } void file_merge(FILE *data1, FILE *data2, FILE *merged) { int n;struct node *ptr,*ptr1,*ptr2; while(fscanf(data1,"%d",&n) > 0) { insert(&first1,n); } while(fscanf(data2,"%d",&n) > 0) { insert(&first2,n); } ptr1 = first1; ptr2 = first2; while(ptr1 != NULL && ptr2 != NULL) { if(ptr1->num num) { fprintf(merged,"%d ",ptr1->num); ptr1 = ptr1->next; } else { fprintf(merged,"%d ",ptr2->num); ptr2 = ptr2->next; } } while(ptr1 != NULL) { fprintf(merged,"%d ",ptr1->num);ptr1=ptr1->next; } while(ptr2 != NULL) { fprintf(merged,"%d ",ptr2->num);ptr2=ptr2->next; } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.