Question

In: Computer Science

There are at least 10 errors in the following C program. For each error you can...

There are at least 10 errors in the following C program. For each error you can find you should list the location of the error, describe what the error is, and state how the error can be fixed (write updated code for that line if necessary). Each error you find is worth 1.5 marks. Note that missing brackets, braces, etc count as only one error, even though the missing brackets may occur at two places.

The program is supposed to perform the following task: Read a list of names and heights from a file called “heights.txt”. Each line of the file contains a single name (one word, 50 chars max) and an integer value representing that person’s height in cm. These names and heights should be stored in two arrays, with a maximum size of 1000. Once this is done, the user should be asked to enter a minimum and maximum height (in cm), and the program should display all people whose heights fall between those values (inclusive). If a zero is entered for either criteria, then that criteria is not checked. For example, if the user enters 0 for the maximum height, then there is no maximum value and all people above the minimum height will be listed. If both values are zero then all people will be listed. The names and heights should be displayed one per line, with the name first followed by a colon, then the height in cm. At the end of the program the total number of people matching the search criteria should be displayed as well.

1 #include <stdio.h>
2
3 int main ( void ){
4
5    int heights[1000], i, n = 0, total = 0, min, max ;
6       char names[50][1000] ;   
7 file *fp ;
8
9       fp = fopen ( "heights.txt", "w" ) ;
10       if ( fp == NULL ){
11          printf ( "Cannot open heights.txt for reading\n" ) ;
12          exit ( -1 ) ;
13       }
14       while (n<1000 && scanf("%c %d",names[n],&heights[n])!=EOF){
15          n++ ;   
16       }
17
18       printf ( "Enter minimum height to display: " ) ;
19 scanf ( "%d", &min ) ;
20       printf ( "Enter maximm height to display: " ) ;
21       scanf ( "%d", &max ) ;
22
23 for ( i = 0 , i <= n , i++ ){
24 if ((heights[i]>=min || min==0)|| (heights[i]<=max || max==0)){
25 // display the person and height
26 printf ( "%c: %dcm\n", names[i][50], heights[i] ) ;
27   }
28 }
29 printf ( "Total matches: %d\n", total ) ;
30 return (0);
31 }

Solutions

Expert Solution

Here is the answer for your question in C Programming Language.

Kindly upvote if you find the answer helpful.

###############################################################

LIST OF ERROR WITH SOLUTIONS :

  • Line No. 6 - ERROR - 1 :char names[50][1000];
       It is given that max length of name will be 50 characters but the declaration sets it to 1000
       Solution : char names[1000][50];
  • Line No. 7 - ERROR - 2 : name 'file' in 'file *fp;'
       Solution : file object should be create with data type 'FILE' (all uppercase letters)
        file *fp (commented the error line);
  • Line No. 9 - ERROR - 3 : fopen ( "heights.txt", "w" ) ; - mode of the file
        When we have to read the file the mode should be 'r' but not 'w'
        Solution : fopen ( "heights.txt", "r" ) ;
  • Line No. 14 - ERROR - 4 : '%c' in 'scanf("%c %d",names[n],&heights[n])'
       "%c to read names will read only one character and not the entire name.
    Solution : Replace %c with %s
  • Line No. 14 - ERROR - 5 : scanf() - scanf function is used to read input from keyboard but not from files
    Solution : scanf should be replaces with fscanf()
  • Line No. 14 - ERROR - 6 : fscanf("%c %d",...) - in fscanf() function the first parameter should be the file object
    Solution : fscanf(fp,"%c %d",...)
  • Line No. 14 - ERROR - 7 : (fscanf(fp,"%s%d",names[n],&heights[n])!=EOF)
    fscanf should not be compared with EOF because fsacnf will return 'int' and it may not equal EOF
    Solution : replace EOF with -1
  • Line No. 23 - ERROR - 8 : In for loop,initialization,condition and increment/decrement operators
       should be seprated with a semi-colon but not comma.
       Solution : Replace comma with semicolon
  • Line No. 23 - ERROR - 9 : 'i<=n'
       As for loop started from 0 if i should be traversed to (n-1) i.e., no of elements - 1,otherwise one exta element will be displayed
       Solution : i<=n should be replaced with i < n
  • Line - 24 - ERROR - 10 : (....)|| (...)
            The operator '||' should be '&&'
            Solution : (heights[i]>=min || min==0)&& (heights[i]<=max || max==0)
  • Line No. 26 - ERROR - 10: names[i][50]
                It will displau 50th character of ith name but not full name
                Solution : names[i][50] should be replaced with names[i]
  • ERROR - 11 : Total should be incremented at this point to keep tracck how many persons fell under the condition
                Solution : write 'total++;' inside if condition

