|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (4 bit)
Join Date: Apr 2003
Location: TX
Posts: 9
|
C Programming
Having some trouble making a program. Here's what it asks for:
Write a program to compute the sum of the series 1^3+2^3+….…+iMax^3. The user has to input the maximum value iMax for the series. The printf and scanf associated with this input for iMax should be inside the main function. The computation part should be done by a user-defined function. Call it ‘SumofCubes’. It will have one argument. The printf() for the final answer can be inside either main or the SumofCubes function. And here is what I have so far: #include #include float sumofcubes(float); int main(void) { float fMax; float fSum; printf("Please enter a maximum value.\n"); scanf("%d", &fMax); iSum = sumofcubes(); printf("The sum is %d", fSum); return 0; } float sumofcubes(float fMax) { int iCount; float fValue; for (iCount=1; iCount<=iMax; iCount=iCount+1) fValue=pow(iCount,3); return(fValue); } Any help would be greatly appreciated! Thanks |
|
|
|
|
|
#2 |
|
Member (4 bit)
Join Date: Apr 2003
Location: TX
Posts: 9
|
Changed it up a little bit, but it still doesn't work. It will compile, but when I run it, it gives the same very large negative number for the result even when I change the input number.
#include #include float sumofcubes(float fMax); int main(void) { float fMax; float fSum; printf("Please enter a maximum value.\n"); scanf("%d", &fMax); fSum = sumofcubes(fMax); printf("The sum is %d", fSum); return 0; } float sumofcubes(float fMax) { int iCount; float fValue; for (iCount=1; iCount<=fMax; iCount=iCount+1) fValue = fValue + pow(iCount,3); return(fValue); } |
|
|
|
|
|
#3 |
|
Member (7 bit)
Join Date: Sep 2003
Posts: 90
|
the few things i changed are:
1. have the user input an integer, unless you really want floats 2. float sumofcubes( int ), unless you really want floats other than that, the only problem is your scanf and printf. you are using %d which means you are asking for an integer, but casting it as a float. floating points have a vastly different encoding than 2's complements and thus you get wild garbage. use %f. AS |
|
|
|
|
|
#4 |
|
Member (9 bit)
Join Date: Oct 2003
Location: NorthEastern USA
Posts: 369
|
you should make the fMax to global variable.
float fMax; float sumofcubes(float); float sumofcubes(float fMax) { int iCount; float fValue; for (iCount=1; iCount<=fMax; iCount++) fValue = fValue + pow(iCount,3); return(fValue); } |
|
|
|
|
|
#5 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
why should fMax be global?
I think using %f with scanf when reading a float will fix the problem. Last edited by aym; 04-06-2004 at 12:49 PM. |
|
|
|
|
|
#6 |
|
Gremlin Overlord
Join Date: Apr 2003
Location: Australia
Posts: 2,382
|
Yeah, I don't think it needs to be a global, nothing else really needs to access it
|
|
|
|
|
|
#7 |
|
Member (9 bit)
Join Date: Oct 2003
Location: NorthEastern USA
Posts: 369
|
..
|
|
|
|
|
|
#8 |
|
Professional gadfly
|
Here is code that works well:
Code:
int sumofcubes(int fMax);
int main(void)
{
int intMax;
int intSum;
printf("Please enter a maximum value.\n");
scanf("%d", &intMax);
intSum = sumofcubes(intMax);
printf("The sum is %d \n", intSum);
system("pause");
return 0;
}
int sumofcubes(int intMax)
{
if(intMax==1) {
return 1;
} else {
return (intMax*intMax*intMax) + sumofcubes(intMax-1);
}
}
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|