#include <stdio.h> #pragma warning(disable : 4996) // CSE 240 Fall 2017 Homework
ID: 2246745 • Letter: #
Question
#include <stdio.h>
#pragma warning(disable : 4996)
// CSE 240 Fall 2017 Homework 1 Question 3 (25 points)
// Before starting this assignment, please compile and run this program.
// You will first notice that the program will not compile. Fix the errors and define the error types.
// Next, you will notice that the program is printing incorrect information. Fix the errors and define those error types.
void main() {
// Problem 1: (4 points)
// The statement below should cause the program to not compile. Correct the error(s). (2 points).
y = 67
printf("Integer y is equal to %d ", y);
// Define what type of error this is, your answer should replace the space next to "Error Type: " below (2 points).
// Your answer should be either Syntactic, Semantic, or Contextual.
printf("Error Type: ________ ");
// Problem 2: (4 points)
// Half of 10 is 5, why is the program printing that half of 10 is 0? Correct the error(s) (2 points).
int x = 10;
printf("Half of %d is %d ", x, x * (1 / 2));
// Define what type of error this is, your answer should replace the space next to "Error Type: " below (2 points).
// Your answer should be either Syntactic, Semantic, or Contextual.
printf("Error Type: ________ ");
//Problem 3 (2 points)
//Integer variable y is initialized to 67. However the print statement is giving a different result. Why is it giving that particular character as result?(1 point)
printf("%c",y);
// Your answer should be either Syntactic, Semantic, or Contextual. (1 point)
printf("Error Type: ________ ");
//Give your explanation here:
printf("Explanation:_____________ ");
// Problem 4: (2 points)
// Float variable a has been initialized to 10.5, why does the program seem to think otherwise? Correct the error(s) (2 points).
float a = 10.5;
if (a = 20);
printf("a is equal to 20. ");
if (a > 20);
printf("a is greater than 20. ");
if (a < 20);
printf("a is less than 20. ");
// Define what type of error this is, your answer should replace the space next to "Error Type: " below (2 points).
// Your answer should be either Syntactic, Semantic, or Contextual.
printf("Error Type: ________ ");
// Problem 5: (4 points)
// Surely, 10 is an even number. Why is the program printing that 10 is an odd number? Correct the error(s) (2 points).
x = 10;
if (x / 2 == 0)
printf("%d is an even number. ", x);
if (x / 2 != 0)
printf("%d is an odd number. ", x);
// Define what type of error this is, your answer should replace the space next to "Error Type: " below (2 points).
// Your answer should be either Syntactic, Semantic, or Contextual.
printf("Error Type: ________ ");
// Problem 6: (4 points)
// This bit of code is meant to print "Hello World!". Correct the error(s) (2 points).
printf("Hello %d ", "World!");
// Define what type of error this is, your answer should replace the space next to "Error Type: " below (2 points).
// Your answer should be either Syntactic, Semantic, or Contextual.
printf("Error Type: ________ ");
}
Explanation / Answer
Problem 1:
There is no semicolon after y = 67. Also y is not declared with a type.
The error type is a syntactic error.
Problem 2:
We are multiplying integer x with (1/2). Hence, when we multiply integer with any number, it is also converted to integer also. So,
(1/2)=0.5 = 0(When converted to integer)
Therefore, x*0 = 0.
The error type is a semantic error.
Problem 3:
In the print statement, %c is used instead of %d to print an integer.
%c is used to print a charactor.Hence, 67 is converted to a character with ASCII code = 67.
The error type is a semantic error.
Problem 4:
There is semicolon after the if satements, so the if statement ends after the semicolon and the next printf statement is executed. Also in the first if statement use == sign instead of = sign to check for equality.
The error type is a semantic error.
Problem 5:
x / 2 = 5
Hence, the output is not as expected.
The error type is a semantic error.
Problem 6:
Instead of %d use %s in the printf statement as %d is used to print an integer as %s is used to print a string.
The error type is a semantic error.
Correct Code:
#include<stdio.h>
int main() {
// Problem 1: (4 points)
// The statement below should cause the program to not compile. Correct the error(s). (2 points).
int y = 67;
printf("Integer y is equal to %d ", y);
// Define what type of error this is, your answer should replace the space next to "Error Type: " below (2 points).
// Your answer should be either Syntactic, Semantic, or Contextual.
printf("Error Type: Syntactic ");
// Problem 2: (4 points)
// Half of 10 is 5, why is the program printing that half of 10 is 0? Correct the error(s) (2 points).
int x = 10;
printf("Half of %d is %f ", x, x * 0.5);
// Define what type of error this is, your answer should replace the space next to "Error Type: " below (2 points).
// Your answer should be either Syntactic, Semantic, or Contextual.
printf("Error Type: Semantic ");
//Problem 3 (2 points)
//Integer variable y is initialized to 67. However the print statement is giving a different result. Why is it giving that particular character as result?(1 point)
printf("%d ",y);
// Your answer should be either Syntactic, Semantic, or Contextual. (1 point)
printf("Error Type: Semantic ");
//Give your explanation here:
printf("Explanation:In the print statement, %c is used instead of %d to print an integer.%c is used to print a charactor.Hence, 67 is converted to a character with ASCII code = 67. ");
// Problem 4: (2 points)
// Float variable a has been initialized to 10.5, why does the program seem to think otherwise? Correct the error(s) (2 points).
float a = 10.5;
if (a == 20)
printf("a is equal to 20. ");
if (a > 20)
printf("a is greater than 20. ");
if (a < 20)
printf("a is less than 20. ");
// Define what type of error this is, your answer should replace the space next to "Error Type: " below (2 points).
// Your answer should be either Syntactic, Semantic, or Contextual.
printf("Error Type: Semantic ");
// Problem 5: (4 points)
// Surely, 10 is an even number. Why is the program printing that 10 is an odd number? Correct the error(s) (2 points).
x = 10;
if (x % 2 == 0)
printf("%d is an even number. ", x);
if (x % 2 != 0)
printf("%d is an odd number. ", x);
// Define what type of error this is, your answer should replace the space next to "Error Type: " below (2 points).
// Your answer should be either Syntactic, Semantic, or Contextual.
printf("Error Type: Semantic ");
// Problem 6: (4 points)
// This bit of code is meant to print "Hello World!". Correct the error(s) (2 points).
printf("Hello %s ", "World!");
// Define what type of error this is, your answer should replace the space next to "Error Type: " below (2 points).
// Your answer should be either Syntactic, Semantic, or Contextual.
printf("Error Type: Semantic ");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.