Undeclared identifier in c++ - c++

I want to print three number from maximum to minimum for 3 numbers, when I try to compile this code it shows me this error C2065 "function parameter :Undeclared identifier function parameter"for every function arguments . other error is C 2062 type "int" unexpected.
Here is my code
#include "stdafx.h"
#include <iostream>
using namespace std;
int max, min;//making global variable of max and min
void numMax(int x, int y, int z);//finding maximum number
void numMin(int x, int y, int z);/finding minimum number
int main()
{
int x, int y, int z;
int middle = 0;
cout << "This program will take 3 number and print them from minimum to maximum" << endl;
cout << "_________________" << endl;
cout << "Pleas enter three number" << endl;
cout << "num1 =";cin >> x;cout << endl << "\n";
cout << "num2 =";cin >> y;cout << endl << "\n";
cout << "num3 =";cin >> z;cout << endl << "\n";
numMax(x, y, z);
numMin(x, y, z);
if (x<max & x>min)
{
middle = x;
}
if (y<max & y>min)
{
middle = y;
}
if (z<max & z>min)
{
middle = z;
}
cout <<"ordered numbers are : "<< min << "\t"<< middle << "\t" <<max ;
return 0;
}
void numMAx(int x, int y, int z)
{
int max;
max = x > y ? x : y;
max = z > max ? z : max;
cout << max;
}
void numMin(int x, int y, int z)
{
int min;
min = x < y ? x : y;
min = min<z ? min : z;
cout << min;
}
first I have defined my functions, then in main function I have passed parameter to function argument then I have mentioned my numMax and numMin fuctions to excute their task. finally I have used if statement for determining middle number. What should I do ?

int numMax (int x,int y ,int z)
{
if(x>y && x>z)
return x;
else if (y>x && y>z)
return y;
else
return z;
}
int numMin(int x,int y ,int z)
{
if(x<y && x<z)
return x;
else if (y<x && y<z)
return y;
else
return z;
}
void main()
{
int x,y,z;
int max,min;
clrscr();
cout<<"\n Enter 3 Number: \n";
cout<<"1st Num: ";cin>>x;
cout<<"2nd Num: ";cin>>y;
cout<<"3rd Num: ";cin>>z;
max = numMax(x,y,z);
min = numMin(x,y,z);
if(x<max && x>min)
{
middle=x;
}
else if(y<max && y>min)
{
middle=y;
}
else
middle=z;
cout<<"Number from max to min are: \n "<<numMax(x,y,z)<<" "<<numMin(x,y,z)<<" "<<middle;
}
try this

Related

C++ recursion. Why won't my recursion method call back to itself

