Question
Last function in program is returning a negative number, I think it's because of overflow but I can't tell For this project you will be
Last function in program is returning a negative number, I think it's because of overflow but I can't tell
For this project you will be designing and implementing a system, in either C or C++, to answer a few mathematical questions.First, given the number 123456789, is it possible to find a permutation (i.e. a rearrangement that preserves the count of each number) such that the left most digit is evenly divisible by 1, the two left most digits are evenly divisible by 2, the three left most digits are divisible by 3 and so on? For example, in 123456789, 1 is evenly divisible by 1, 12 is evenly divisible by 2, 123 is evenly divisible by 3, but because 1234 is not evenly divisible by 4 this number is not a solution to the question.
Second, is it possible to find a similar permutation given the number 1234567890? Finally, is it possible to find a similar permutation for any of the given hexadecimal numbers, 1234567890AB, 1234567890ABC,1234567890ABCD, 1234567890ABCDE, 1234567890ABCDEF?
The purpose of this program is to find solutions to a permutation of numbers such that the first digit of each permutation is divisible by 1, the first two digits are divisible by two, and so on and so forth. For instance the number 123 fits the first three requirements because 1 is divisible by 1, 12 is divisible by 2, and 123 is divisible by 3. However 1234 does not meet the requirements because 1234 is not divisible by 4. So far I've written code that accurately solves this requirement for all digits 1-9. But for the 10th digit I have to cycle through all the digits again and incorporate 0-9 instead of 1-9.
On my last function I test to see which solutions for 0-9 for ten digit numbers are divisible by 10, and I don't get any results, which is odd, because I should get at least one result if the first run was any indication. When I added a cout to print the value of the numbers the function was testing, it printed out a negative number. I was pretty sure I'm using the correct variable types, and I even casted the different variables to make sure they were all the same. Please help...
The following is a header file in which I define a class object that stores all the solutions to the problem and also a function to check for repeating digits
#include#include #pragma once using namespace std; //class to store the different digits and arrange them into integers class Num { public: vector FirstDigit; //stores the first digit vector SecondDigit; //stores the second digit vector ThirdDigit; // stores the third digit vector FourthDigit; // so on vector FifthDigit; // and so forth. vector SixthDigit; vector SeventhDigit; vector EighthDigit; vector NinthDigit; vector FirstDigitrun2; //stores the first digit vector SecondDigitrun2; //stores the second digit vector ThirdDigitrun2; // stores the third digit vector FourthDigitrun2; // so on vector FifthDigitrun2; // and so forth. vector SixthDigitrun2; vector SeventhDigitrun2; vector EighthDigitrun2; vector NinthDigitrun2; vector TenthDigit; vector EleventhDigit; vector TwelfthDigit; vector ThirteenthDigit; vector FourteenthDigit; vector FifteenthDigit; vector SixteenthDigit; int checker(int oldnum, int digitcheck, int skip) { int i = oldnum; vector check; int counter = 0; while (i != 0) { check.push_back(i % 10); if (check[counter] == digitcheck) { skip = 1; // new digit is a repeat return skip; } i /= 10; counter++; } return skip = 0; } private: };
The following are the last two sections of code which I believe the problem to resides.
Num Digit9(Num number, int run) { int multiplier = 10; if (run == 1) { cout << "Solutions for nine digit numbers: "; for (int i = 0; i < number.EighthDigit.size(); i++) { for (int j = 1; j < 10; j++) { int newnum = multiplier*number.EighthDigit[i] + j; int skip = 0; skip = number.checker(number.EighthDigit[i], j, skip); if (skip == 1) { continue; } else if (newnum % 9 == 0) { number.NinthDigit.push_back(newnum); } } } for (int i = 0; i < number.NinthDigit.size(); i++) { cout << number.NinthDigit[i] << endl << endl; } Digit10(number, run); } if (run == 2) { for (int i = 0; i < number.EighthDigitrun2.size(); i++) { for (int j = 0; j < 10; j++) { int newnum = multiplier*number.EighthDigitrun2[i] + j; int skip = 0; skip = number.checker(number.EighthDigitrun2[i], j, skip); if (skip == 1) { continue; } else if (newnum % 9 == 0) { number.NinthDigitrun2.push_back(newnum); } } } Digit10(number, run); } return number; } Num Digit10(Num number, int run) { unsigned long long int multiplier = 10; if (run == 1) { run = 2; Digit1(number, run); } else if (run == 2) { cout << "Solutions for ten digit numbers: "; for (int i = 0; i < number.NinthDigitrun2.size(); i++) { for (unsigned long long int j = 0; j < 10; j++) { int longconverter = number.NinthDigitrun2[i]; int newnum = multiplier* (unsigned long long int) longconverter + j; cout << newnum << endl; int skip = 0; skip = number.checker(number.NinthDigitrun2[i], j, skip); if (skip == 1) { continue; } else if (newnum % 10 == 0) { number.TenthDigit.push_back(newnum); } } } for (int i = 0; i < number.TenthDigit.size(); i++) { cout << number.TenthDigit[i] << endl << endl; } } return number; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored 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