/* • The following code consists of 5 parts. • Find the errors for each part and
ID: 3849931 • 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. (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(" ");
//////////////////////////////////////////////////////////////////////////
In part one error is becase of
printf("%s", Inside function g n " );
here we are trying to print string but Inside function g n " it is not a string string should in betwenn " and " so correct syantax is
printf("%s"," Inside function g n " );
for part 1 correct code is:
//////////////////////////////////////////////////////////////////////////
////////////// 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) //////////////////
void f( float a );
{
float a;
printf( "%f", a );
}
printf("Part C: % 1f ", f(1.1));
In part c there lot of error
1)void f( float a ); here we are trying to create a function f but we are put ; at the end so it doesn't act as a function it acts as a statement so we have to remove semicolon 2)float a; we are alredy passing a as an argument so it leads to redeclaration to avoid this eirther we have to remove float a from functino body or we have to change it's name.
3)printf("Part C: % 1f ", f(1.1));
here we are trying print the return value of f(float a).But the return values is void.so we have to change it to float.
Part C:
float f( float a )
{
printf( "%f", a );
return a;
}
printf("Part C: % 1f ", f(1.1));
D)
There is no error in part D internally.But if we observe it it has a main method. we are writing part D inside a main method.A c programm doesn't contain two main methods. we can resolve it by
1) writing part D in a separate file
printf( "Part E: %d", product ());
In part E
1) printf( "%s", "Enter three integers: " ) there is no semicolon at the end so insert ; at the end.
2)void product( void ) here we are returning void but return result; in function body we are returning an int type. so change it return type to int.
int product(void);
Part E:
int 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;
//entire code for part a,b,c,e
#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) //////////////////
float f( float a )
{
printf( "%f", a );
}
printf("Part C: % 1f ", f(1.1));
////////////// Part E. (5 points) //////////////////
int 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;
}
//////////////////////////////////////////////////////////////////////////
////////////// 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 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(" ");
//////////////////////////////////////////////////////////////////////////
In part one error is becase of
printf("%s", Inside function g n " );
here we are trying to print string but Inside function g n " it is not a string string should in betwenn " and " so correct syantax is
printf("%s"," Inside function g n " );
for part 1 correct code is:
//////////////////////////////////////////////////////////////////////////
////////////// 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) //////////////////
void f( float a );
{
float a;
printf( "%f", a );
}
printf("Part C: % 1f ", f(1.1));
In part c there lot of error
1)void f( float a ); here we are trying to create a function f but we are put ; at the end so it doesn't act as a function it acts as a statement so we have to remove semicolon 2)float a; we are alredy passing a as an argument so it leads to redeclaration to avoid this eirther we have to remove float a from functino body or we have to change it's name.
3)printf("Part C: % 1f ", f(1.1));
here we are trying print the return value of f(float a).But the return values is void.so we have to change it to float.
Part C:
float f( float a )
{
printf( "%f", a );
return a;
}
printf("Part C: % 1f ", f(1.1));
D)
There is no error in part D internally.But if we observe it it has a main method. we are writing part D inside a main method.A c programm doesn't contain two main methods. we can resolve it by
1) writing part D in a separate file
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 ());
In part E
1) printf( "%s", "Enter three integers: " ) there is no semicolon at the end so insert ; at the end.
2)void product( void ) here we are returning void but return result; in function body we are returning an int type. so change it return type to int.
int product(void);
Part E:
int 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;
//entire code for part a,b,c,e
#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) //////////////////
float f( float a )
{
printf( "%f", a );
}
printf("Part C: % 1f ", f(1.1));
////////////// Part E. (5 points) //////////////////
int 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;
}
//////////////////////////////////////////////////////////////////////////
////////////// 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;
}
//////////////////////////////////////////////////////////////////////////
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.