#############################################################################

CODE :

#include <stdio.h>
int main ( void ){
   int heights[1000], i, n = 0, total = 0, min, max ;
   // Line No. 6 - ERROR - 1 :char names[50][1000];
   //It is given that max length of name will be 50 characters but the declaration sets it to 1000
   //Solution : char names[1000][50];
char names[50][1000] ;   
  
//Line No. 7 - ERROR - 2 : name 'file' in 'file *fp;'
   //Solution : file object should be create with data type 'FILE' (all uppercase letters)
    //file *fp (commented the error line);
    FILE * fp;
   
    //Line No. 9 - ERROR - 3 : fopen ( "heights.txt", "w" ) ; - mode of the file
    //When we have to read the file the mode should be 'r' but not 'w'
    //Solution : fopen ( "heights.txt", "r" ) ;
//fp = fopen ( "heights.txt", "w" ) ; (commented the error line)
  
fp = fopen ("heights.txt", "r" ) ;
if ( fp == NULL ){
printf ( "Cannot open heights.txt for reading\n" ) ;
exit ( -1 ) ;
}
//Line No. 14 - ERROR - 4 : '%c' in 'scanf("%c %d",names[n],&heights[n])'
   //"%c to read names will read only one character and not the entire name.
// Solution : Replace %c with %s
// while (n<1000 && scanf("%c %d",names[n],&heights[n])!=EOF){ (commeneted error line)
  
//Line No. 14 - ERROR - 5 : scanf() - scanf function is used to read input from keyboard but not from files
//Solution : scanf should be replaces with fscanf()
//while (n<1000 && scanf("%s %d",names[n],&heights[n])!=EOF){ (commented error line)
  
//Line No. 14 - ERROR - 6 : fscanf("%c %d",...) - in fscanf() function the first parameter should be the file object
//Solution : fscanf(fp,"%c %d",...)
//while (n<1000 && fscanf("%s %d",names[n],&heights[n])!=EOF){ (Commented error line)
  
//Line No. 14 - ERROR - 7 : (fscanf(fp,"%s%d",names[n],&heights[n])!=EOF)
//fscanf should not be compared with EOF because fsacnf will return 'int' and it may not equal EOF
//Solution : replace EOF with -1
//while (n < 1000 && (fscanf(fp,"%s%d",names[n],&heights[n])!=EOF)){ (Commented the error line)
while (n < 1000 && (fscanf(fp,"%s %d",names[n],&heights[n])!=-1)){    
       n++ ;   
}
  
fclose(fp);
printf ( "Enter minimum height to display: " ) ;
scanf ( "%d", &min ) ;
printf ( "Enter maximm height to display: " ) ;
scanf ( "%d", &max ) ;
  
   //Line No. 23 - ERROR - 8 : In for loop,initialization,condition and increment/decrement operators
   //should be seprated with a semi-colon but not comma.
   //Solution : Replace comma with semicolon
   //for(i = 0, i < = n , i++ ) (commented the error line)
  
   //Line No. 23 - ERROR - 9 : 'i<=n'
   //As for loop started from 0 if i should be traversed to (n-1) i.e., no of elements - 1,otherwise one exta element will be displayed
   //Solution : i<=n should be replaced with i < n
   //for ( i = 0 ; i <= n ; i++ ){ (Commented error line)
  
    for ( i = 0 ; i < n ; i++ ){
        //Line - 24 - ERROR - 10 : (....)|| (...)
        //The operator should be '&&'
        //Solution : (heights[i]>=min && min==0)&& (heights[i]<=max || max==0)
        if ((heights[i]>=min || min==0) && (heights[i]<=max || max==0)){
            // display the person and height
            //Line No. 26 - ERROR - 11: names[i][50]
            //It will displau 50th character of ith name but not full name
            //Solution : names[i][50] should be replaced with names[i]
            //printf ( "%c: %dcm\n", names[i][50], heights[i] ) ;(commented error line)
            printf ( "%s: %dcm\n", names[i], heights[i] ) ;           
           
            //ERROR - 12 : Total should be incremented at this point to keep tracck how many persons fell under the condition
            //Solution : write 'total++;' inside if condition
            total++;
        }
    }
    printf ( "Total matches: %d\n", total ) ;
   return (0);
}

####################################################################

heights.txt

