Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,262 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,508 people online right now. Registration is fast and FREE... Join Now!




summation and functions

 
Reply to this topicStart new topic

summation and functions

leachrj
10 Dec, 2006 - 03:32 PM
Post #1

New D.I.C Head
*

Joined: 10 Dec, 2006
Posts: 8


My Contributions
this program keeps giving me errors that i am using too many arguments in function term
i am attaching the assignment page someone please help.

CODE

#include <stdio.h>
#include <math.h>
long factorial(int n)
{
  int x;
  int fact = 1;
  for (x=1; x<=n; x++)
    fact = x*fact;
  return(fact);
}
float term(int n)
{
  return((pow(-1,n))*(1/factorial(n)));
}
int main()
{
  int j,n,d;
  int sum = 0;
  int i;
  printf("Enter n\n");
  scanf("%d",&n);
  
  for (j=0; j<=n; j++)
    sum += term(n,j);

  printf("Result = %d\n",sum);
  
  scanf("%i",&i);
  return(0);
}



Attached File(s)
Attached File  assign5.pdf ( 59.79k ) Number of downloads: 60
User is offlineProfile CardPM
+Quote Post

gordo
RE: Summation And Functions
10 Dec, 2006 - 03:38 PM
Post #2

New D.I.C Head
Group Icon

Joined: 7 Dec, 2006
Posts: 34


Dream Kudos: 25
My Contributions
in this line...

for (j=0; j<=n; j++)
sum += term(n,j);

you can only pass n or j, not both.

sum += term(n);
//or
sum += term(j);

you decide which.
User is offlineProfile CardPM
+Quote Post

leachrj
RE: Summation And Functions
10 Dec, 2006 - 04:00 PM
Post #3

New D.I.C Head
*

Joined: 10 Dec, 2006
Posts: 8


My Contributions
i changed the line for which you said but now i am getting 0 for the answer what is happening i am desperate help
User is offlineProfile CardPM
+Quote Post

eXceed69
RE: Summation And Functions
10 Dec, 2006 - 05:33 PM
Post #4

"Super Sentai Knight Of DawN"
Group Icon

Joined: 12 Nov, 2006
Posts: 682



Thanked: 1 times
Dream Kudos: 675
My Contributions
your returning so many value without a function to catch it,

CODE
sum += term(n[j]);


and just create a 2 catch onyour main function.
CODE

#include <stdio.h>
#include <math.h>

long factorial(int n)
{
int x;
int fact = 1;
for (x=1; x<=n; x++)
fact = x*fact;
return(fact);
}



float term(int n)
{
return((pow(-1,n))*(1/factorial(n)));
}


int main()
{

int j,n,d;
int sum = 0;
int i;
printf("Enter n\n");
scanf("%d",&n);

for (j=0; j<=n; j++)
{
sum += term(n[j]);

printf("Result = %d\n",sum);

scanf("%i",&i);
}
return(0);

}



This post has been edited by eXceed69: 10 Dec, 2006 - 05:56 PM
User is offlineProfile CardPM
+Quote Post

Dark_Nexus
RE: Summation And Functions
10 Dec, 2006 - 05:46 PM
Post #5

or something bad...real bad.
Group Icon

Joined: 2 May, 2004
Posts: 1,309



Thanked: 3 times
Dream Kudos: 625
My Contributions
CODE

for (j=0; j<=n; j++)
{
sum += term(n[j]);

printf("Result = %d\n",sum);

scanf("%i",&i);
return(0);

}


this shouldn't compile because you are missing a } bracket.
also, the last for loop in your main function includes a return inside of the for loop, meaning that the for loop will only iterate once, and then it will return 0;
User is offlineProfile CardPM
+Quote Post

shakti8ie
RE: Summation And Functions
14 Dec, 2006 - 02:24 AM
Post #6

New D.I.C Head
*

Joined: 11 Dec, 2006
Posts: 21



Thanked: 1 times
My Contributions
CODE

#include <stdio.h>
#include <math.h>

long factorial(int n)
{
  int x;
  int fact = 1;
  for (x=1; x<=n; x++)
    fact = x*fact;
  return(fact);
}
float term(int n)
{
  return((pow(-1,n))*(1/(float)factorial(n)));
}
int main()
{
  int j,n,d;
  float sum = 0.0;

  printf("Enter n\n");
  scanf("%d",&n);
  
  for (j=0; j<=n; j++)
    sum += term(j);

  printf("Result = %f\n",sum*factorial(n));
  
  return(0);
}


Hi,

this code should work...the problem was u have to some typecasting while returning value from term()...this is the reason why u r getting 0 as output...again print the result as sum*factorial(n)...put the argument of term() as j in the for loop...right?
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:04PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month