Problem 1 (35 points): Jumbled File Fix Your boss has given you a file, named Sc
ID: 3587411 • Letter: P
Question
Problem 1 (35 points): Jumbled File Fix Your boss has given you a file, named Scrambled File.bxt, that is completely jumbled. The file is actually a mixture of two separate files, one of which is supposed to be strictly numerical (i.e. contains only digits 0-9), and the other contains only non-numerical characters. Write a C program that reads the file Scrambled File.txt, one character at a time. If the character is non-numerical then print this character to a file called Unscrambled.txt. If the character is a numeric digit then your boss would like to know the largest product of 4 consecutive digits if the data. For example if the file Scrambled File.txt, contains 15H8e29211387406234 482T009312423m234 then the file Unscrambled.txt, would contain the message Hello Tim and the following message would be printed to the screen The largest product of a 4-digit sequence is 5·8" 219 #720 Note that the character '0' has an ASCII value of 48, and the character '9 has an ASCII value of 57. Write a function isdig as follows int isdig(char c, int Current_Digit) ( I/This function returns 0 if the character c is non-numerical, or lfreturns 1 if the character c is a digit (ASCIl value 48 to 57) Il If a 1 is returned the Current Digit contains the integer number // lf, for example. C = 7. then·Current-Digit # 7, and a l is returned // lf, for example, c #·+, then a o is returned llYour code goes here Use the function Update Samples to help keep track of old values void Update Samples(int Current Digit irt .One Frames Old, int·Two Frames Old, int "Three Frames Old)( Three Frames Old Two Frames Old: Two Frames Old - 'One Frames Öld: One Frames Old - Current_Digit; return For this problem, if your program is working, the Unscrambled.txt file will contain the pattern that you can read and the largest product of 4-digit sequence (from Scrambled File.txt) is 5832Explanation / Answer
#include<stdio.h>
// Function prototypes
int isDigit(char c,int *Current_Digit);
void Update_Samples(int Current_Digit,int *One_frame_Old,int *Two_frames_Old,int *Three_frames_Old);
/**********************************************************************************************
* Name :
* main()
* Purpose :
* Read a file "Scrambled_File.txt" one character at a time.
* If character is non-numerical then write this to a file "Unscrambled.txt".
* And if character is numerical then finds the largest product of 4-digit sequence.
*
***********************************************************************************************/
int main()
{
FILE *fp1,*fp2;
char ch;
int mul=0,max=0,final_curr,final_first,final_second,final_third,flag;
int curr=0,first=0,second=0,third=0;
fp1 = fopen("Scrambled_File.txt","r");
fp2 = fopen("Unscrambled.txt","w");
do
{
ch = (char)fgetc(fp1);
flag = isDigit(ch,&curr);
if(flag)
{
curr = atoi(&ch);
Update_Samples(curr,&first,&second,&third);
mul = curr*(first)*(second)*(third);
if(mul > max)
{
max=mul;
final_curr = curr;
final_first = first;
final_second = second;
final_third = third;
}
}
else
{
fputc(ch,fp2);
}
} while(ch != EOF);
printf("The largest product of 4-digit sequence is %d * %d * %d * %d = %d ",final_curr,final_first,final_second,final_third,max);
return 0;
}
/**********************************************************************************************
* Name :
* Update_Samples()
* Purpose :
* Keep track of old values.
* Input :
* Current digit and three previous sequenced digits
*
***********************************************************************************************/
void Update_Samples(int Current_Digit,int *One_frame_Old,int *Two_frames_Old,int *Three_frames_Old)
{
*Three_frames_Old = *Two_frames_Old;
*Two_frames_Old = *One_frame_Old;
*One_frame_Old = Current_Digit;
return;
}
/**********************************************************************************************
* Name :
* isDigit()
* Purpose :
* To determine whether the cutrrent character is digit or non-digit.
* Input :
* Laest read character and the latest digit.
* Output :
* 0 if character is non-numerical
* 1 if character is numerical
*
***********************************************************************************************/
int isDigit(char c,int *Current_Digit)
{
if (c < 48 || c > 57)
{
return 0;
}
else
{
return 1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.