I should write a code to add up two 80 digits numbers. At first, program take numbers in form of strings.

Code:
```#include <stdio.h>
#include <conio.h>
void input (void);
void change (void);
void output (void);
char a , b ;
int a_1 , b_1 , s ;
int main ()
{
input ();
change ();
output ();
getch ();
return 0;
}
void input (void)
{
printf ("Enter First String Of Numbers:\n");
gets (a);
printf ("Enter Second String Of Numbers:\n");
gets (b);
}
//*********************
void change (void)
{
int j = 0;
for (int i = 0; a [i]; i++)
if (a [i] >= '0' && a [i] <= '9')
a_1 [j++] = a [i] - 48;

int k = 0;
for (int i = 0; b [i]; i++)
if (b [i] >= '0' && b [i] <= '9')
b_1 [k++] = b [i] - 48;
}
//***********************
{
for (int i = 79; i >= 0; i--)
{
s [i] = a_1 [i] + b_1 [i];
if (a_1 [i] + b_1 [i] >= 10)
{
c = a_1 [i] + b_1 [i] - 10;
s [i] = c;
s [i - 1] += 1;
}
//************************
void output (void)
{
for (int i = 0; s [i]; i++)
printf ("%d", s [i]
}``` 2. what's the problem?
That's for you to tell us! I mean, other than the use of non-standard headers like conio.h (let me guess...Turbo C?), using gets(), global variables, arrays meant to hold numbers 80-digits long without enough space, accessing arrays out of bounds, and mismatched brackets? 3. Fix the two missing curly braces in your add function and then either post the exact compiler errors or tell us what it does wrong. 4. I know what you are saying, but there is a problem. I'm an iranian physics student and because of sanctions I (We) do not have any compiler by myself. We should write our programs in Notepad and then we can compile it at university. So I wrote this code in notepad and then I read it and I guess there are some problems in algorithm.
If you can help me, You are the man!, if not, again thanks for your attentions.  6. Thanks. I compiled it, NO error is taken, but Program still doesn't work.

Code:
```#include <stdio.h>
#include <conio.h>
void input (void);
void change (void);
void output (void);
char a , b ;
int a_1 , b_1 , s ;
int main ()
{
input ();
change ();
output ();
getch ();
return 0;
}
void input (void)
{
printf ("Enter First String Of Numbers:\n");
gets (a);
printf ("Enter Second String Of Numbers:\n");
gets (b);
}
//*********************
void change (void)
{
int j = 0;
for (int i = 0; a [i]; i++)
if (a [i] >= '0' && a [i] <= '9')
a_1 [j++] = a [i] - 48;

int k = 0;
for (int i = 0; b [i]; i++)
if (b [i] >= '0' && b [i] <= '9')
b_1 [k++] = b [i] - 48;

int m, i;
for (m = j; m >=0; m--)
{
s [i] = a_1 [i] + b_1 [i];
if (s [i] > 9){
s[i] -= 10;
s [i -1] += 1;}
}
}
//************
void output (void)
{
for (int i = 0; s [i]; i++)
printf ("%d", s [i]);
}``` 7. what I see with your code (when compiling as C++ - in C mode there is more)
Code:
```Line 18: error: conio.h: No such file or directory
In function 'int main()':
Line 14: error: 'getch' was not declared in this scope
compilation terminated due to -Wfatal-errors.``` 8. The way to debug your program is to give it some simple problems, and insert a print line of code showing the intermediate results to you, followed by a getchar() to pause the program long enough that you can study it and determine where it's goofing up. Put the print and getchar statements, right inside the for loop that is doing the adding.

Be sure to include all the "edge" cases - adding 9+0, 9+1, 9+9, 0+0, etc. 9. Originally Posted by vart what I see with your code (when compiling as C++ - in C mode there is more)
Code:
```Line 18: error: conio.h: No such file or directory
In function 'int main()':
Line 14: error: 'getch' was not declared in this scope
compilation terminated due to -Wfatal-errors.```
I think this errors are not related to my problem! I mean, I have algorithm problem. 10. Originally Posted by alireza beygi I think this errors are not related to my problem! I mean, I have algorithm problem.
You are correct, that is not the problem. 11. Any one can help me? 12. OK, look at what your program is doing, right from the start. Is the change function putting the digits into the a_1 and b_1 arrays, correctly? It looks good to me, but you should do a print out after a_1 and b_1 have received the digits, to confirm it. I'm not going to do that for you.

What I would do is first, fill the a_1 and b_1 arrays, with zero's, because right now, they will have undetermined values in them. You could use a for loop on them or memset, or other techniques, whichever you want. Something like this.

Code:
```int i=0
while(i < 100) {
a_1[i]=b_1[i]=0;
++i;
}```
Then before that last add part in the change function, you need to compare j and k, and use whichever one is larger to control (stop), the adding for loop. You don't want to use just j or just k to control the loop, you want to use the larger of the two variables.

Give that a try and see if you can make that go. Popular pages Recent additions 