-
calculating a function
here is the code quzah starte for me and I added the cout and cin but I'm unsure where to add the PowerInt(n) =(n!)/n^2 formula. I think I should be adding that somewhere because it states how to test the variable that is entered or am I completly wrong?
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
float p(int i)
{
if( i < 10 && i > 1 )
{
int x = i*i;
y;
float z
cout << "enter a number" << endl;
cin >> z;
return z / (float)x;
}
return 1.0;
}
-
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(); //Took an error out when I added semicolon
float p(int i)
{
if( i < 10 && i > 1 )
{
int x = i*i;
int y; ///Needed variable int
float z; ///Forgot semicolon
cout << "enter a number" << endl;
cin >> z;
return z / (float)x;
}
return 1.0;
}
That fixed it when I compiled. I got these 2 errors when I tried to build however.
Code:
Compiling...
Cpp1.cpp
c:\compiled\float\cpp1.cpp(14) : warning C4101: 'y' : unreferenced local variable
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/Cpp1.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Cpp1.exe - 2 error(s), 1 warning(s)
I might be wrong just trying to help though :D
-
Ug. No that's not what I started. Well actually, it's part of what I started, used incorrectly. 'p' is a function. It is not a part of main. Before 'y', that was a , not a ;
Furthermore, the answer was given in your original thread. You ignored the second point of my illustration. Just read the original thread and pay attention to the provided code.
Quzah.
-
well I added a few things, sorry about that. I guess this program is just really hard. I'm going to look in the text book again and see if I cant put your examples and their examples together and figure somethign out.
Bryan
-
this again, i thought this was resolved.
-
I made up a new code, and it seems to work for the most part until it tries to calculate the PowerInt. I know where my problem is but I cant seem to make the n! do what it should. It needs to multiply. If user enters a 4 then it needs to multiply 1*2*3*4/4^2 but I cant seem to make it do that. Any more suggestions.
Thanks
Code:
#include <iostream>
using namespace std;
int main()
{
int iNum; //stores input value
int iSum; //stores sum
cout << "Please enter an interger: "; //user enters number
cin >> iNum; //letter stored
while (iNum >= 1 && iNum <= 10)
{
iSum = iNum!/iNum*iNum; //THIS IS MY PROBLEM
cout << "PowerInt" <<"(" << iNum << ") =" <<iSum << endl;
cout << "Please enter an interger: ";
cin >> iNum;
}
cout << "Exiting the program." << endl;
return 0;
}
-
>> iSum = iNum!/iNum*iNum; //THIS IS MY PROBLEM
Yes it is. First, use some parentheses. Second... ! does not do a factorial in C++ - it's a logical negation. You're going to have to code a factorial function...
Code:
int factorial(int number)
{
int result=1;
for(int t=number;t=1;t--)
result*=t;
return result;
}
Use it like this
iSum=factorial(iNum)/(iNum*iNum);
-
so if I use the code for factorial, which is what i was looking for, I would replace my erro with iSum=fac. ect. But where can I enter the code that you wrote in my program to make it work?
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int iNum; //stores input value
int iSum; //stores sum
cout << "Please enter an interger: "; //user enters number
cin >> iNum; ; //letter stored
while (iNum >= 1 && iNum <= 10)
{
iSum=factorial(iNum)/(iNum*iNum); //where do I input that code you wrote to make this not undefined?
cout << "PowerInt" <<"(" << iNum << ") =" <<iSum << endl;
cout << "Please enter an interger: ";
cin >> iNum;
}
cout << "Exiting the program." << endl;
return 0;
}
-
>> //where do I input that code you wrote to make this not undefined?
somewhere before main()
-
or stick the line
int factorial(int number);
before int main() and then stick the function that I wrote at the end of your program.
-
I figured out what the n factroail should look like with your help and researching the book more, but it still dont work out. I get this error when trying to build it. error C2601: 'factorial' : local function definitions are illegal
Code:
#include <iostream>
using namespace std;
int main()
{
int iNum; //stores input value
int iSum; //stores sum
int factorial (int iNum)
{ //ITS POINTING TO THIS BRACKET
int result;
result = 1;
while (iNum > 0)
{
result = result * iNum;
iNum--;
}
return result;
}
while (iNum >= 1 && iNum <= 10)
{
iSum=factorial(iNum)/(iNum*iNum);
cout << "Please enter an interger: "; //user enters number
cin >> iNum; ; //letter stored
cout << "PowerInt" <<"(" << iNum << ") =" <<iSum << endl;
cout << "Please enter an interger: ";
cin >> iNum;
}
cout << "Exiting the program." << endl;
return 0;
}
-
Change your code to look like this
Code:
#include <iostream>
using namespace std;
int factorial(int iNum);
int main()
{
int iNum; //stores input value
int iSum; //stores sum
while (iNum >= 1 && iNum <= 10)
{
iSum=factorial(iNum)/(iNum*iNum);
cout << "Please enter an interger: "; //user enters number
cin >> iNum; ; //letter stored
cout << "PowerInt" <<"(" << iNum << ") =" <<iSum << endl;
cout << "Please enter an interger: ";
cin >> iNum;
}
cout << "Exiting the program." << endl;
return 0;
}
int factorial (int iNum)
{
int result;
result = 1;
while (iNum > 0)
{
result = result * iNum;
iNum--;
}
return result;
}
-
blackrat did you even run your program? It goes right to the exiting the program string.
-
Code:
int iNum; //stores input value
int iSum; //stores sum
while (iNum >= 1 && iNum <= 10)
so what does iSum and iNum == there??
Code:
int factorial (int iNum)
{
int result;
result = 1;
while (iNum > 0)
{
result = result * iNum;
iNum--;
}
return result;
}
will always return 0
-
I GOT IT
thanks for everyone who helped me with this. I appreciate it so much. After a few days of this I finally got it. I could not get them to print out in decimals becuase I was putting int iNum and int iSum and they needed to be float iNum and float iSum...it works great now. thanks again
Code:
//**************************
//This program calculates
//an n! factorial
//**************************
#include <iostream>
using namespace std;
int factorial(int iNum); //number whose factorial is to be computed
int main()
{
float iNum; //stores input value
float iSum; //stores sum
do
{
cout << "Please enter an interger: "; //user enters number
cin >> iNum; ; //letter stored
if (iNum<=-1)break; //if negative number program will end
iSum=factorial(iNum)/(iNum*iNum); //call to factorial
cout << "PowerInt" <<"(" << iNum << ") ="<<iSum << endl; //output
}while(true); //if true program will exit
cout << "Exiting the program." << endl; //end program
return 0;
}
int factorial (int iNum) //n! factorial
{
int result;
result = 1;
while (iNum > 1)
{
result = result * iNum;
iNum--;
}
return result;
}