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 = 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 }
Here is the answer for your question in C Programming Language.
Kindly upvote if you find the answer helpful.
###############################################################
LIST OF ERROR WITH SOLUTIONS :
#############################################################################
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 :)