Alright, so I have this program that I'm writing and I need to find all the possible outcomes of a the last 5 digits of a 10 digit number. So, I have the user input the first four numbers, then an IF statement to see if the the 4th number is odd or even, so that the 5th number is set to the opposite (IE 4th = odd, 5th = even; vic versa) Then I have a vector that I want to randomly generate all possible outcomes of the last 5 numbers that sum 33.
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <vector>
#include <algorithm>
usingnamespace std;
int main()
{
vector<int> teleNumbs;
vector<int> geneNumbs;
int telenum;
int oddEven = 0;
int iRand;
srand(time(NULL));
cout << "Please enter the first four digits of the telephone number: " << endl;
for( int i=0; i<4; i++){
cout << " Enter number " << i+1 << ": " << endl;
cin >> telenum;
teleNumbs.push_back(telenum);
}
if( teleNumbs[3] % 2 == 0 ){
oddEven = 1;
}
else{ oddEven = 2;}
//Now I want to find all possible outcomes totaling 33 and print those possible outcomes in this format:
//(xxx) xxx xxxx
// I want the last 5 digits from the geneNumbs vector to total 33 in all possible ways
return 0;
}
The list of five digit numbers that sum to 33 will always be the same. (There are 1745 of them.)
Here's some pseudocode to do it:
1 2 3 4 5 6 7 8
for (int n = 0; n < 100000; n++)
{
int sum = 0;
for (int x = n; x; x /= 10)
sum += x;
if (sum == 33)
cout << setw( 5 ) << setfill( '0' ) << right << n << endl;
}
That's not tested. Here's the code I actually used to do it in Tcl:
for {set n 0} {$n < 100000} {incr n} {
set ns [split $n {}]
set sum [expr [join $ns +]]
if {$sum == 33} {puts $ns}
}
The only thing you'll have to do is go through the 5 possible values for the fifth digit (odd or even), which will bring your grand total to 8725 answers.
Hope this helps.
[edit] Fixed code to put each number on its own line...
[edit] Fixed code typo to check against the correct variable...
Your code didn't have any results that pertained to my question. I'm not sure what you were trying to do but I think you misunderstood what I was trying to do, unless you care to explain further.
Your code didn't have any results that pertained to my question.
Oh?
... Then I have a vector that I want to randomly generate all possible outcomes of the last 5 numbers that sum 33.
My code did exactly that; it produces all five-digit numbers whose digits sum to 33. If that isn't what you want, then why did you ask for it?
To print a number as you want it, use the formatted output functions.
In the following snippet, the variables are:
first_four - The first four digits of the telephone number
current_5th - The fifth digit, which will be (as the loop rolls) 1,3,5,7,9 or 0,2,4,6,8
n - The same as the previous snippet I gave you
You will, of course, have to put in the loop for current_5th and the loop as above, and replace the output with the following.
That may work as a set of numbers, which I'm sure is much more efficient. Though, as you can see by the vector initializations, I would like each digit stored in there respective vector position, so then I can output the proper output using each element, such as:
Combinations don't care about order. So "5 2 9 8 7" is the same as "8 9 2 7 5". (Repeats are possible, but I am still unsure if you want them...)
For permutations, order does matter, just as it does for normal telephone numbers.
If you want all possible numbers, but you want individual digits stored as separate integers in a vector, then you need to improve the way you will store your numbers (such as a vector of vectors), or do you just plan to count them off in fives?
I am still unconvinced you answered my question about the 'teleNumbs' and 'geneNumbs' variables. A complete phone number is ten digits. Should the 'geneNumbs' variable have the complete phone number, or do you wish it to have only the last five, or what?
For example, do you want each output to be 'teleNumbs' + 5th digit + 'geneNumbs' or something like that?
-----
The algorithm I have showed you works like this: by starting at 00000 and counting to 99999, you have every possible five-digit number. This is easily done in a loop by simply adding one to a variable every time.
Each time through the loop, you want to know if the digits sum to 33 (or 30 or whatever). To do this you'll need a way to separate and sum the digits. (My example above does just that for you.) Once you have their sum, if it matches the desired sum, then you can add the digits to your list of answers.
Yes, I want combination, so the order doesn't matter, nor does repetition. Just so long as the sum is the same.
The teleNumbs is the first four digits of the telephone number, which is user input, then the fifth digit is to be odd is teleNumbs[3] is even and vic versa. The geneNumbs will be the rest of the phone number, and the the last 6 digits must combine to be 30/33, whatever.
I want the output to be teleNumbs[0] - [3] + fifth digit + geneNumbs[0] - [4].
Thanks a lot for the help, it has stumped me for a while now.
Thanks, but I believe I caught that too, I was messing around with it because I knew sum was being intialized, but not being used. So I knew something wasn't right.