So..my problem is that I don't really understand the last part of the code,more specifically the second while ( while(n) ).I don't understand the purpose of i and p.I would appreciate the if somebody will take the value 12345 and show me the step by step code until n becomes 0.
I tried to write the code step by step on paper,but the final number for nr was 254,which is wrong.I need as much explanation as possible,so I can deduce what I am doing/thinking wrong.
I had qualms about replying to this, as I don't believe it is "the correct code for the exercise". In essence you are removing characters: it has nothing really to do with the number; (it wouldn't work in a different number base, for example). I feel that a simpler approach would simply have been to stringstream it into a string and erase the relevant characters - it doesn't rely on them being numerical digits.
However, as you wanted an explanation of the code, this is my attempt. You can remove the "CHECK LINES" when you are satisfied that you know what they are doing. Try it through CPP-shell.
#include <iostream>
usingnamespace std;
int main()
{
int n, k, x, p, i, mijl, nr;
cout << "n="; cin >> n;
// First count the digits in the number
x = n;
k = 0;
while( x ) // while x is not zero (i.e. still has some digits)
{
k++;
x /= 10; // integer division effectively knocks off the last digit
}
cout << "The number of digits in the number is " << k << endl; // CHECK LINE; REMOVE LATER
// Set mijl to the first of the middle digits if k is even, or the middle digit if k is odd (can count from either end)
if ( k%2 == 0 ) mijl = k / 2;
else mijl = k / 2 + 1;
cout << "The value of mijl is " << mijl << endl; // CHECK LINE; REMOVE LATER
// Reconstruct the number without those pesky middle digits
p = 1; // p will hold the power of 10 to reconstruct the number
nr = 0; // nr will hold the reconstructed number
i = 1; // i is the digit position (counting from the right-hand end)
while( n ) // about to demolish the original number!
{
if ( i == mijl ) // here is where you remove digit(s)
{ // ADD A BRACE, PLEASE
if ( k%2 == 0 ) // if even, remove two digits
{
n = n / 100; // integer division obliterates two digits
i = i + 2;
}
else // if odd, remove just one digit
{
n = n / 10; // integer division obliterates one digit
i = i + 1;
}
} // ADD THE CORRESPONDING CLOSING BRACE
nr = nr + p * ( n % 10 ); // n%10 is the current last digit
// p is the power of 10, so p * ( n % 10 ) is the actual numerical value associated with this digit
// nr is the so-far-reconstructed number with this new part added
cout << "Reconstructed number after rightmost digit " << i << " is " << nr << endl; // CHECK LINE; REMOVE LATER
p = p * 10; // you are working from the right, so increase the power of 10
i++; // increment the digit position (remember you are counting from the right)
n = n / 10; // integer division removes the last digit; it has been dealt with and added to nr
}
cout << "\nFinal number is: " << nr;
}
n=12345678
The number of digits in the number is 8
The value of mijl is 4
Reconstructed number after rightmost digit 1 is 8
Reconstructed number after rightmost digit 2 is 78
Reconstructed number after rightmost digit 3 is 678
Reconstructed number after rightmost digit 6 is 3678
Reconstructed number after rightmost digit 7 is 23678
Reconstructed number after rightmost digit 8 is 123678
Final number is: 123678
n=1234567
The number of digits in the number is 7
The value of mijl is 4
Reconstructed number after rightmost digit 1 is 7
Reconstructed number after rightmost digit 2 is 67
Reconstructed number after rightmost digit 3 is 567
Reconstructed number after rightmost digit 5 is 3567
Reconstructed number after rightmost digit 6 is 23567
Reconstructed number after rightmost digit 7 is 123567
Final number is: 123567
For the record, here's a version using stringstream.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <sstream>
usingnamespace std;
int main()
{
int n;
cout << "Input n: "; cin >> n;
stringstream ss;
ss << n;
string s = ss.str();
int length = s.size();
if ( length % 2 == 0 ) s.erase( length / 2 - 1, 2 );
else s.erase( length / 2 , 1 );
cout << "Final number is " << s;
}