1,105,290 Community Members

sum of integers in a range

Member Avatar
power_computer
Junior Poster
144 posts since Feb 2009
Reputation Points: 20 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

have to compute a function that calculates a sum in between a range let say user enters value of 10 and end value of 15 and then it is divided by a a divisor entered by the user say 2

I have the following code but its not working quite right

its a user defined function fyi

int Range (int start, int end, int divisor)
{
    int range, counter, sum;
    sum=0;
    
    
    for (counter=start; counter<end; counter++)
    {
        sum= start + counter;
        }
    
    range = sum / divisor;
    
   
    
return range;
}

the answer should equal 36
g. sum integers in a range that are evenly divisible by a specific integer

hmm edit ill work on this my code is wrong

Member Avatar
SeeTheLite
Junior Poster
109 posts since Mar 2009
Reputation Points: 20 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 0 [?]
 
0
 

the problem is you do not have an accumulator, this revised code should work

int Range (int start, int end, int divisor)
{
    int range, counter, sum;
    sum=0;
    
    
    for (counter=start; counter<end; counter++)
    {
        sum= sum + counter;
        }
    
    range = sum / divisor;
    
   
    
return range;
}

note I just changed start to sum, because sum is what you are using to count, sum is what you add to counter, which changes. Before you would only have two digits add each time, wipping the previous result because start is the same and counter changes.

Member Avatar
power_computer
Junior Poster
144 posts since Feb 2009
Reputation Points: 20 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

Sorry I found this description of what the assignment called for

g. sum integers in a range that are evenly divisible by a specific integer

Member Avatar
SeeTheLite
Junior Poster
109 posts since Mar 2009
Reputation Points: 20 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 0 [?]
 
0
 

Well you have your snippet right there >_>, just make an if statement and you are set.

Member Avatar
power_computer
Junior Poster
144 posts since Feb 2009
Reputation Points: 20 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

So it will read each integer if it divisble evenly then it will add it for the counter

kinda confused though

Member Avatar
SeeTheLite
Junior Poster
109 posts since Mar 2009
Reputation Points: 20 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 0 [?]
 
0
 

Eh, I don't wanna give it all for you, what I am saying, is make an if statement to check if the integer is divisible by your divisor first. If it is, accumulate it, if not, move on to the next number. If you don't understand this basic code, I suggest you read up on your books or look through the internet for some guides before coming here.

Member Avatar
power_computer
Junior Poster
144 posts since Feb 2009
Reputation Points: 20 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

i dont understand what condition i put for the if statement

Member Avatar
SeeTheLite
Junior Poster
109 posts since Mar 2009
Reputation Points: 20 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 0 [?]
 
0
 

Think about it really hard for 10 seconds... If you still can't figure it out,


if((count / divisor)%2 == 0) at least from what I can tell from what you are giving me.

Member Avatar
power_computer
Junior Poster
144 posts since Feb 2009
Reputation Points: 20 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

hmm

well its divided the number by the select integer if it is evenyl divided it add it all together


i did

div = counter/divisor;

if (div==0)
{
sum=sum+counter;

}

i didnt work to well

Member Avatar
SeeTheLite
Junior Poster
109 posts since Mar 2009
Reputation Points: 20 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 0 [?]
 
0
 

Well thats because you are never going to get 0 unless counter = 0, you have to use the modulus operator(remainder) to determine if it is divisible, look at the code more carefully next time :count / divisor)%2

Member Avatar
power_computer
Junior Poster
144 posts since Feb 2009
Reputation Points: 20 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

I just had to play around with it for a bit

//Function Definition for Choice 
int Range (int start, int end, int divisor)
{
    int range, counter, sum;
    sum=0;
    counter=0;
    
    for (counter=start; counter<=end; counter++)
    {
       range = (counter%divisor);
        
        if (range == 0)
        {
                     sum=sum+counter;
                     }
        
                     }


    
return sum;
}
Member Avatar
SeeTheLite
Junior Poster
109 posts since Mar 2009
Reputation Points: 20 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 0 [?]
 
0
 

Silly me, i know something was superflous T.T, good job though!

Member Avatar
power_computer
Junior Poster
144 posts since Feb 2009
Reputation Points: 20 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for your help as well! Pm me

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article