C: Squaring string represented as positive integer

Click For Summary
SUMMARY

The discussion focuses on implementing a program to square a positive integer represented as a string in C. It is established that there is no built-in function like pow() for strings; instead, squaring must be done through digit-by-digit multiplication. The provided code demonstrates how to read two string numbers, add them, and print the result. Key issues identified include the excessive size of the array used for storing digits and the unnecessary struct wrapper around the array.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with string manipulation in C
  • Knowledge of basic arithmetic operations and algorithms
  • Experience with memory management and data structures in C
NEXT STEPS
  • Implement a digit-by-digit multiplication algorithm for squaring strings
  • Optimize memory usage by reducing the size of the array in the NUMBER struct
  • Explore alternatives to struct for handling arrays in C
  • Learn about error handling in C, particularly with input functions like scanf_s
USEFUL FOR

Students learning C programming, software developers working with string manipulations, and anyone interested in implementing arithmetic operations on 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
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K