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, 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 }

Solutions

Expert Solution

//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);
}


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
Detect the errors in the following program? How many errors do you observe in the following...
Detect the errors in the following program? How many errors do you observe in the following line of code? Clearly explain what these errors are and how they can be fixed? (Note that this program has only three lines of code). Please type your program in the following textbox. for i in Range(1,20.0): if i%2=0: print(i) This is a python program
- 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...
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