### program for division digit on digit by school algorithm hello. i trying to write a program that dividing one digit to another by school algorithm. what i mean showed on youtube https://www.youtube.com/watch?v=tjO7YaSXg54. i can not achieve correct result in calculation. what changes in code need make?
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181`` `````` Pu#include #include #include using namespace std; double pow1(double a, double b); double extractdigit(double* a, int position, int shift); void greaterpush(double* a, double * b, int del); void digittoarray(double a, double * b); int positsija = 0; int smeshenije = 0; int z = 0; int main(){ cout << "enter a digit " << endl; double digit; cin >> digit; double zifra; zifra = digit; int var; cout << "vvedi delitel " << endl; cin >> var; int i = 0; while (digit >= pow1(10, i)) i = i + 1; double * ptr=new double[i]; double * ptr2=new double[i]; double * ptr3=new double[i]; i = i - 1; double k; k = i; double zeloje; double drobnoje; int mn = 0; int mnb = i; int l = 0; while (l <= k){ drobnoje= modf((digit / pow1(10, i)), &zeloje); ptr[l] = zeloje; digit = digit - ptr[l] * pow1(10, i); l = l + 1; i = i - 1; } double j; l = 0; while (l<=k){ ptr2[l] = 0; l = l + 1; } l = 0; j = 7; digittoarray(zifra, ptr2); while (l <= k){ cout << ptr2[l] << endl; l = l + 1; } l = 0; int v = 0; int x, y; x = 0; y = 0; while (l <= mnb){ ptr2[l] = 0; l = l + 1; } int u; int w = 0; int count = 0; l = 0; int o=0; int s1, s2; s1 = 0; l = 0; while (count < mnb){ greaterpush(ptr, ptr2, var); while (l <= mnb){ l = l + 1; } l = l + 1; while (l <= z - 1){ o = extractdigit(ptr2, 0, l); l = l + 1; } ptr3[count] = o / var; w = o - ptr3[count] * var; digittoarray(w, ptr2); z = 0; l = 0; count = count + 1; } cout << endl; while (l <= mnb){ cout << ptr3[l] << endl; l = l + 1; } while (1){ ; } } double pow1(double a, double b){ double c = 1; double d; double i = 1; if (b == 0) d = 1; else if (b != 0){ while (i <= b){ c = c*a; i = i + 1; } d = c; } return d; } double extractdigit(double * a, int position, int shift){ int pos = position; int smes = shift; double b, c; double * pointer = a; c = 0; int k = position + shift; int m = k - pos; int l = k - 1; while (pos <= k){ b = pointer[pos] * pow1(10, m); c = c + b; m = m - 1; pos = pos + 1; } m = 0; b = 0; return c; } void greaterpush(double * a, double * b, int del){ int delitel = del; double c; int k = 0; double * pointer1 = a; double * pointer2 = b; while (1){ c = extractdigit(pointer1, positsija, smeshenije); if (c >= delitel){ digittoarray(c, pointer2); positsija = positsija + smeshenije + 1; z = smeshenije; smeshenije = 0; break; } else if (c < delitel){ smeshenije = smeshenije + 1; } } } void digittoarray(double a, double * b){ double chislo; chislo = a; int i = 0; double * pointer = b; double c, d; int k = 0; double zel, drob; while (chislo >= pow1(10, i)) i = i + 1; i = i - 1; z = 0; while (i >= 0){ while (pointer[k] != 0) k = k + 1; drob = modf((chislo / pow1(10, i)), &zel) ; c = zel; pointer[k] = c; chislo = chislo - c*pow1(10, i); i = i - 1; k = k + 1; z = z + 1; } }t the code you need help with here.`````` Hello tohamoa,

I ran across your program a couple of days ago and the first question I have is if there is any way that you could make the program any more difficult to read?

Some blank lines and a little more care with the indenting would help.

Then there is the use of single letter variable names. I think that you missed a few letters.

You wrote the program and to you the variables have some meaning, but to others they have no meaning.