John 123
James 145
Henry 120
Albert 133

#################################################################

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

################################################################

heights.txt

########################################################################

OUTPUT :

Any doubts regardning this can be explained with pleasure :)


Related Solutions

There are at least 10 errors in the following C program. For each error you can...
There are at least 10 errors in the following C program. For each error you can find you should list the location of the error, describe what the error is, and state how the error can be fixed (write updated code for that line if necessary). Each error you find is worth 1.5 marks. Note that missing brackets, braces, etc count as only one error, even though the missing brackets may occur at two places. The program is supposed to...
There are at least 10 errors in the following C program. For each error you can...
There are at least 10 errors in the following C program. For each error you can find you should list the location of the error, describe what the error is, and state how the error can be fixed (write updated code for that line if necessary). Each error you find is worth 1.5 marks. Note that missing brackets, braces, etc count as only one error, even though the missing brackets may occur at two places. The program is supposed to...
Trial Balance Errors For each of the following errors, considered individually, indicate whether the error would...
Trial Balance Errors For each of the following errors, considered individually, indicate whether the error would cause the trial balance totals to be unequal. If the error would cause the trial balance totals to be unequal, indicate whether the debit or credit total is higher and by how much. If the debit and credit totals would be equal, enter zero ("0") in the amount box. a. The payment of an insurance premium of $7,450 for a three-year policy was debited...
For each of the following inventory errors occurring in 2018, determine the effect of the error...
For each of the following inventory errors occurring in 2018, determine the effect of the error on 2018's cost of goods sold, net income, and retained earnings using understated (U), overstated (O), or no effect (NE). Assume that the error is not discovered until 2019 and that a periodic inventory system is used. Ignore income taxes. Cost of Net Retained Goods Sold Income Earnings 1. Overstatement of ending inventory 2. Overstatement of purchases 3. Understatement of beginning inventory 4. Freight-in...
explain this errors with example of each on: 1- error of omission 2- error of commission...
explain this errors with example of each on: 1- error of omission 2- error of commission 3- error of principles 4- error of original entry 5- error of reversal entries 6- addition errors 7- posting error 8- trial balance errors 9- compensating errors
- In general, the occurrence of errors (errors) in a program can be classified in four...
- In general, the occurrence of errors (errors) in a program can be classified in four categories. List and explain, then give an example, of each mistake (error)! and also - It is known that: S -> aB | bA A -> a | aS | bAA B -> b | bS | aBB Derivation for the string aaabbabbba Provide a solution for its derivation and syntax analysis. Please explain it with easy understandable method, because im still in learning...
Find and correct at least two errors in each of the following segments of code. a)...
Find and correct at least two errors in each of the following segments of code. a) final int ARRAY_SIZE = 5; ARRAY_SIZE = 10; int[] table = new int[ARRAY_SIZE]; for (int x = 0; x <= ARRAY_SIZE; x++) table[x] = 99; b) String[] names = {“Mina” , “George”}; int totalLength = 0; for (int i = 0; i <= names.length() ; i++) totalLength += names[i].length; c) String[] words = “Hello”,“Goodbye”; System.out.println(words.toUpperCase()); d) public class MyClass { private int x; private...
What is invalid about each of the following program? Fixed the error once you find it....
What is invalid about each of the following program? Fixed the error once you find it. (You may find more than one errors in the code.) Question #include <iostream> #include <iomanip> using namespace std; class B { protected: virtual void func1() { cout << "base:" << endl; } }; class D1 : public B { void func1() { cout << "D2:" << endl; } }; class D2 : public B {}; int main() { B Bobj; D1 Dobj; B* p...
For each of the following errors, considered individually, indicate whether the error would cause the adjusted...
For each of the following errors, considered individually, indicate whether the error would cause the adjusted trial balance totals to be unequal. If the error would cause the adjusted trial balance totals to be unequal, indicate whether the debit or credit total is higher and by how much. 1 The adjustment for accrued wages of $5,550 was journalized as a debit to Wages Expense for $5,550 and a credit to Accounts Payable for $5,550. --- A. The totals are equal....
For each of the following errors, considered individually, indicate whether the error would cause the trial...
For each of the following errors, considered individually, indicate whether the error would cause the trial balance totals to be unequal. If the error would cause the trial balance totals to be unequal, indicate whether the debit or credit total is higher and by how much. If the debit and credit totals would be equal, enter zero ("0") in the amount box. a. The payment of an insurance premium of $7,450 for a three-year policy was debited to Prepaid Insurance...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT