Advertising | Loans | Loans | Credit Cards | Web Advertising
C Programming [Archive] - PCMech Forums

PDA

View Full Version : C Programming


DavidMX6
04-05-2004, 07:49 PM
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 <math.h>
#include <stdio.h>

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

DavidMX6
04-05-2004, 08:14 PM
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 <math.h>
#include <stdio.h>

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

AerynSedai
04-05-2004, 11:50 PM
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

ThePoor
04-06-2004, 01:41 PM
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);
}

aym
04-06-2004, 01:46 PM
why should fMax be global?

I think using %f with scanf when reading a float will fix the problem.

Jaggannath
04-07-2004, 03:17 AM
Yeah, I don't think it needs to be a global, nothing else really needs to access it

ThePoor
04-07-2004, 10:20 AM
..

doctorgonzo
04-07-2004, 12:00 PM
Here is code that works well:

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


Since you are using whole numbers, I changed the variables to ints. Also, I changed sumofcubes into a recursive function to demonstrate how it works. For example, computing sumofcubes(10) results in computing 10*10*10 + sumofcubes(9), then 10*10*10 + 9*9*9 + sumofcubes(8), etc. until you get down to 1.