C: Squaring string represented as positive integer

In summary, the conversation discusses the process of squaring a positive integer represented as a string using a programming language. The speaker asks if there is a way to square a string number without using multiplication, and if a function similar to pow() exists for strings. The expert responds that there is no other way and explains the process of multiplying two string numbers digit by digit. They also suggest starting with a simple example and provide feedback on the code provided by the speaker.
  • #1
gruba
206
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
  • #2
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;
}
 

Related to C: Squaring string represented as positive integer

1. What does it mean to square a string represented as a positive integer?

Squaring a string represented as a positive integer means to multiply the number by itself, resulting in the value being raised to the power of 2.

2. How do you represent a string as a positive integer in C?

In C, a string can be represented as a positive integer by using the atoi() function, which converts a string to an integer.

3. Can a string be squared if it contains non-numeric characters?

No, a string cannot be squared if it contains non-numeric characters. The atoi() function will only convert a string to an integer if the string consists of only numerical characters.

4. What is the purpose of squaring a string represented as a positive integer?

Squaring a string represented as a positive integer can be useful in mathematical operations and algorithms, such as calculating area or finding the square root of a number.

5. Are there any limitations to squaring a string represented as a positive integer in C?

Yes, there are limitations to squaring a string represented as a positive integer in C. The string must be within the range of values that can be represented as an integer in C, which is typically -2,147,483,648 to 2,147,483,647.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
974
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top