Hello all! First time posting here, my friend just recently recommended it.
First semester c++ course and we are on do/while and for loops.
here is the question (i've seen it posted here before but its slightly different in this question):
let n = ak ak-1 ak-2....a1 a0 be an integer and t = a0 - a1 a2 - ... +(-1)k ak. it is known that n is divisible by 11 if and only if t is divisible by 11. For example, suppose that n = 8784204. Then, t = 4 - 0 + 2 - 4 + 8 - 7 +8 = 11. Because 11 is divisible by 11, 8784204 must be divisible by 11. Write a C++ program that prompts the user to enter a positive integer and then uses this criteron to determine whether the number is divisible by 11.
Hint: you need the following functions to solve the program. (1) the string length function to determine the size of the string of digits. (2) the string at function to extract each of the digits (in character form) (3) the type cast function to convert each digit from char to int. (4) subtract 48 from the converted digit (e.g. '4' = 52 in ASCII) to get the digit in decimal form.
I feel a for-loop with multiple declared variables would be better than a do-while loop in this situation. That way you'd be able to create a 'sign' variable (probably of bool type because you have a binary either-or option) that you could then test conditionally to see if you're adding or subtracting.
There are other things about this code that need to be fixed, but that's certainly the first thing to be addressed before continuation.
Better; the most appropriate loop is now being used. Also, you're condition is wrong in your for statement. You're going to want to be able to access the 0th element of your string (as that is the first digit of the number), so i > 0 should actually be i >= 0. Now expanded a bit with the bits you'll need.
1 2 3 4 5 6 7 8 9 10 11 12
bool positive = true;
int digit_sum = 0;
for (int i = input - 1; i >= 0; i--)
{
char x = inputChar.at(i);
int y = (x - 48);
//check to see if y is going to be positive or negative based on positive variable
//change sign of y if necessary
//add correctly signed y to digit_sum
///change value of positive
}
//check to see if digt_sum is divisible by 11
Take advantage of using a bool variable. If it is true, then the next number you're adding to digit_sum is going to be positive. If it is false, the next number you're adding is going to be negative.
So something like:
1 2
if ( positive ) dig_sum += y;
else dig_sum -= y;
Checking the i variable is also dangerous in this case because you don't know whether the last number in inputted number is going to have a positive or negative index.
#include < string > ;
#include <iostream>;
usingnamespace std;
int main()
{
int decision = 1, input;
string inputChar;
while (decision != 0)
{
cout << "Enter a positive integer" << endl;
cin >> inputChar;
input = inputChar.length();
int digit_sum = 0;
for (int i = input - 1; i >= 0; i--)
{
char x = inputChar.at(i);
int y = (x - 48);
if (i % 2 != 1)
{
digit_sum = digit_sum + y;
}
else
digit_sum = digit_sum - y;
}
if (digit_sum % 11 != 0)
cout << "This integer is not divisible by 11" << endl;
else
cout << "This integer is divisible by 11" << endl;
cout << "Would you like to continue? (0 to exit, 1 to continue)" << endl;
cin >> decision;
}
}
just to follow up, checking the i variable allows me to figuratively set the sign. because of the way the code is written (using the string function) i = input - 1 will always send the first loop through the else statment and switching each time.
Actually, you are correct there. I was thinking the first digit had to be positive, but so long as you alternate signs, the final sum will be divisible be 11 either way. Good job. :P