Reputation: 33
I was assigned to write this code and it seemed pretty simple at first. I wrote it out and tried to understand it as best as I could and really thought I got it down. But when I tried to check the code using VisualStudio, errors with the code popped up and the code would not finishing processing.
Here's the assignment:
Write a function named specialNumbers that computes and returns the total number of integers between two target numbers that are divisible by 3. The function takes two parameters: 1. start, an integer 2. end, an integer bigger than start The function returns the total number of multiples of 3, between start and end, inclusive. For example, if start=3, end=10, the function would return 3.
Here is the code I have so far:
#include <iostream>
using namespace std;
int specialNumbers(int start, int end) {
int count = 0;
for (int i = start; i < end; i++) {
if (i % 3 == 0)
count++;
}
return count;
}
int main() {
int a, b;
cout << "Enter two numbers to find total number of integars divisble by 3 between them" << endl;
cin >> a, b;
int n = specialNumbers(a, b);
cout << "Number of integars divisible by 3 between" << a << "and" << b << "are" << n << endl;
return 0;
}
The error is displays is
Debug Error! The variable b is used and not initialized
Upvotes: 3
Views: 822
Reputation: 4299
The other answers addressed proper use of cin
however there is also an algorithm error. The code returns an incorrect result if the last number is a multiple of 3.
For instance, if you enter the numbers 4 and 6 it will return 0 when 6 is a multiple of 3 when it should return 1.
This can be corrected by changing:
for (int i = start; i < end; i++) {
to
for (int i = start; i <= end; i++) {
However, the algorithm is extremely inefficient when the integers are far apart. Examining each integer would be time consuming if they are, for instance, 15 and 2,000,000,000. This is like multiplying two numbers by just adding one of them over and over.
This code first notices that the number of values divisible exactly by 3 will be unchanged if a multiple of 3 is subtracted from both the start and end values.
Second, after doing this the number of values inclusively between start and end will be exactly the value of end/3
incremented by one if start==0
Thus this code will produce the correct answer without looping:
#include <iostream>
using namespace std;
int specialNumbers(int start, int end) {
int adj = 3 * (start / 3); // find and remove the multiples of 3 from start and end
start = start - adj; // start will now be either 0, 1 or 2
end = end - adj;
int count = end / 3; // count is the number of exact multiples of 3 in the sequence
if (start == 0) // unless the adjusted start is zero in which case it must be incremented
count++;
return count;
}
int main() {
int a, b;
while (1) {
cout << "Enter two numbers to find total number of integars divisble by 3 between them" << endl;
cin >> a >> b;
int n = specialNumbers(a, b);
cout << "Number of integars divisible by 3 between" << a << "and" << b << "are" << n << endl;
}
return 0;
}
Upvotes: 1
Reputation: 4359
You're using the wrong syntax to extract two ints from cin
, it should be:
cin >> a >> b;
Your code gives an "uninitialized error" because of the semantics of the comma operator, which takes two arguments and returns the latter.
Put simply, your code is equivalent to:
(cin >> a), b; // ERROR: `b` isn't being initialized.
Upvotes: 3
Reputation: 73394
ChangeChange this:
cin >> a, b;
to:
cin >> a >> b;
since you definetely don't want the comma operator in this case, but the >>
operator.
Upvotes: 0