Questions: 1. see if you can improve the following program segments through reor
ID: 3559382 • Letter: Q
Question
Questions:
1. see if you can improve the following program segments through reorganization. First analysis the code to determine the function actually being performed. Then recode that function as clearly and simply as possible. (i.e. without the use of break or continue)
a) while (d) {
if (e) {
continue;
}
f ();
}
Second program segment:
b) do {
if (!a) {
continue;
}
else {
b ( );
c( );
}
} while (a);
2. write a section of code that attempts to open file f.txt for input and, if the file does not exist, generate an error message and aborts the program.
3. write a section of code that opens file diary.txt to add new data data to the file without modifying the data that might already been in the file.
4. what could happen if you attempt to open a file for input and you do not check to see whether or not the file exists.
Explanation / Answer
a)- in this inside while loop if(e) condition is being evaluated and continue statement is used to go to check the condition of loop. we can recode it as following without using continue,
while(d)
{
if( ! e)
f();
}
this will do the same thing
b)-
similarly,
we can simplify it as,
do
{
if( a )
{
b();
c();
}
}while(a);
2. code segment that attempts to open file f.txt for input and, if the file does not exist, generate an error message and aborts the program.
function( FILE *fp)
{
fp= fopen(f.txt, 'r');
if(fp==NULL)
{
printf("Unable to open the file ");
exit();
}
}
3. code that opens file diary.txt to add new data data to the file without modifying the data that might already been in the file.
function(FILE *fp)
{
fp= fopen(diary.txt, 'w');
if(fp==NULL)
{
printf("Unable to open the file ");
exit();
}
// now reach to the end of file and add data there
while(fp !=EOF)
fp++;
// now write your data here.
}
4. if we try to open a file in write mode and the file does not exit then a new file with the given name will be created. and data will be written into that file.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.