Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 04-05-2004, 06:49 PM   #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
DavidMX6 is offline   Reply With Quote
Old 04-05-2004, 07:14 PM   #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);
}
DavidMX6 is offline   Reply With Quote
Old 04-05-2004, 10:50 PM   #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
AerynSedai is offline   Reply With Quote
Old 04-06-2004, 12:41 PM   #4
Member (9 bit)
 
ThePoor's Avatar
 
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);
}
ThePoor is offline   Reply With Quote
Old 04-06-2004, 12:46 PM   #5
aym
Registered User
 
aym's Avatar
 
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.
aym is offline   Reply With Quote
Old 04-07-2004, 02:17 AM   #6
Gremlin Overlord
 
Jaggannath's Avatar
 
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
Jaggannath is offline   Reply With Quote
Old 04-07-2004, 09:20 AM   #7
Member (9 bit)
 
ThePoor's Avatar
 
Join Date: Oct 2003
Location: NorthEastern USA
Posts: 369
..
ThePoor is offline   Reply With Quote
Old 04-07-2004, 11:00 AM   #8
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
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);
    }
}
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.
doctorgonzo is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 12:51 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2