Question

Given the following main program: int main() {    int x;    int y = 10;...

Given the following main program:

int main()
{
   int x;
   int y = 10;
   int z = 20;

   x = add(y,z);

   return 0;
}

Which function is correctly written to store the value in x?

int add(int a, int b)
{
   return a+b;
}

float add(float a, float b)
{
   float x;

   x = a + b;

   return x;
}

void add(int y, int z)
{
   int x;

   x = y + z;

   return;
}

int add(y, z)
{
   int x;

   x = y + z;

   return x;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: Option (a) and Option (b)

Option(a):

int add(int a, int b)
{
   return a+b;
}

It is taking both a and b values. After adding them it is returning this result to x in the main function.

Code:

#include<stdio.h>
int add(int a, int b);
int main()
{
int x;
int y = 10;
int z = 20;
x = add(y,z);
printf("The value of x will be: %d",x);
return 0;
}
int add(int a, int b)
{
return a+b;
}

Output:

Option(b):

float add(float a, float b)
{
   float x;

   x = a + b;

   return x;
}

It is taking both integer values and storing them as float values. When you add them the result is float value. So it is returning a float value to the main functon. And 'x' is an integer variable , so it will cast the input and stores into it.

This processs is more hectic than the process in the option (a) , so option (a) might be the answer if you have to pick only one option.

Code:

#include<stdio.h>
float add(float a, float b);
int main()
{
int x;
int y = 10;
int z = 20;
x = add(y,z);
printf("The value of x will be: %d",x);
return 0;
}      
float add(float a, float b)
{
float x;

x = a + b;

return x;
}

Ouput:

Option (c):

void add(int y, int z)
{
   int x;

   x = y + z;

   return;
}

You can observe that this code is not returning any value to the main function . After execution of this code the values computed by this peice of code will be vanished. This value will not be stored on the variable 'x' of main function. So this is an incorrect option.

Option(d):

int add(y, z)
{
   int x;

   x = y + z;

   return x;
}

You can observe that it is a wrong syntax. function header "int add(x,y)" is having a wrong syntax. Type of the variable x and y are not specified which leads to an error. So this option is also incorrect.

Add a comment
Know the answer?
Add Answer to:
Given the following main program: int main() {    int x;    int y = 10;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is the output of the following program? int main(void) { int x, y, z; x...

    What is the output of the following program? int main(void) { int x, y, z; x = 5; y= test(x); z= x + y; cout<< setw(4)<<x <<” + “<<setw(4)<<y<<”=” << setw(4)<<z<<endl; return 0; } int test (int x) { int w; w = x + 10; return (w); }

  • C++ What is the result of the following program? #include <algorithm> int main() {int x =...

    C++ What is the result of the following program? #include <algorithm> int main() {int x = 10; int *y = &x; int *z = new int; z[0] = 20; std::swap (y, z); return x;} (A) Does not compile. (B) Runtime Error or Undefined Behavior (C) Returns 10 (D) Returns 20

  • 1 Rewrite the following program so that it will use function. include <stdio.h> Int main) printf...

    1 Rewrite the following program so that it will use function. include <stdio.h> Int main) printf ("Hello!") return 0 2 Assume a program has the following declarations: short s- 4 int i--5; long m-3 float f-19.3f; double d-3.6; What are the following values? (a) s+m (b) (float) d (c) d s (e) (int) f 3 What will be the output of the following program: int x = 3 float a 2.76; y-2* x; print f("X-2d, y*ta, z»%d", x, y, z);...

  • (a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0)...

    (a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0) return 0; 3 return (n-1 + g(n-1)); 4} (b) Consider the following C++ function: 1 bool Function (const vector <int >& a) { 2 for (int i = 0; i < a. size ()-1; i ++) { 3 for (int j = i +1; j < a. size (); j ++) { 4 if (a[i] == a[j]) return false; 5 6 } 7 return...

  • Consider the following program: # include <iostream> int x = 3, y = 5; void foo(void)...

    Consider the following program: # include <iostream> int x = 3, y = 5; void foo(void) { x = x + 2; y = y + 4; } void bar(void) { int x = 10; y = y + 3; foo( ); cout << x << endl; cout << y << endl; } void baz(void) { int y = 7; bar( ); cout << y << endl; } void main( ) { baz( ); } What output does this program...

  • QUESTION 62 Consider the following code: void Pancake(int x, int& y, int z); void Waffle(int& x,...

    QUESTION 62 Consider the following code: void Pancake(int x, int& y, int z); void Waffle(int& x, int& y); int Doughnut(int y, int z); int main( ) {             int a = 1;             int b = 2;             int c = 3;             int d = 4;             Pancake(a, b, c);             Waffle(b, c);             Pancake(d, c, b);             d = Doughnut(b, a);             return 0; } void Pancake(int x, int& y, int z) {             y += 3;            ...

  • Design a swap function with the following interface: void swap( int *x,    int *y) { }...

    Design a swap function with the following interface: void swap( int *x,    int *y) { } In your main( ), you perform the following test: int main( ) {             int a = 10, b = 20;             cout << “a = “ << a << “   b= “   << b   << endl;             swap( a, b);             cout << “a = “ << a << “   b= “   << b   << endl;             return 0;                         … }

  • Consider the following in C int main() { float x = 3.14, *p = &x; int...

    Consider the following in C int main() { float x = 3.14, *p = &x; int r, a = 2, b[] = {5, 6, 7}; <more code here> r = Foo(x, p, &a, b) <more code here> } int Foo(float x,float y, int *z, int *w) { <Foo's code goes here> } statement in Foo result (assigned value) modify Foo's AF variables modify main's AF variables statement in Foo result (assigned value) modify Foo's AF variables modify main's AF variables...

  • 16 Points) Question 3 Write down the outputs of the following program into the provided table include <iostream> using namespace std; void fun I(int a); int fun2(int a, int b); int x-3: int...

    16 Points) Question 3 Write down the outputs of the following program into the provided table include <iostream> using namespace std; void fun I(int a); int fun2(int a, int b); int x-3: int main) int x-1,y 0,z-2; x-fun2(y,z); cout sx fun 1 (z); cout (#xtytz(endl; y-fun2(x,x); cout <exty+zscendl; system("pause"); void fun 1 (int a) int fun2(int a, int b) int static c2; return atx; 16 Points) Question 3 Write down the outputs of the following program into the provided table...

  • Three is 3 questions please answer them all Question 1 int main(void) { int x =...

    Three is 3 questions please answer them all Question 1 int main(void) { int x = 10, y = 20; if (x == y); printf ("\n%d %d",x,y); } Output as you would see on the compiler: Question 2 int main(void) { int x = 3, y = 5; if (x == 3) printf ("\n%d",x); else ; printf("%d", y); return 0; Output as you would see on the compiler: Question 3 int main(void) { int x = 3; float y =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT