I have typed this many ways into dev-c++ trying to figure out how to figure out the output but it always comes back with errors no matter what headers I have used and other things. Can anyone give me some ideas? I don't want the answer, just ideas on how to find it. Being given the answer won't help me learn how to solve future problems. The problem is:
Consider the following function:

int mystery (int x, double y, char ch)
{
    int u;
    if ('A' <= ch && ch <= 'R')
       return (2 * x + static_cast<int> (y));
    else
       return (static_cast<int> (2 * y) - x);
}

what is the output of the following C++ statement?
cout << mystery (5, 4.3, 'B') << endl;

Dani AI

Generated

A quick, practical way to reason this through without running the program (though compiling is a good check, as suggested).

For the first function: the character test chooses the first branch because 'B' lies between 'A' and 'R' in the execution character set on normal systems. That branch returns 2x plus the integral part of y. With x = 5 and y = 4.3 you get 25 = 10 and static_cast<int>(4.3) = 4, so the expression evaluates to 14. Note: static_cast<int> drops the fractional part (truncates toward zero).

For the secret example with x = 10: doubling gives i = 20, so the if branch runs and j = x/2 = 5; the function returns j - 1, i.e. 4. Remember integer division discards any remainder (truncates toward zero).

The compile error you saw ("x undeclared") is just scope: a function parameter name exists only inside that function. As and pointed out, either declare and initialize x in main before calling secret, or pass a literal. Small extra tips: initialize variables before use, watch for unused locals (they cause warnings), and use a tiny, compilable test program when you want to confirm behavior quickly.

Recommended Answers

All 5 Replies

To turn this into a compilable and runnable program, use this:

#include <iostream> //this will include the "std::cout" stream for output to console.
using namespace std; //this will allow you to avoid writing "std::" all the time.

int mystery (int x, double y, char ch)
{
  int u;
  if ('A' <= ch && ch <= 'R')
    return (2 * x + static_cast<int> (y));
  else
    return (static_cast<int> (2 * y) - x);
}

int main() {
  //what is the output of the following C++ statement?
  cout << mystery (5, 4.3, 'B') << endl;

  return 0; //this is necessary to return a "all went well" result of the program.
};

That's all. Please use code-tags in the future to better format the code in your post.

My next question is on "assuming x, y and k are int variables what is the output of x=10; cout << secret (x) << endl;

int secret (int x)
	{
		int i, j;
		i = 2 * x;
		if (i > 10)
			j = x / 2;
		else
			j = x / 3;
		return j – 1;
	}

	int another(int a, int b)
	{
		int i, j;
		j = 0;
		for ( i = a; i <= b; i++)
		      j = j + i;

		return j;
	}

I am getting the error x undeclared on line 26. What I currently have is

#include <iostream>

using namespace std;
int secret (int x)
	{
		int i, j;
		i = 2 * x;
		if (i > 10)
			j = x / 2;
		else
			j = x / 3;
		return j - 1;
	}

	int another(int a, int b)
	{
		int i, j;
		j = 0;
		for ( i = a; i <= b; i++)
		      j = j + i;

		return j;
	}
int main ()
{
    cout << secret (x) << endl;
    system ("pause");
	return 0;
       }

>>I am getting the error x undeclared on line 26. What I currently have is
is it because you didn't declare type variable in main?(int x)

You have no variable called "x" in the function main(), so the statement "cout << secret (x) << endl;" is meaningless. You need to either make a variable "x" and give it the proper type and assign a value to it. Or, just pass a literal constant, like 5, to the function call, i.e. "cout << secret (5) << endl;".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.