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;

Edited 6 Years Ago by Nick Evan: Added code-tags

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;".

This article has been dead for over six months. Start a new discussion instead.