i am not sure how to resolve this math problem. what should i recall and where did i miss something. i have tried different opportunities. i think i just call not existing index or something like that..
#include <iostream>
using namespace std;
double recur(int n, int x);
double x;
int number;
int main()
{
cout << "enter n: " ;
cin >> number;
cout << endl;
do
{
cout << "enter float x!=0: ";
cin >> x;
cout << endl;
} while (x==0);
cout << "recur(" << number << "," << x << ")=" << recur(number, x) << endl;
system("pause");
}
double recur(int n, int x)
{
if (n > 1) return (x * recur(n, x - n) * recur(n - 1, x));
else if( n == 1) return x * recur(n,x) - x;
else return 1;
}
Formula:
For formula:
It's implementation:
#include <iostream>
#include<cmath>
using namespace std;
double recur(int n, int x);
double x;
int number;
int main()
{
cout << "enter n: " ;
cin >> number;
cout << endl;
do
{
cout << "enter float x!=0: ";
cin >> x;
cout << endl;
} while (x==0);
cout << "recur(" << number << "," << x << ")=" << recur(number, x) << endl;
system("pause");
}
double recur(int n, int x)
{
if (n > 1) return (x*(pow(log(x),n)) - n*(recur(n-1,x)));
else if( n == 1) return x * log(x) - x;
}
For n>1 line
(x*(pow(log(x),n)) = x*(ln x)^n
n*(recur(n-1,x)) = n* integral( (lnx)^(n-1) ) <- In next recursion call one power will get reduced
For n=1 line
x * log(x) - x = xlnx - x <- base condition(where recursive call will stop)
In this implementation recur(n,x) denotes integration of (lnx)^n w.r.t x.
Your integral isn't the same as the log portion, so you should probably split that out.
double ln_n(int n, double x) {
while (n --> 0) x = log(x);
return x;
}
double integral(int n, double x) {
if (n > 0) return (x * ln_n(n, x)) - (n * integral(n - 1, x));
return x;
}
See it live

C++ function will not print array to console in reverse

I'm working on homework for my c++ class we have just started arrays. I cannot figure out why the function reverse_array() does not print to the console when the function show_array() prints to the console without an issue. I've attempted googling the issue without success. I am just beginning c++ so it could be something small that I'm overlooking,. I appreciate any help.
#include<iostream>
using namespace std;
double dbval[5];
void fill_array(int x, double dbval[]);
void show_array(int x, double dbval[]);
void reverse_array(int x, double dbval[]);
int main(){
int x = 0;
fill_array(x,dbval);
show_array(x,dbval);
reverse_array(x,dbval);
return 0;
}
void fill_array(int x, double dbval[]){
int count = 0;
for (x = 0; x < 5; x++){
cin >> dbval[x];
if(!cin){
break;
}
}
for(x = 0; x < 5; x++){
count = count + 1;
}
cout << "Entries " <<int(count);
cout <<endl;
}
void show_array(int x, double dbval[]){
for (x = 0; x<5;x++){
cout << dbval[x] << " ";
}
cout << endl;
}
void reverse_array(int x, double dbval[]){
for(x = 5; x < 5; x--){
cout << dbval[x] << " ";
}
cout << endl;
}
This loop for(x = 5; x < 5 ; x--) failed because it did not satisfy your condition the first time it checked (x = 5, but it needs to be smaller than 5 to enter the loop)
Change to this:
void reverse_array(int x, double dbval[]){
for(x = 4; x >= 0 ; x--){
cout << dbval[x] << " ";
}
cout << endl;
}

(C++) How to pass a local variable from main to another function's body if the value is already inputed by user in main?

The only way I was able to solve this is by creating global variables like float x, y; and then using those throughout the program. I am very new to programming as it's obvious, but I've heard that is usually a bad practice to create global variables.
My guess is that the problem occurs in the void completeOperation(int z){} function as error messages basically state that both the variable x and y haven't been declared. When I declare them in that scope, it asks me to give them a value while I need a user to input the said value in main.
How can I make this work? Forgive me for having to put the whole code up. It's tiny.
#include <iostream>
using namespace std;
void createMenu();
void completeOperation(int z);
float add(float x, float y);
float subtract(float x, float y);
float multiply(float x, float y);
float divide(float x, float y);
float greaterNumber(float x, float y);
float lesserNumber(float x, float y);
int main()
{
float x, y;
cout << "Enter the first real number value." << endl;
cin >> x;
cout << "Enter the second real number value." << endl;
cin >> y;
createMenu();
int z;
cin >> z;
completeOperation(z);
system("pause");
}
void createMenu()
{
cout << "\nChoose a desired arithmetic operation by entering a number from 1 to 6. \n\n";
cout << "1.) Addition \n2.) Subtraction \n3.) Multiplication \n4.) Division \n5.) Bigger number \n6.) Smaller number" << endl;
}
void completeOperation(int z)
{
switch (z)
{
case 1:
cout << "\nAddition: " << add(x, y) << endl;
break;
case 2:
cout << "\nSubtraction: " << subtract(x, y) << endl;
break;
case 3:
cout << "\nMultiplication: " << multiply(x, y) << endl;
break;
case 4:
cout << "\nDivision: " << divide(x, y) << endl;
break;
case 5:
cout << "\nBigger number: " << greaterNumber(x, y) << endl;
break;
case 6:
cout << "\nSmaller number: " << lesserNumber(x, y) << endl;
break;
}
}
float add(float x, float y)
{
return (x + y);
}
float subtract(float x, float y)
{
return (x - y);
}
float multiply(float x, float y)
{
return (x * y);
}
float divide(float x, float y)
{
return (x / y);
}
float greaterNumber(float x, float y)
{
if (x > y)
{
return x;
}
else return y;
}
float lesserNumber(float x, float y)
{
if (x < y)
{
return x;
}
else return y;
}

How to evaluate operations then print a message

Take 4 variables from user w, x ,y ,z.
If w ‘x’ y = z print “You are right” otherwise, print
“ERROR
Below is the code I created for this problem, but it seems there is something wrong.
#include<iostream>
using namespace std;
void calculate(int w, char x, int y, int z)
{
if (x == '+') {
int z = w + y ;
cout << "You are right" <<endl;
}
else if (x == '-') {
int z = w - y ;
cout << "You are right" << endl;
}
else if (x == '*') {
int z = x * y;
cout << "You are right" << endl;
}
else
cout << "Error!" << endl;
}
int main()
{
int w, y, z;
char x;
cout << "Enter values for w, x, y, and z: " << endl;
cin >> w >> x >> y >> z;
cout << endl;
calculate(w, y, x, z);
return 0;
}
void calculate(int w, char x, int y, int z)
{
if (x == '+') {
int z = w + y ;
cout << "You are right" <<endl;
}
else if (x == '-') {
int z = w - y ;
cout << "You are right" << endl;
}
else if (x == '*') {
int z = x * y;
cout << "You are right" << endl;
}
else
cout << "Error!" << endl;
}
If you look closely int z = w + y is wrong as you are already taking z as a function parameter.
The correct statement, however, would be z = w + y.
The second thing, you need to remove int z from your function parameters, but declare and define it inside the function body. This is because when you pass z from main(), you aren't really passing the variable z. But merely the value of it which will get copied into int z in calculate.
Passing z as a reference
If you want the z in your main to update, you shall pass the value by reference.
void calculate(int w,char x,int y,int& z)
{
//updating z
}
Now, if you apply any change to z in calculate(), it will also appear your main().
Another solution is to to return int from the function and assign to the variable z in main().
#include <iostream>
int calculate(int w,int y,char x)
{
switch(x)
{
case '+':
std::cout << "You are right!\n";
return w+y;
case '-':
std::cout << "You are right!\n";
return w-y;
case '*':
std::cout << "You are right!\n";
return w*y;
default:
std::cout << "You are wrong!\n";
break;
}
}
int main()
{
int w = 5;
int y = 10;
char x = '+';
int z = calculate(w,y,x);
std::cout << z;
return 0;
}

Try catch using a function for negative integers

So basically I am trying to get it to stop repeating. If I enter numbers correctly it works fine. If I enter negative numbers which are not allowed and needs a try-catch exception it keeps repeating and won't stop asking for numbers.
All I have is this source file for the code and I am trying to make a function for main.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void gcd(int x, int y);
int main()
{
int x;
int y;
cout << "Please enter two integer values" << endl;
cin >> x;
cin >> y;
gcd(x, y);
return 0;
}
void gcd(int x, int y)
{
int gcd;
int s = 0;
while (s == 0)
{
try
{
if (x < 0 || y < 0)
throw 1;
else
{
s == 1;
break;
}
}
catch (int x)
{
cout << "Wrong negative input please type in two Positive integers" << endl;
cin >> x >> y;
continue;
}
}
for (int i = 1; i <= x && i <= y; i++)
{
if (x % i == 0 && y % i == 0)
gcd = i;
}
cout << "The gcd of x: " << x << " and y: " << y << " is: " << gcd << endl;
}
If you don't want your function gcd() to be called with negative values, throw a std::invalid_argument exception. It is not the business of gcd() to request user input. Validate the input in main() before you call gcd().
#include <limits>
#include <stdexcept>
#include <iostream>
int gcd(int, int);
int main()
{
int x, y;
while (std::cout << "Please enter two positive integers: ",
!(std::cin >> x >> y) || x < 0 || y < 0)
{
std::cerr << "Input error :(\n\n";
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
std::cout << "The gcd of x: " << x << " and y: " << y << " is: " << gcd(x, y) << "\n\n";
}
int gcd(int x, int y)
{
if (x < 0 || y < 0)
throw std::invalid_argument("No negative arguments to gcd(), please :(");
return y == 0 ? x : gcd(y, x % y);
}
You can (and perhaps should) remove the logic from gcd function and instead place it where you get your input from user, that is, in main. Also, state the requirements up front. For example:
int main()
{
int x;
int y;
cout << "Please enter two positive integer values" << endl;
cin >> x;
cin >> y;
if (x < 0 || y < 0)
{
cout << "Wrong negative input please type in two Positive integers" << endl;
return 0;
}
gcd(x, y);
return 0;
}
Now, you can place assertions in gcd to enforce no negative values get in:
void gcd(int x, int y)
{
assert(x >= 0);
assert(y >= 0);
// ...
}