for those that are good with c++ (1 Viewer)

A2RAYA

You've done us proud boys
Joined
Mar 14, 2004
Messages
308
Location
Old Trafford
Gender
Male
HSC
2004
hey all...if anyone here knows their c++ programs..i need some help with a couple of programs i'm writing for uni

1)

Code:
//prac week 13-Q1
//program will ask the user to enter a value (x) and then use it in one of two functions,
//depending on its size,to return a value for the function g(x)

#include <iostream.h>
#include <stdlib.h>
#include <math.h>

double g(double x1),
double g_2(double x2);

void main()
{
	double x;

	cout<<"enter a number: ";
	cin>>x;

	if(x<0)
	{
		 g(x);
	}

	if(x>=0)
	{
		  g_2(x);
	}

	//--------------------------------------------------------------------------------


	  double g(double x)

-->{
		cout<<"g(x)= 0"<<endl<<endl;
}

	//--------------------------------------------------------------------------------

 double g_2(double x)

-->{	
		cout<<"g(x)= "<<(6*exp(x-3))<<endl;
}

	return;
}
now i can't see a prob with this but then i compile it and it says:
error C2601: 'g' : local function definitions are illegal
error C2601: 'g_2' : local function definitions are illegal

any ideas?
cheers
 
Last edited by a moderator:

Slidey

But pieces of what?
Joined
Jun 12, 2004
Messages
6,600
Gender
Male
HSC
2005
Code:
#include <iostream.h>
#include <stdlib.h>
#include <math.h>

double g(double x1);
double g_2(double x2);

int main()
{
    
double x;
cout<<"enter a number: ";
cin>>x;
if(x<0)
{
g(x);
}
else if(x>=0)
{
g_2(x);
}
system("pause");
return 0;
}

double g(double x)
{
cout<<"g(x)= 0"<<endl;
}

double g_2(double x)
{ 
cout<<"g(x)= "<<(6*exp(x-3))<<endl;
}
 
Last edited by a moderator:

A2RAYA

You've done us proud boys
Joined
Mar 14, 2004
Messages
308
Location
Old Trafford
Gender
Male
HSC
2004
thanks heaps slide....but now the b**tards have given us an even harder one...heres it is if u wana try it :):

"The factorial of a nonnegative integer n is written n! and is defined as follows:
n! = n*(n-1)*(n-2)* ….*1 (for values of n =>1)
and
n! = 1 (for n = 0).

The cosine of an angle can be computed from the following infinite series:

cos x= 1- x^2/2! + x^4/4! + x^6/6 + .......


Write a program that reads an angle x (in radians) from the keyboard and the number of terms to be used in the series. Then, in a function, compute the cosine of the angle using the terms specified for this series. Print the value computed along with the value of the cosine computed using the C++ library function."
 

Slidey

But pieces of what?
Joined
Jun 12, 2004
Messages
6,600
Gender
Male
HSC
2005
Here's something on factorials to get you started, but since this is presumably a uni assignment, I am not going to code the programme for you:
Code:
#include<iostream.h>
int main()
{
    int n=0;
    cin >> n;
    int a=n;
    if(n==0)//case where 0!=1
    {
        cout << n << "! is 1\n";
    }
    else if(n>0)//where n is a natural number
    {
        int result=1;
        for(int i=0;i<n;i++)
        {
            result=result*(a-i); 
        }   
        cout << n << "! is " << result << endl; 
    }
    else//case where n is negative
    {
        cout << "Invalid input.\n";
    }           
    system("pause");
    return 0;
}
Think about how I used the for loop as you'll want to that to generate your cosine approximation.

Feel free to ask for any clarification.
 
Last edited:

sunny

meh.
Joined
Jul 7, 2002
Messages
5,350
Gender
Male
HSC
2002
Factorials are recursive by definition - the easiest way to solve it is to write a 3 or 4 line recursive program - much simpler than using iterations.
 

Slidey

But pieces of what?
Joined
Jun 12, 2004
Messages
6,600
Gender
Male
HSC
2005
Didn't I use recursion? Actually, I can't tell the difference.
 

HellVeN

Banned
Joined
Jun 26, 2004
Messages
532
Gender
Male
HSC
2005
That's cause you're a noob at life Slimer.


Recursion <> Repetition
 

jm1234567890

Premium Member
Joined
Aug 18, 2002
Messages
6,516
Location
Stanford, CA
Gender
Male
HSC
2003
sunny said:
Factorials are recursive by definition - the easiest way to solve it is to write a 3 or 4 line recursive program - much simpler than using iterations.
we were always taught that recursion uses more resources and is less efficient than repetition and should be avoided.

then again, we will be so screwed at programming competitions.
 

Slidey

But pieces of what?
Joined
Jun 12, 2004
Messages
6,600
Gender
Male
HSC
2005
Feel free to explain the difference to me. :p
 

acmilan

I'll stab ya
Joined
May 24, 2004
Messages
3,989
Location
Jumanji
Gender
Male
HSC
N/A
Recursion = when a method calls itself, or at least thats what its used for in java, havent tried doing it on c++ yet
 

sunny

meh.
Joined
Jul 7, 2002
Messages
5,350
Gender
Male
HSC
2002
jm1234567890 said:
we were always taught that recursion uses more resources and is less efficient than repetition and should be avoided.

then again, we will be so screwed at programming competitions.
This is generally true, but for most problems this is irrelevant. When it is relevant, you can still use dynamic programming over iterative methods. For most problems n has to be fairly big or computationally expensive to have much difference.

acmilan said:
Recursion = when a method calls itself, or at least thats what its used for in java, havent tried doing it on c++ yet
Recursion is a general programming technique - it will work in any high level language.
 

jm1234567890

Premium Member
Joined
Aug 18, 2002
Messages
6,516
Location
Stanford, CA
Gender
Male
HSC
2003
sunny said:
This is generally true, but for most problems this is irrelevant. When it is relevant, you can still use dynamic programming over iterative methods. For most problems n has to be fairly big or computationally expensive to have much difference.
Why can't you use dynamic prgramming methods with recursion?
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top