Quote

We will say that a non-negative integer divides itself if every digit of the integer divides the integer with no remainder. Thus, 24 divides itself since 2 and 4 both divide 24 without remainder, but 32 does not divide itself since 3 divides 32 with a remainder of 2.

You will implement the body of a C function that will determine whether a given integer does divide itself, and return true if so and false otherwise. The function must conform to the interface:

bool dividesItself(FILE *Out, uint32_t N);

The function will take a non-negative integer value N and analyze it by considering the digits, one-by-one from right to left. The function must write output showing the results of this analysis. For example, if N == 61422 the function would write:

Testing integer 61422:

digit yes/no

2 yes

2 yes

4 no

The function would, of course, return false in this case. Notice that the function stops testing as soon as it finds a digit that does not divide N. The output code given for part A should provide some clues as to formatting codes and using fprintf(). The lecture notes and assigned readings in King provide more.

You will implement the body of a C function that will determine whether a given integer does divide itself, and return true if so and false otherwise. The function must conform to the interface:

bool dividesItself(FILE *Out, uint32_t N);

The function will take a non-negative integer value N and analyze it by considering the digits, one-by-one from right to left. The function must write output showing the results of this analysis. For example, if N == 61422 the function would write:

Testing integer 61422:

digit yes/no

2 yes

2 yes

4 no

The function would, of course, return false in this case. Notice that the function stops testing as soon as it finds a digit that does not divide N. The output code given for part A should provide some clues as to formatting codes and using fprintf(). The lecture notes and assigned readings in King provide more.

This is what I have.

bool dividesItself(FILE *Out, uint32_t N); int main() { // Open the input and output files: FILE *In = fopen("data.txt", "r"); FILE *Out = fopen("log.txt", "w"); uint32_t N; // will hold the integer to be tested // The loop uses read-to-input-failure logic. As mentioned in part A, the // fscanf() function tells us how many values it successfully read and // assigned to our variables. This loop will halt whenever the expected // single value is not read; in particular, the loop will halt when reading // reaches the end of the file and nothing is read. while ( fscanf(In, "%"SCNu32"\n", &N) == 1 ) { // Call the test function to see if N has the specified properties. // We could assign the return value to a local variable, but we have // no further use for the information. if ( dividesItself(Out, N) ) { fprintf(Out, "%"PRIu32" divides itself\n", N); } else { fprintf(Out, "%"PRIu32" does not divide itself\n", N); } } // Close the input and output files: fclose(In); fclose(Out); // Exit with a success code: return 0; } // Determine whether a non-negative integer is divisible by each // of its digits. // // Pre: Log points to an open output file. // N has been initialized. // Post: Test results have been written to the output file. // Returns: true iff each digit of N divides N bool dividesItself(FILE *Log, uint32_t N) { // You may declare whatever local variables you find to be // useful. The declarations do not have to be here; you // can declare locals within the loop, for example. // Log the integer being tested and a header for the // detailed results; you must use the specified formatting. // Loop to analyze N. The loop test is up to you. // You may use a different kind of loop if you like, // but you must produce the specified output. bool divide; uint i = 0; uint count = 0; uint A = N; do{ ++count; A /= 10; }while (A != 0); uint number[count]; uint B = N; while (i = 0, i <= count, i++){ number[i] = B % 10; B = B / 10;} uint C; while (i = count, i >= 0, i--){ C = N % number[i]; if (C = 0){ divide = true;} else{ divide = false;} } // Return true iff every digit of N does divide N. return divide; }

This post has been edited by **CoryMore**: 05 October 2012 - 03:15 PM