Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Modify your Sum all even script so that given an integer n you generate a vector vec that contains all of the ODD numbers between 1 and 2^n. Then add them all up in the variable out.

조회 수: 1 (최근 30일)
Instructions: To solve this problem, modify the template bellow with your code. Leave the name of the function and variables unchanged. Otherwise your code will not pass the test.
function out = sumallodd(n) % Put your code here end
  댓글 수: 3
John Chilleri
John Chilleri 2017년 2월 9일
편집: John Chilleri 2017년 2월 9일
Hints:
1. Somehow go through each number from 1 to 2^n.
2. Figure out how to test if they're odd (one option is using the mod function).
3. Somehow store them in a vector vec if they're odd.
4. Somehow make out the sum of all elements in vec.
Note: consider what value the variable out should initially equal before adding the odd numbers.
Guillaume
Guillaume 2017년 2월 9일
2. can be avoid by changing 1. to go through each number from 1 to 2^n in steps of 2.

답변 (1개)

Roger Stafford
Roger Stafford 2017년 2월 10일

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by