Understanding Arithmetic Errors in C Programs

Click For Summary
The discussion revolves around a C program designed to convert a decimal number to a specified base, which encounters an "Arithmetic Error (core dumped)" during execution. The program compiles successfully but fails when performing calculations, particularly during a division operation. The user inputs a decimal number and a base, and while the initial input handling works, the error arises in a loop where the program attempts to calculate the digits in the new base.The core issue identified is a division by zero occurring in the loop where the variable `z` becomes zero after several iterations. Specifically, the calculation `x[p] = D % z` fails when `z` equals zero, leading to the arithmetic exception. The loop's logic is flawed, as the variable `f` decrements to a negative value, causing `pow(n, f)` to return zero. This results in an invalid operation when the program attempts to divide by zero. The discussion highlights the importance of validating calculations to prevent such errors in programming.
Defennder
Homework Helper
Messages
2,590
Reaction score
5
C Program "Arithmetic Error"

I'm writing a C program which prompts a user for an input decimal number (or any integer in base 10) as well as the n-base to which he wishes to convert the number to. However, for some reason my program failed to work and displayed "Arithmetic Error (core dumped", though it compiled successfully (GCC). Here is the website the algorithm I used is based on:

http://www.learn-programming.za.net/articles_decbinhexoct.html

And here is the output I got for a sample input. User input underlined.

Enter decimal number followed by base number: 55 6

No. of digits in final answer is 3

Arithmetic Exception (core dumped)

What does the last line of error mean? The first 2 lines worked perfectly. This is the code:

#include <stdio.h>
#include <math.h>

int main(void)

{

/* Declare and initialise variables */

int y, e, z, m, p, f, g=0, k=2, D=0, n=0, d1, d[9], x[9];



/* Prompt for User Input */

printf("Please enter decimal number followed by base number. \n");



/* Scanf in the values */

scanf("%d %d", &D, &n);



/* Check if entered decimal no. is already in correct base */

y = D/n;





if (y==0)

{ printf("Decimal no. %d has value of %d in base %d \n", D,D,n);

}

/* Digit-by-digit calculation of decimal no. in new base */

else

{ while(y>0)

{ y = D/(pow(n,k));

k+=1;}



printf("No. of digits in final answer is %d \n", k-1);

d1 = D / (pow(n,(k-1)));

e = k-1;

f = k-1;



for (m=2, p=1; m=e, p=e-1; m++, p++)

{ z = pow(n,f);

x[p] = D%z;

f-=1;

d[m] = (x[p])/(pow(n,f));}



printf("Decimal number of %d has value of", D);



g = 1;

while(g<k-1)

{ printf("%d",d[g]);

g += 1; }



printf(" in base %d", n);

}

/* Exit */

return 0;

}
 
Last edited by a moderator:
Technology news on Phys.org
I have tested your code in Visual c++, and it reported that
divide by zero...

it's here:
for (m=2, p=1; m=e, p=e-1; m++, p++)
{ z = pow(n,f);
x[p] = D%z;******************************here!
f-=1;
d[m] = (x[p])/(pow(n,f));}
...

it seems that z have the value for each loop: 216->36->6->1->0.
as f becoms -1, the pow(n,-1) return a Zero;
 
Thanks so much, zyh!
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
14
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
47
Views
5K
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 8 ·
Replies
8
Views
3K