Question

Find the sum of squares 1^2 + 2^2 + ... n^2 using iteration and recursion

* I'm guessing we need to modify it

Here's the code unmodified

public class Sum {

//Non recursive sum

public static long sum1 (int n) {

long sum = 1L;

for (int i = 2; i <= n; ++i)

sum = sum + i *i ;

return sum;

}

//Recursive sum

public static long sum2 (int n) {

if (n < 2)return 1L;

return sum2(n - 1) + n * n;

}

}

Answer #1

Write a method that returns the sum of all the elements in a
specified column in a 3 x 4 matrix using the following header:
public static double sumColumn(double[][] m, int
columnIndex)
The program should be broken down into methods, menu-driven, and
check for proper input, etc.
The problem I'm having is I'm trying to get my menu to execute
the runProgram method. I'm not sure what should be in the
parentheses to direct choice "1" to the method. I'm...

Write a recursive method public static int
sumEveryOther(int n) that takes a positive int as an
argument and returns the sum of every other int from n down to
1.
For example, the call sumEveryOther(10) should return 30, since
10 + 8 + 6 + 4 + 2 = 30.
The call sumEveryOther(9) should return 25 since 9 + 7 + 5 + 3 +
1 = 25.
Your method must use recursion.

QUESTION 1
For the following recursive function, find f(5):
int f(int n)
{
if (n == 0)
return 0;
else
return n * f(n - 1);
}
A.
120
B.
60
C.
1
D.
0
10 points
QUESTION 2
Which of the following statements could describe the general
(recursive) case of a recursive algorithm?
In the following recursive function, which line(s) represent the
general (recursive) case?
void PrintIt(int n ) // line 1
{ // line 2...

Find a formula for the sum of the squares of the numbers 1, 2,
...., n and prove it by induction.

There is a Java program that is missing one recursive function:
public class Fibonacci {
/* / 0 when n = 0
* fib(n) = | 1 when n = 1
* \ fib(n-1)+fib(n-2) otherwise
*/
public static int fib(int n) {
return 0;
}
/* Fibonacci Test Framework
*
* Note, this takes a long time to compute fib(44).
*/
public static void main(String[] args) {
int[] input = { 11, 22, 33, 44};
int[] expect = { 89,...

Write a for loop that prints: 1 2 ... userNum
Ex: userNum = 4 prints:
1 2 3 4
import java.util.Scanner;
public class ForLoops {
public static void main (String [] args) {
int userNum;
int i;
Scanner input = new Scanner(System.in);
userNum = input.nextInt();
for (/* Your code goes here */) {
System.out.print(i + " ");
}
}
}
Need to fill out the "for" solved in JAVA

Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3
+ 1/4 + … 1/n.
Let’s estimate natural using n = 20.
sum_thread.cpp
#include <chrono>
#include <iostream>
#include <mutex>
#include <random>
#include <utility>
#include <vector>
#include <thread>
using namespace std;
constexpr long long size= 1000000;
mutex myMutex;
void sumUp(unsigned long long& sum, const vector<int>& val,
unsigned long long beg, unsigned long long end){
long long localSum = 0;
for (auto it= beg; it < end; ++it){
localSum+=...

Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3
+ 1/4 + … 1/n.
Let’s estimate natural using n = 20.
sum_thread.cpp
#include <chrono>
#include <iostream>
#include <mutex>
#include <random>
#include <utility>
#include <vector>
#include <thread>
using namespace std;
constexpr long long size= 1000000;
mutex myMutex;
void sumUp(unsigned long long& sum, const vector<int>& val,
unsigned long long beg, unsigned long long end){
long long localSum = 0;
for (auto it= beg; it < end; ++it){
localSum+=...

1) Consider the following Java program. Which statement updates
the appearance of a button?
import java.awt.event.*;
import javax.swing.*;
public class Clicker extends JFrame implements ActionListener
{
int count;
JButton button;
Clicker() {
super("Click Me");
button = new
JButton(String.valueOf(count));
add(button);
button.addActionListener(this);
setSize(200,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
count++;
button.setText(String.valueOf(count));
}
public static void main(String[] args) { new
Clicker(); }
}
a. add(button);...

1) Consider the following Java program, which one of the
following best describes "setFlavor"?
public class Food {
static int count;
private String flavor = "sweet";
Food() { count++; }
void setFlavor(String s) { flavor = s; }
String getFlavor() { return flavor; }
static public void main(String[] args) {
Food pepper = new
Food();
System.out.println(pepper.getFlavor());
}
}
a. a class variable
b. a constructor
c. a local object variable
d....

ADVERTISEMENT

Get Answers For Free

Most questions answered within 1 hours.

ADVERTISEMENT

asked 3 minutes ago

asked 17 minutes ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 2 hours ago

asked 2 hours ago

asked 2 hours ago

asked 2 hours ago

asked 3 hours ago