0

Hi this is my code and it does not work I want to do division of first number and second number (There are large number ==> 50 digits) it returns 0 when there are those inputs at top

#include <stdio.h>
#include<string.h>

//inputs a = "85548187429241610817174857069711212619572772836876"
//inputs b = "43488970802022113439274600911335838550652591258202"

int main() {
    char a[50],b[50];
int r,q=0;
printf("1:");
scanf("%s",&a);
printf("2:");
scanf("%s",&b);

r=atoi(a);

if(atoi(a)<atoi(b)){return 0 ;}

while(r>atoi(b)){
q = q+1;
r = r-atoi(b);

}
printf("%d %d",q,r);
    return 0 ;
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Mohamad Sh
  • 41
  • 5
  • 2
    `atoi` is good for maybe 10 digits. Absolutely does not work for 50 digit numbers. – user3386109 Feb 25 '23 at 20:45
  • @MohamadSh And what should the code return? – Vlad from Moscow Feb 25 '23 at 20:45
  • 1
    Also, division by repeated subtraction is really slow if the first number is much larger than the second number. – user3386109 Feb 25 '23 at 20:47
  • 1
    Does this answer your question? [Arbitrary-precision arithmetic Explanation](https://stackoverflow.com/questions/1218149/arbitrary-precision-arithmetic-explanation) – dawg Feb 25 '23 at 20:47
  • 2
    And then there's the buffer overflow problem. If you use `scanf` to read a string of 50 digits, the array must have at least 51 characters, because `scanf` puts a null byte at the end of the string. – user3386109 Feb 25 '23 at 20:50
  • 1
    You obviously can't store the number in integer variables, since their ranges are bounded. One approach is to store them in integer arrays, with one digit per entry. Then just do long division, the same way you'd do it by hand. – Tom Karzes Feb 25 '23 at 20:51
  • Kind of irrelevant considering all the other problems, but you're calling `atoi(b)` _twice_ for _each iteration_ of your loop. It should be called once, before the loop, and stored in a variable. – pmacfarlane Feb 25 '23 at 20:52
  • Repeated subtraction is a poor algorithm even for `int`: for a 50 digit value you'll need to wait at *Milliways*, the Restaurant at the End of the Universe. – Weather Vane Feb 25 '23 at 20:55
  • 1
    `while(r>atoi(b))` You want `>=` here.... Then the code might actually work for more appropriate (smaller) values. – Fe2O3 Feb 25 '23 at 20:56
  • 1
    Also: `scanf("%s",&a);` ==> `scanf("%49s",a);` Not only the overrun problem, but the wrong use of "address of"... – Fe2O3 Feb 25 '23 at 21:05
  • Please don't make drastic edits like that. It invalidates the answer – General Grievance Mar 06 '23 at 13:42
  • @WeatherVane For *this particular problem*, repeated subtraction is fine, because the quotient is 1. – Steve Summit Mar 06 '23 at 15:55

1 Answers1

2

Troubles:

Not compiling with all warnings enable

scanf("%s",&a); --> scanf("%s", a);

Not saving in a big enough buffer

To save a string with length 50, takes size 51.

Not using a width limit

Do not use scanf("%s",... without a width

Check input return value.

atoi() is insufficient for such a large string

OP needs to write numeric string compare, subtract, add functions

int main() {
  char a[50+1],b[50+1];
  printf("1:");
  if (scanf("%50s", a) != 1) {
    fprintf(stderr, "Failure to read input a.\n");
    return EXIT_FAILURE;
  }
  printf("2:");
  if (scanf("%50s", b) != 1) {
    fprintf(stderr, "Failure to read input b.\n");
    return EXIT_FAILURE;
  }

  // Leave the rest for OP.  GTG
  // I estimate OP is about 10% done. 
 

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256