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

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.

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

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

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

kinda confused though

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.

i dont understand what condition i put for the if statement

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.

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

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

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

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

Thanks for your help as well! Pm me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.