calculating a function

This is a discussion on calculating a function within the C++ Programming forums, part of the General Programming Boards category; here is the code quzah starte for me and I added the cout and cin but I'm unsure where to ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    73

    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;
    }

  2. #2
    ___
    Join Date
    Jun 2003
    Posts
    806
    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
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    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

  5. #5
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    this again, i thought this was resolved.
    Be a leader and not a follower.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    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;
    }

  7. #7
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    >> 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);
    Away.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    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;
    }

  9. #9
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >> //where do I input that code you wrote to make this not undefined?

    somewhere before main()

  10. #10
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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.
    Away.

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    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;
    }

  12. #12
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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;
    }
    Away.

  13. #13
    ___
    Join Date
    Jun 2003
    Posts
    806
    blackrat did you even run your program? It goes right to the exiting the program string.
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  14. #14
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  15. #15
    Registered User
    Join Date
    Jun 2003
    Posts
    73

    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;
    }
    Last edited by romeoz; 07-12-2003 at 04:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21