-3

I'm trying to create a program that be able to get the two numbers with a maximum of 1000 digits from the user and then print the result (with C (programming language))

notes to create this program :

1-to get the two numbers, use strings with a length of 1000 characters.(consider a character for the zero in the end of the sring)

2-to store the numbers , use two array with a length of 1000 block (home).each digit of a large numbers must be store in a separate array block.(for example: 365 ( "3" store in a block(home) and 6 store in another block and ...)

3-To get the numbers as strings and convert it to a numeric array, addition, subtraction and printing large numbers, use separate function. the "main" function must be very small.


I wrote this code but can't figure out why it wont work:

/*codes*/


#include <conio.h>
#include <stdio.h>
#define hang 1

void sinascan1(char sina[2],int i){
     puts("Enter number 1");
     gets(sina);
    while (i != hang){
          sina[i] -= 48;
          printf ("%d\n", sina[i]);
          i++;
          }
     }
void sinascan2(char sina1[2],int i){
     puts("Enter number 2");
     gets(sina1);
    while (i != hang){
          sina1[i] -= 48;
          printf ("%d\n", sina1[i]);
          i++;
          }
     }
void sinajam(char sina[2],char sina1[2],char jam[3],int i){
     puts("____");
     int f,g;
     while (i != hang){ 
           jam[i]=sina[i]+sina1[i];
           printf ("%d\n", jam[i]);
           i++;
           }
     }
int main()
{   int i=0;
    char sina[2],sina1[2];
    char jam[2];
    sinascan1(sina,i);
    sinascan2(sina1,i);
    sinajam(sina,sina1,jam,i);
    getch();
    return 0;
}

how can I do that?

Filburt
  • 17,626
  • 12
  • 64
  • 115

2 Answers2

0

I think consider a character for the zero in the end of the sring is the point:

char str[1000]; //number of digits
int i=0;

while(true){
    scanf("%c", (str+i));
    if( *(str+i) == '0') break;
}

I'm not very sure, but I think it at least give a point

0

First, it is hard to store 1000 characters in an array of size 2 ! C language is very confident in the programmer and never controls capacity of arrays ... simply it leads to undefined behavious since you try to write in memory that can not exist or be used for anything else.

Next write in your manual in flashing red over gets function : never use that !. It is inherently unsafe because you write a string in an array (of a defined size) without any way of knowing the size of input ! Always use fgets instead that can limit the input string to the size of buffer.

Finaly, but it is far less serious than the 2 others, avoid using conio.h unless you have special requirements because it is not portable across different architectures.

All that are reasons for which your program could break lead to unexpected results or be not portable, but that's not all : when I was much younger than today, I learned that to add 2 numbers, you add their digits but when the sum of 2 digits is greater than 9 (say 6 + 7 = 13), you only keep the low order digit (here 3) and and the carry (1) to next column. And you add numbers starting from low digits ...

What you currently have can only add numbers of 1 digit !

So here is a possible implementation :

#include <stdio.h>
#include <memory.h>
#include <string.h>
#define SIZE 1000

void sinascan(char* sina, int size, int i){ 
    char *c = sina;
    int l;
    int temp;

    ::memset(sina, 0, size); /* initialize buffer to 0 */
    printf("Enter number %d : ", i);
    fgets(sina, size , stdin);
    while ((*c >= '0') && (*c <= '9')) { /* only get digits */
          c++;
          }
    *c = 0; /* remove optional spaces and end of line */
    /* now reverse digits so that sina[0] is low order digit */
    l = strlen(sina);
    for (i=0; i<l/2 + 1; i++) {
        temp = sina[i];
        sina[i] = sina[l - 1 - i];
        sina[l - 1 - i] = temp;
    }
    for (i=0; i<l; i++) {
        sina[i] -= '0';
    }
     }


int sinajam(char *sina,char *sina1,char *jam, int size){
     puts("____");
     int i,sum, carry = 0;
     for (i=0; i<size; i++ ) {
           sum = sina[i] +sina1[i] + carry;
           jam[i] = sum % 10;
           carry = sum / 10;
           printf ("%d", jam[i]);
           }
     return carry;
     }
int main()
{   int i=0;
    char sina[SIZE+1],sina1[SIZE+1]; /* add one place for terminating null */
    char jam[SIZE];
    sinascan(sina,SIZE+1, 1);
    sinascan(sina1, SIZE+1, 2);
    i = sinajam(sina,sina1,jam,SIZE);
    if (i != 0) {
        puts("overflow");
    }
    return 0;
}

This implementation accepts input up to 1000 digits, generates a sum up to 1000 digits and signals overflows.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252