• The following code consists of 5 parts. • Find the errors for each part and fi
ID: 3850708 • Letter: #
Question
• The following code consists of 5 parts.
• Find the errors for each part and fix them.
• Explain the errors using comments on top of each part.
*/
#include <stdio.h>
#include <string.h>
int main( ) {
//////////////////////////////////////////////////////////////////////////
////////////// Part A. (5 points) //////////////////
int g(void)
{
printf("%s", Inside function g n " );
int h(void)
{
printf(" % s ", Inside function h n ");
}
}
printf("Part A: ");
g();
printf(" ");
//////////////////////////////////////////////////////////////////////////
////////////// Part B. (5 points) //////////////////
int sum( int x, int y )
{
int result;
result = x + y;
}
printf("Part B: % d ", sum(5,3));
//////////////////////////////////////////////////////////////////////////
////////////// Part C. (5 points) //////////////////
void f( float a );
{
float a;
printf( "%f", a );
}
printf("Part C: % 1f ", f(1.1));
//////////////////////////////////////////////////////////////////////////
////////////// Part D. (5 points) //////////////////
#include <stdio.h>
#include <string.h>
int main( ) {
int sum( int n )
{
if ( 0 == n )
{
return 0;
}
else
{
n + sum( n - 1 );
}
}
printf("Part D: % d ", sum(3));
return 0;
}
//////////////////////////////////////////////////////////////////////////
////////////// Part E. (5 points) //////////////////
void product( void )
{
int a, b, c, result;
printf( "%s", "Enter three integers: " )
scanf( "%d%d%d", &a, &b, &c );
result = a * b * c;
printf( "Result is: %d", result );
return result;
}
printf( "Part E: %d", product ());
return 0;
}
Explanation / Answer
PART A:
Syntax of the printf function is incorrect. And the program is not enclosed with braces. Correct snippet is
#include <stdio.h>
#include <string.h>
int main( ) {
//////////////////////////////////////////////////////////////////////////
////////////// Part A. (5 points) //////////////////
int g(void)
{
//Correct syntax of printf
printf("Inside function g n " );
int h(void)
{
printf("Inside function h n ");
}
}
printf("Part A: ");
g();
printf(" ");
}
PART B:
Here the function sum has return type int , but the function is not returning any values. Correct snippet would be
int sum( int x, int y )
{
int result;
result = x + y;
//Return statement missing
return result;
}
printf("Part B: % d ", sum(5,3
PART C:
Here semicolon (;) is incorrectly placed in the first line of the function. Correct snippet would be
//Semicolon at the end of the function's first line
void f( float a );
{
float a;
printf( "%f", a );
}
printf("Part C: % 1f ", f(1.1));
PART D:
Return statement is missing in the inside function.
#include <stdio.h>
#include <string.h>
int main( ) {
int sum( int n )
{
if ( 0 == n )
{
return 0;
}
else
{
//Return statement is missing
return n + sum( n - 1 );
}
}
printf("Part D: % d ", sum(3));
return 0;
}
PART E:
semicolon is missing after printf statement
void product( void )
{
int a, b, c, result;
//Semicolon missing at the end of this printf statement
printf( "%s", "Enter three integers: " );
scanf( "%d%d%d", &a, &b, &c );
result = a * b * c;
printf( "Result is: %d", result );
return result;
}
printf( "Part E: %d", product ());
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.