you will write two versions of a function that calculates the sum of all the numbers in a LinkedList. In the first version, you should use an iterator and either a while loop or a traditional for loop to compute the sum . In the second version, you should use the range - based for loop. Write one or more tests in a main() function that demonstrate that either version of your function works as intended.
I would personally like to just know how the functions should be written so I can finish this problem without any trouble. I mainly want to know how to write the iterator with the loops, the range-based loop on how that can be done, and finally, how to write the tests to make it work.
Hi, I have taken two versions of a function but taking two functions to explain 2 versions.
1) sumVersion1() - calculates the sum of all the numbers in a LinkedList using an Iterator
2) sumVersion2() - calculates the sum of all the numbers in a LinkedList using the range-based for loop
Program
import java.util.*;
class LinkedListSumDemo
{
public static int sumVersion1(LinkedList<Integer>
l1)
{
Iterator<Integer> i1=l1.iterator();
int sum=0;
while(i1.hasNext())
{
sum=sum+i1.next();
}
return sum;
}
public static int sumVersion2(LinkedList<Integer>
l1)
{
int lowLimit,uppLimit;
Scanner sc=new Scanner(System.in);
System.out.print("\n\nEnter a lowerLimit(must be greater than or
equal to 0) index : ");
lowLimit=sc.nextInt();
System.out.print("Enter a upper Limit(must be less than the size of
LinkedList) index: ");
uppLimit=sc.nextInt();
int sum=0;
for(int i=lowLimit;i<=uppLimit;i++)
{
sum=sum+l1.get(i);
}
return sum;
}
public static void main(String args[])
{
LinkedList<Integer> l1=new LinkedList<Integer>();
Scanner sc=new Scanner(System.in);
System.out.print("\nHow many numbers do you want in your list:
");
int nums=sc.nextInt();
int num;
for(int i=0;i<nums;i++)
{
System.out.print("Enter number "+(i+1)+": ");
num=sc.nextInt();
l1.add(num);
}
System.out.println("\nElements of a LinkedList: "+l1);
System.out.println("\n\nVersion1: ");
System.out.println("------------------------------------------------------");
System.out.println("\nThe sum of all the numbers using an Iterator:
"+sumVersion1(l1));
System.out.println("\n\nVersion2: ");
System.out.println("------------------------------------------------------");
System.out.println("\nThe sum of all the numbers within a range:
"+sumVersion2(l1));
System.out.println("\n");
}
}
Output:

you will write two versions of a function that calculates the sum of all the numbers...
a. write a program that calculates the sum of two numbers x and y and then the program should call the function by address and prints the sum b. write a program that stores 10 integers provided at run time in an array. the program should prompt the user for a random integer exit in the array, the value must be replaced by a-1
Write a function to calculate the sum of the reciprocals of a series of odd numbers. The function will have one input and no output, with the input being the ending value for the series of odd values. Write the function definition statement Initialize a variable to zero. This variable will contain the sum of all the values. Create a for loop that loops over all odd numbers from 1 to the specified ending value. Inside the loop, add the...
Step 2. Write a C function to passed two numbers to the function and calculate the sum of all the numbers between them (both inclusive). [50 marks] a. Make a version with for loop b. Make a version with while loop c. Make a version with do.. while loop d. Make a version with goto loop Tip : Try to use the same number of variables and almost the same logic in all the four loops To do: Using the...
Write two versions of the function mostFrequentCharacter, one that accepts a C-string and one that accepts a string object, as its argument. The function should return the character that appears most frequently in the string. Demonstrate the function in a complete program. Do not use any string or cstring functions in the cstring version.
****python****
Q1.
Write a recursive function that returns the sum of all numbers
up to and including the given value.
This function has to be recursive; you may not use loops!
For example:
Test
Result
print('%d : %d' % (5, sum_up_to(5)))
5 : 15
Q2,
Write a recursive function that counts the number of odd
integers in a given list.
This function has to be recursive; you may not use loops!
For example:
Test
Result
print('%s : %d' % ([2,...
Write a program that calculates the average of a stream of non-negative numbers. The user can enter as many non-negative numbers as they want, and they will indicate that they are finished by entering a negative number. For this program, zero counts as a number that goes into the average. Of course, the negative number should not be part of the average (and, for this program, the average of 0 numbers is 0). You must use a method to read...
Python
1 Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers, if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. 3 To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use...
Step 1. Write a program in assembly language (using macros) to print out the following messages on the screen [20 marks]: Hello, programmers! Welcome to the world of, Linux assembly programming! Step 2. Write a C function to passed two numbers to the function and calculate the sum of all the numbers between them (both inclusive). [50 marks] a. Make a version with for loop b. Make a version with while loop c. Make a version with do.. while loop...
Please answer this python questions.Thank you! Question Two Write a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what the salary was for each day, and then show the total pay at the end...
How do you write a JavaScript function to pass two numbers and return back the sum within HTML? (write very ease code with document.write...sum)