Question

In: Computer Science

(a)Design an algorithm that takes two numeric strings s1 and s2 and return another numeric string...

(a)Design an algorithm that takes two numeric strings s1 and s2 and return another numeric string that represent their sum without using using SHIFT OR any operator +.

(b) Analyze time and space of part (a)

(c)Implement part (a) in your favorite programming language without using 0+0 operator. You

should read the two numeric strings from a FILE input.in and output their addition to standard output.

You Solution will be test on 10,000 test cases. Each test case will include two randomly selected numbers.

i want it in c program

positive number

Solutions

Expert Solution

ALGORITHM : -

1. We will make sure that str1 is of smaller length than str2 as it will help to code our program.

2. We will reverse both string as addition is done from left most significant bit.

3. We will perform add operation digit by digit considering carry.

4. Atlast we will be left with only bigger string digits so we will simply add them considering carry.

5. The resultant string obtained will be in reverse order so we will reverse it to get final answer.

Below program performs addition of two numbers in string format

#include <stdio.h>
#include <string.h>
void strrrev(char str[])
{
   int i = strlen(str) - 1, j = 0;

char ch;
while (i > j)
{
ch = str[i];
str[i] = str[j];
str[j] = ch;
i--;
j++;
}
}
void findSum(char str1[], char str2[])
{
if (strlen(str1) > strlen(str2))
{
   char tmp[100];
   strcpy(tmp, str1);
       strcpy(str1, str2);
       strcpy(str2, tmp);
}
char str[100];
int n1 = strlen(str1), n2 = strlen(str2);
strrrev(str1);
strrrev(str2);
  
int carry = 0,ctr=0;
for (int i=0; i<n1; i++)
{
int sum = ((str1[i]-'0')+(str2[i]-'0')+carry);
str[ctr++]=sum%10 + '0';
carry = sum/10;
}
  
for (int i=n1; i<n2; i++)
{
int sum = ((str2[i]-'0')+carry);
str[ctr++]=sum%10 + '0';
carry = sum/10;
}
  
if (carry)
str[ctr++]=carry+'0';
strrrev(str);
printf("Total Sum=");
for(int i=0;i<ctr;i++)
   printf("%c",str[i]);
  
}
int main()
{
char str1[100] = "12";
char str2[100] = "198111";
findSum(str1, str2);
return 0;
}

#include <stdio.h>
#include <string.h>
void strrrev(char str[])
{
   int i = strlen(str) - 1, j = 0;

char ch;
while (i > j)
{
ch = str[i];
str[i] = str[j];
str[j] = ch;
i--;
j++;
}
}
void doSum(char str1[], char str2[])
{
if (strlen(str1) > strlen(str2))
{
   char tmp[100];
   strcpy(tmp, str1);
       strcpy(str1, str2);
       strcpy(str2, tmp);
}
char str[100];
int n1 = strlen(str1), n2 = strlen(str2);
strrrev(str1);
strrrev(str2);
  
int carry = 0,ctr=0;
for (int i=0; i<n1; i++)
{
int sum = ((str1[i]-'0')+(str2[i]-'0')+carry);
str[ctr++]=sum%10 + '0';
carry = sum/10;
}
  
for (int i=n1; i<n2; i++)
{
int sum = ((str2[i]-'0')+carry);
str[ctr++]=sum%10 + '0';
carry = sum/10;
}
  
if (carry)
str[ctr++]=carry+'0';
strrrev(str);
printf("Total Sum=");
for(int i=0;i<ctr;i++)
   printf("%c",str[i]);
  
}
int main()
{
char str1[100] = "12";
char str2[100] = "198111";
doSum(str1, str2);
return 0;
}

OUTPUT:-

Total Sum=198123

Time complexity = O(n)

Space Complexity = O(n)


Related Solutions

Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2. => union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2. =>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.
In C: Find a string within a string Given two strings S1 & S2, search for...
In C: Find a string within a string Given two strings S1 & S2, search for an occurrence of the second string within a first string. Note: Do not use system library for the implementation. Input: Code Zinger University Zinger where, First line represents string S1. Second line represents string S2. Output: 5 Here 'Zinger' word starts at 5th index within 'Code Zinger University’. Assume that, The length of strings S1 & S2 are within the range [1 to 10000]....
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2...
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2 of the same length n. What is the complexity of your algorithm?
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2...
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2 of the same length n. What is the complexity of your algorithm?
Given the strings s1 and s2 that are of the same length, create a new string...
Given the strings s1 and s2 that are of the same length, create a new string s3 consisting of the last character of s1 followed by the last character of s2, followed by the second to last character of s1, followed by the second to last character of s2, and so on
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must...
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must do 3 things: 1. Take two parameters, the original String (String s1) and the substring (String s2) that is to be removed; 2. Create a new String that consists of the original String data less all occurrences of the substring data; 3. Return the new String. Note: 1. The data to be removed is each occurrence of the complete substring, not individual characters of...
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists...
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists some probability for an atom in S2 to decay to S1. What actually causes the atom to decay to the lower energy state? is it the fact that the lower state is more probable for the atom to be in as given by the Boltzmann Factor? so since it is more probable, it has more microstates and entropy causes it to decay? Please help...
Given the two stacks S1 and S2 each with a set of numbers, write an algorithm...
Given the two stacks S1 and S2 each with a set of numbers, write an algorithm to check if the two stacks are identical (have the same information). Use only the Push and Pop operations. The original stacks should be kept. Briefly explain the time complexity of your algorithm.
1.   What is the output of the following code: string s1 = “X”; string s2 =...
1.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s2; 2.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s[0] + s[3]; 3.   What is the output of the following code: string s1 = “X”; string...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT