# First foray into C. Attempting to get the math right in divisor/remai

Page 1 of 1

## 9 Replies - 732 Views - Last Post: 10 October 2012 - 09:43 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294447&amp;s=2a7ef90409c3139b1c94155ef7b88144&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 CoryMore

• D.I.C Head

Reputation: 6
• Posts: 111
• Joined: 26-June 12

# First foray into C. Attempting to get the math right in divisor/remai

Posted 05 October 2012 - 03:14 PM

This is my first use of C. I'm attempting to get my math working correctly, and frankly think I VERY much over thought it. That being said, the program just keeps running and doesn't return anything.

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.

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

Is This A Good Question/Topic? 0

## Replies To: First foray into C. Attempting to get the math right in divisor/remai

### #2 #define

• Duke of Err

Reputation: 1636
• Posts: 5,765
• Joined: 19-February 09

## Re: First foray into C. Attempting to get the math right in divisor/remai

Posted 05 October 2012 - 05:03 PM

You probably want a for loop :

```74	   while (i = count, i >= 0, i--){

```

... notice the semicolons with the for loop.

```74	   for (i = count; i >= 0; i--){

```

Was This Post Helpful? 0

### #3 CoryMore

• D.I.C Head

Reputation: 6
• Posts: 111
• Joined: 26-June 12

## Re: First foray into C. Attempting to get the math right in divisor/remai

Posted 06 October 2012 - 08:48 AM

Hmm, I see. That did however give me a floating point exception for some reason.
Was This Post Helpful? 0

### #4 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: First foray into C. Attempting to get the math right in divisor/remai

Posted 06 October 2012 - 09:08 AM

Post your current code along with the complete error message, exactly as it appears in your development environment.

Also please state the compiler and compiler version you are using.

Jim
Was This Post Helpful? 0

### #5 CoryMore

• D.I.C Head

Reputation: 6
• Posts: 111
• Joined: 26-June 12

## Re: First foray into C. Attempting to get the math right in divisor/remai

Posted 07 October 2012 - 10:44 AM

Goal:

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().

Current Code:
```#include <stdio.h>      // for I/O facilities (FILE, printf, fscanf, fprintf, etc.
#include <stdint.h>     // for exact-width integer types
#include <inttypes.h>   // for formatting codes to print them
#include <stdbool.h>    // for C99 bool type

//  All identifiers must be declared before being used; that includes function
//  names.  As a general rule, the declaration of a function should be the same
//  as the first line in the function implementation, but it is legal to omit
//  the names of parameters here.
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;
for (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;
}

```

I'm using Cygwin, and writing in notepad++

\$ gcc -o ./starter -std=c99 ./starter.04b.c

\$ ./starter
Floating point exception (core dumped)
Was This Post Helpful? 0

### #6 CoryMore

• D.I.C Head

Reputation: 6
• Posts: 111
• Joined: 26-June 12

## Re: First foray into C. Attempting to get the math right in divisor/remai

Posted 07 October 2012 - 11:05 AM

Debugging gave me...

Quote

Program received signal SIGFPE, Arithmetic exception.
0x00401240 in dividesItself ()

Was This Post Helpful? 0

### #7 CoryMore

• D.I.C Head

Reputation: 6
• Posts: 111
• Joined: 26-June 12

## Re: First foray into C. Attempting to get the math right in divisor/remai

Posted 07 October 2012 - 03:03 PM

I was not accounting for one of the numbers being zero. I have now gotten the code to run, but the 0 scenario is in an infinite loop.

```#include <stdio.h>      // for I/O facilities (FILE, printf, fscanf, fprintf, etc.
#include <stdint.h>     // for exact-width integer types
#include <inttypes.h>   // for formatting codes to print them
#include <stdbool.h>    // for C99 bool type

//  All identifiers must be declared before being used; that includes function
//  names.  As a general rule, the declaration of a function should be the same
//  as the first line in the function implementation, but it is legal to omit
//  the names of parameters here.
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 = true;
uint i;
uint count = 0;
uint A = N;
do{
++count;
A = 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 (divide = true){
while (i = count, i >= 0, i--){
if (number[i] != 0){
C = N % number[i];
if (C = 0){
divide = true;}

else{
divide = false;}
}

else {
printf("Cannot divide by zero\n");
divide = false;
}
}
}
//  Return true iff every digit of N does divide N.
return divide;
}

```

Was This Post Helpful? 0

### #8 CoryMore

• D.I.C Head

Reputation: 6
• Posts: 111
• Joined: 26-June 12

## Re: First foray into C. Attempting to get the math right in divisor/remai

Posted 10 October 2012 - 07:40 PM

As I thought, drastically overthought. Went back to the drawing board:

```uint original = N;
bool divide = true;
while ( N > 0 && divide) {
uint digit = N % 10;
if(digit == 0)
{
divide = false;
}
else if(original % digit != 0)
{
divide = false;
}
else
{
divide = true;
}
N = N / 10;
return divide;

```

Was This Post Helpful? 0

### #9 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: First foray into C. Attempting to get the math right in divisor/remai

Posted 10 October 2012 - 07:44 PM

Also you probably shouldn't be mixing the "fixed" size type (uint32_t) with normal int, long, etc. If you are using the fixed types stick with them for the entire program.

Jim
Was This Post Helpful? 1

### #10 CoryMore

• D.I.C Head

Reputation: 6
• Posts: 111
• Joined: 26-June 12

## Re: First foray into C. Attempting to get the math right in divisor/remai

Posted 10 October 2012 - 09:43 PM

Thank you, I'm running into issues like that trying to learn C and C++ at the same time.
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }