Question
1. Assume the int variables i, lo, hi, and result have been declared and that lo and hi have been initialized. Write a for loop
1. Assume the int variables i, lo, hi, and result have been declared and that lo and hi have been initialized. Write a for loop that adds the integers between lo and hi (inclusive), and stores the result in result. Your code should not change the values of lo and hi. Also, do not declare any additional variables -- use only i, lo, hi, and result.
My answer is :
for(i=lo;i<=hi;i++) { result=result+i; }
But I was told that there is a logical error. So how can I fix this? Thank you.
- In every test case: The value of result is incorrect
- In some test cases: result was not assigned a value
- You are using an incorrect number somewhere in your solution
2. Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out the sum of all the even integers read and the sum of all the odd integers read(The two sums are separated by a space). Declare any variables that are needed.
My answer is :
int sum=0; int num=1; while(num > 0){ cin >> num; if ((num % 2)==0 & (num>0)){ sum+=num; } } cout << sum;
But there is still some errors.
- In every test case: stdout is not correct
- You almost certainly should be using: "
- I haven't yet seen a correct solution that uses: &
- We think you might want to consider using: else
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started