C: Squaring string represented as positive integer

AI Thread Summary
To square a positive integer represented as a string, multiplication must be performed digit by digit, as there is no direct equivalent to a pow() function for strings. The process involves multiplying the string number by each digit of the other number and managing the carry for each partial product. The provided code snippet successfully implements addition of two string numbers but has issues with array size, leading to stack overflow errors. Additionally, the struct used in the code is unnecessary, as the array can be passed directly to functions. Overall, the discussion emphasizes the need for careful implementation when handling large numbers represented as strings.
gruba
Messages
203
Reaction score
1

Homework Statement


Write a program that will square the input positive integer represented as a string.

Homework Equations


3. The Attempt at a Solution [/B]
Is there another way of squaring a string number instead of multiplying it by itself?
Something like a pow() function for strings?
If not, is it possible to the following program that adds two string numbers in order to find the product:
Code:
#include<stdio.h>
#define MAX 100000

typedef struct
{
  int arr[MAX];
}NUMBER;

void read(NUMBER *add_num)
{
  int i,digit=0;
  char ch[101];
  scanf("%s",ch);
  while(ch[digit])
  digit++;
  for(i=0;i < MAX;i++)
  {
  digit--;
  if(digit >= 0)
  add_num->arr[i]=ch[digit]-'0';
  else
  add_num->arr[i]=0;
  }
}

void addition(NUMBER a,NUMBER b,NUMBER *add_res)
{
  int carry=0;
  int i,temp;
  for(i=0;i < MAX;i++)
  {
  temp=a.arr[i]+b.arr[i]+carry;
  add_res->arr[i]=temp % 10;
  carry=temp / 10;
  }
}

void print(NUMBER *add_num)
{
  int i;
  for(i=MAX-1;add_num->arr[i]==0;i--);
  for(;i>=0;i--)
  printf("%d",add_num->arr[i]);
}

int main()
{
  NUMBER x,y,z;
  printf("enter two positive integers: \n");
  read(&x);
  read(&y);
  printf("addition result: ");
  addition(x,y,&z);
  print(&z);
  return 0;
}
 
Physics news on Phys.org
gruba said:

Homework Statement


Write a program that will square the input positive integer represented as a string.

Homework Equations


3. The Attempt at a Solution [/B]
Is there another way of squaring a string number instead of multiplying it by itself?
Something like a pow() function for strings?
No, there isn't.
To square a number represented as a string, you're going to have to do multiplication, digit by digit. For example, to multiply 12345 by 345, you need to do this:
1. multiply 12345 by 5.
2. multiply 12345 by 4, and multiply that result by 10.
3. multiply 12345 by 3, and then multiply that result by 100.
4. add together all of the partial products.

Start with a simple example first to get things working.

BTW, your example below compiled OK for me, after I changed scanf to scanf_s (I'm running VS 2013, and scanf is no longer supported by MSFT). When I tried to run your code, I got a stack overflow error on my machine, as your array is too large. 100,000 ints is 400,000 bytes. There's no reason I can think of to have an array of ints this size, as each digit of your answer is only one character, which could be stored in 100,000 bytes.

One other thing -- there's no reason to have your struct, as it's just a wrapper around your array. Instead of passing the address of the struct to your functions, you could just as well pass the address of the array.
gruba said:
If not, is it possible to the following program that adds two string numbers in order to find the product:
Code:
#include<stdio.h>
#define MAX 100000

typedef struct
{
  int arr[MAX];
}NUMBER;

void read(NUMBER *add_num)
{
  int i,digit=0;
  char ch[101];
  scanf("%s",ch);
  while(ch[digit])
  digit++;
  for(i=0;i < MAX;i++)
  {
  digit--;
  if(digit >= 0)
  add_num->arr[i]=ch[digit]-'0';
  else
  add_num->arr[i]=0;
  }
}

void addition(NUMBER a,NUMBER b,NUMBER *add_res)
{
  int carry=0;
  int i,temp;
  for(i=0;i < MAX;i++)
  {
  temp=a.arr[i]+b.arr[i]+carry;
  add_res->arr[i]=temp % 10;
  carry=temp / 10;
  }
}

void print(NUMBER *add_num)
{
  int i;
  for(i=MAX-1;add_num->arr[i]==0;i--);
  for(;i>=0;i--)
  printf("%d",add_num->arr[i]);
}

int main()
{
  NUMBER x,y,z;
  printf("enter two positive integers: \n");
  read(&x);
  read(&y);
  printf("addition result: ");
  addition(x,y,&z);
  print(&z);
  return 0;
}
 

Similar threads

Replies
4
Views
1K
Replies
3
Views
1K
Replies
3
Views
2K
Replies
1
Views
3K
Replies
3
Views
1K
Replies
12
Views
2K
Back
Top