1.11M Members

sum of integers in a range

 
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

 
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.

 
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

 
0
 

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

 
0
 

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

kinda confused though

 
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.

 
0
 

i dont understand what condition i put for the if statement

 
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.

 
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

 
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

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

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

 
0
 

Thanks for your help as well! Pm me

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