In: Computer Science
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, total, min, max ;
6 char names[1000] ;
7 FILE fp ;
8
9 fp = fopen ( heights.txt, "r" ) ;
10 if ( fp = NULL )
11 printf ( "Cannot open heights.txt for reading\n" )
;
12 exit ( -1 ) ;
13
14 while ( n<1000 && fscanf(fp,"%s %d", &names[n],
&heights[n]) != EOF){
15 n++ ;
16 }
17
18 printf ( "Enter minimum height to display: " ) ;
19 scanf ( "%d", &min ) ;
20 printf ( "Enter maximum height to display: " ) ;
21 scanf ( "%d", &max ) ;
22
23 total = 0 ;
24
25 for ( i = 0 ; i < n ; i++ ){
26 if ( heights[i]>=min || min==0 && heights[i]<=max
|| max==0 ){
27 // display the person and height
28 printf ( "%c: %dcm\n", names[i], heights[i] ) ;
29 total++ ;
30 }
31 i++ ;
32 }
33 printf ( "Total matches: %d\n", total ) ;
34 return (0);
35 }
//ERROR 1
//Description: Semicolon is not requried after
<stdio.h>
//Statement Correction
#include <stdio.h>
//ERROR:6
//Description: to use exit() we need to include stdlib
//Statement Correction
#include <stdlib.h>
int main ( void ){
int heights[1000], i, n, total, min, max ;
char names[1000] ;
//ERROR:5
//Description: we should declre fp as file pointer not as a
variable
//Statement Correction
FILE *fp ;
//ERROR:2
//Description: name of file must be passed as string within
""
//Statement Correction
fp = fopen ( "heights.txt", "r" ) ;
//ERROR:3
//Description: for comparision we should use == not =
//Statement Correction
//ERROR:4
//Description: to put more than 1 statement inside if blck we
should put {}
//Statement Correction
if ( fp == NULL ){
printf ( "Cannot open heights.txt for reading\n" ) ;
exit ( -1 ) ;
}
while ( n<1000 && fscanf(fp,"%s %d", &names[n],
&heights[n]) != EOF){
n++ ;
}
printf ( "Enter minimum height to display: " ) ;
scanf ( "%d", &min ) ;
printf ( "Enter maximum height to display: " ) ;
scanf ( "%d", &max ) ;
total = 0 ;
for ( i = 0 ; i < n ; i++ ){
if ( heights[i]>=min || min==0 && heights[i]<=max ||
max==0 ){
// display the person and height
printf ( "%c: %dcm\n", names[i], heights[i] ) ;
total++ ;
}
i++ ;
}
printf ( "Total matches: %d\n", total ) ;
return (0);
}