Given the reworking of this part of the code:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677`` ``````#include #include #include //using namespace std; // <--- Best not to use. // A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/ double pow1(double a, double b); double extractdigit(double* a, int position, int shift); void greaterpush(double* a, double * b, int del); void digittoarray(double a, double * b); int positsija = 0; // <--- position / посиция or позиция. Initializaton here may not be needed. int smeshenije = 0; // <--- mixing? / смешение? int z = 0; // <--- Unknown or the z axis. int main() { double digit{}; // numerator / числитель. double var{}; // <--- делитель / divisor or demoninator / знаменатель. double zifra{}; // numerator / числитель. //double частное{}; // <--- quotient / частное. A possiable variable name. Unless I have missed this somewhere. std::cout << "\n enter a digit: "; std::cin >> digit; while (!std::cin /*|| var == 0.0*/) { //if (!std::std::cin) // <--- Use the if/else if statement if you use the || part of the while condition. std::cout << "\n Invalid Input! \n"; //else if (var == 0.0) // std::std::cout << "\n Divisor can not be (0) zero! \n"; // <--- Change message. std::cout << "\n enter a digit: "; std::cin >> digit; } std::cout << " vvedi delitel / enter the divider: "; std::cin >> var; while (!std::cin || var == 0.0) { if (!std::cin) std::cout << "\n Invalid Input! \n"; else if (var == 0.0) std::cout << "\n Divisor can not be (0) zero! \n"; std::cout << " vvedi delitel / enter the divider: "; std::cin >> var; } zifra = digit; int i = 0; while (digit >= pow1(10, i)) i = i + 1; double * ptr = new double[i]; // <--- std::vector may be of better use. double * ptr2 = new double[i]; double * ptr3 = new double[i]; //i = i - 1; i++; // <--- Does the same as the line above. double k = i; // <--- Changed. Can be defined and given a value. This makes line 74 unnecessary. double zeloje; // whole. double drobnoje; // factorial. int mn = 0; int mnb = i; int l = 0; //k = i; while (l <= k) {``````

With just a few blank lines is this not much easier to read. I hope that the comments on some of the variable names came out correctly or at least close.

Looking over this much of the program:

Lines 14 - 16. Try to avoid using global variables like these. Any line of code in any function following these definitions has access to these variable and if something puts the wrong value in one of these variables it becomes harder to fine what went wrong. It is best to define these variables in "main" and pass them to the functions that need them. As global variables there is a good chance that your compiler may initialize these variables for you, but initializing them your-self is fine.

Inside "main" these variables are different. They are not automatically initialized, so when you write `double digit;` this only reserves space for this variable. Whatever garbage that may be at that bit of memory "digit" is trying to interpret as a "double" and you end up with an unknown number. On my computer this tends to be "-9.2559631349317831e+64". Not something that you want to work with.

The next potential problem is with `std::cin >> digit;`. This is formatted input and "cin" is expecting a number to be entered. Should you enter anything other than a number "cin" will fail and be unusable the rest of the program. There is a chance that "digit" will not be changed and still contain the garbage from when it was defined. Then when you get to `std::cin >> var;` "cin" is in a failed state and nothing happens. I think that the C++14 standards made a change that if "cin" fails that numeric variables will be set to (0) zero, but I am not completely sure about this.

The while loops following the "cin" statements are a way of catching if "cin" has failed and correcting the problem. The second while loop not only checks the status of "cin", but also shows how you can check for (0.0) or even a range for the number. Adjust as your need dictates.

On line 53 I am guessing that "zifra" is a variable the should never change its value in case you need the original value.

I still have not figured out the next while loop and what is is doing.

Lines 60 - 62 use "new" to define an array, but I can not fine anywhere in the program where you "delete", or free, this memory. I believe this is a memory leak that needs fixed. Instead of creating new memory a "std::vector" might be a better choice and you do not have to worry about deleting any memory as the vector class handles this for you.

As the code shows I do well until the while loop at the end of the code. "l <= k" is where I get confused and loose anything I have figured out when I concentrate on "l" and "k" to try and figure out what they are or are doing.

This is small, but I noticed the while loop at the end of "main". With what little you have this is an endless loop with no way out, so the program gets stuck here until you do something to end the program. Whatever you may do it is not a proper way to end the program.

Some side notes:

White space, blank lines and comments are ignored by the compiler and are not part of the compiled code. So use as much as you need to make the code more easy to read.

When it comes to functions whether you define a variable in the parameters of the function or between the {}s of the function you can use the same names of variables that you defined in "main" because these variables are local to the function. This also makes the function easier to understand because you do not have to deal with a new variable name.

This is a challenge, so it may take longer to understand the program and figure out what it is doing.

I also wanted to mention that "pow1" as a function name is a bit misleading based on what the function does and what "std::pow" is from the "cmath" header file.

Hope that helps,

Andy