Question

Write a non-void method named sum that is passed two int parameters when it is called....

Write a non-void method named sum that is passed two int parameters when it is called. It then uses a for-loop to compute and return the value of the sum of all the numbers between the value of the first parameter and the second, inclusive. This method does not display anything.

Give an example of how this method would be called, to produce output

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer

Editable code

//non-void method named sum, whose return type is int. It take two integer parameters

int sum(int a,int b)
{
//total is initialized with 0 and it will be used to store value of desired sum.
int total=0;
  
//for loop to compute sum
//i will start from a and it will iterate till b, because we need both number inclusive in sum.
for(int i=a;i<=b;i++)
{
// sum is being performed here
total=total+i;
/*
working of for loop
let us suppose a=1 and b=5
initially i=1, total=0 and i will iterate till b.
  
total=total + i
0 = 0 + 1 ---> total=1
1 = 1 + 2 ---> total=3
3 = 3 + 3 ---> total=6
6 = 6 + 4 ---> total=10
10 =10 + 5 ---> total=15
*/
}
//return statement to return value of total(contains desired sum)
return total;
}

Code

Working c++ code

Output

Editable code

//C++ program to find sum using for loop
#include <iostream>
using namespace std;
//non-void method named sum, whose return type is int. It take two integer parameters
int sum(int a,int b)
{
//total is initialized with 0 and it will be used to store value of desired sum.
int total=0;
//for loop to compute sum
//i will start from a and it will iterate till b, because we need both number inclusive in sum.
for(int i=a;i<=b;i++)
{
// sum is being performed here
total=total+i;
/*working of for loop
let us suppose a=1 and b=5
initially i=1, total=0 and i will iterate till b.
  
total=total + i
0 = 0 + 1 ---> total=1
1 = 1 + 2 ---> total=3
3 = 3 + 3 ---> total=6
6 = 6 + 4 ---> total=10
10 =10 + 5 ---> total=15
*/
}
//return statement to return value of total(contains desired sum)
return total;
}
int main()
{
//variables a and b will be entered by user
int a,b;
cout<<"Enter a : ";
cin>>a;
cout<<"\nEnter b : ";
cin>>b;
//sum will return answer i.e. an integer value , so we need to store that in some integer first.
/*calling of sum function*/
int tot_sum=sum(a,b);
//printing value of tot_sum
cout<<"\nsum = "<<tot_sum;
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a non-void method named sum that is passed two int parameters when it is called....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Make a public class called ManyExamples and in the class... Write a function named isEven which...

    Make a public class called ManyExamples and in the class... Write a function named isEven which takes in a single int parameter and returns a boolean. This function should return true if the given int parameter is an even number and false if the parameter is odd. Write a function named close10 which takes two int parameters and returns an int. This function should return whichever parameter value is nearest to the value 10. It should return 0 in the...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • Homework Question Write a void function called transformArray that takes two parameters - a reference to...

    Homework Question Write a void function called transformArray that takes two parameters - a reference to a pointer to a dynamically allocated array of ints, and the size of that array.  The pointer is passed by referencebecause you want to change the value of the pointer. The function should dynamically allocate an array that is twice as long, filled with the values from the original array followed by each of those values times 2. For example, if the array that was...

  • Write a function template named summarray, with two parameters: and array called are and an int...

    Write a function template named summarray, with two parameters: and array called are and an int holding the number of elements in arr. The function should return the sum of the elements in arr. this template must work for double or int arrays. Show a sample function call to the template function you wrote. What would you have to do to use the templates sumArray function with an array of objects of a custom class?   

  • help ASAP 3. Write a string C++ function named UnsignedPartialSum() that takes two string parameters and...

    help ASAP 3. Write a string C++ function named UnsignedPartialSum() that takes two string parameters and an int parameter. If both string parameters represent binary numbers and the int parameter is equal to a positive number less than or equal to the length of the longest string parameter, the function should return a binary string whose length is equal to two times the length of the maximum length of the two string parameters whose value is equal to the sum...

  • Write a method named printReverse() that accepts an int array as its parameter and prints that...

    Write a method named printReverse() that accepts an int array as its parameter and prints that array in reverse order. This method should not have a return statement (void). - All elements should be printed on the same line, separated by a space - A new line should be printed after the entire array prints - If the array passed in held the values: {3,2,1} - Output: 1 2 3 //newline printed here

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Write a method named factorial that accepts an integer n as a parameter and returns the...

    Write a method named factorial that accepts an integer n as a parameter and returns the factorial of n, or n!. A factorial of an integer is defined as the product of all integers from 1 through that integer inclusive. For example, the call of factorial(4) should return 1 2 3 4, or 24. The factorial of 0 and 1 are defined to be 1. You may assume that the value passed is non-negative and that its factorial can fit...

  • Java arrays Create method named repeatedN that takes two integer parameters named range and n and...

    Java arrays Create method named repeatedN that takes two integer parameters named range and n and returns an integer array Method should return an integer array that has numbers 0 - range repeated in order n times If range is less than or equal to 0; return an array that has 0 repeated n times Create a second method named printArray that takes an integer array as a parameter. The method should print out every element on the same line...

  • A method called linearSearch(), which takes as parameters an array of int followed by three values...

    A method called linearSearch(), which takes as parameters an array of int followed by three values of type int, and returns a value of type int. The first int parameter represents a key, the second int parameter represents a starting position, and the third int parameter represents an end position. If the key occurs in the array between the start position (inclusive) and the end position (exclusive), the method returns the position of the first occurrence of the key in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT