Algorithm addingLargeNumbers()
a create 3 empty stacks of integers (stack1, stack2, & result-stack)
b read numerals (digits) of first no & store nums corresponding to them on stack1
c read numerals (digits) of second no & store nums corresponding to them on stack2
d integer carry = 0
e while at least 1 stack is not empty
i pop a no from each nonempty stack & add them to carry
ii push unit part of sum on result-stack
iii store carry in carry
f push carry on result-stack if it is not zero
g pop numbers from result-stack & display them
Write C++ program for adding 2 very large nums
1 Use Stack class to store integers
2 Program should read 2 numbers from console & store them as 2 strings
Suppose max no of numerals (digits) in a no is 30
3 After reading input determine sum of 2 large numbers by above algorithm & print on console
Input
123456789123456789123456789
123454321123454321123454321
Output
23456789123456789123456789
123454321123454321123454321
246911110246911110246911110
Steve has a string of lowercase characters in range ascii[‘a’..’z’]. He wants to reduce the string to its shortest length by doing a series of operations. In each operation, he selects a triple of adjacent lowercase letters that match, and he deletes them. For instance, the string aaab could be shortened to b in one operation. Steve’s task is to delete as many characters as possible using this method and print the resulting string. If the final string is empty, print "Empty String" without quotes. Characters can be deleted only if they form a triple and are the same(i.e. from aaaa we can only delete 3 a's and will be left with a single a).
Example:
Sample Input: aaaabcccdd
Sample Output: abdd
Will the following lines of code print the same thing? Explain why or why not.
x = 7
print(x)
print("x")
Create a dynamic array of user defined size. Now move the largest number in the array to first position. Print the array after moving the number. All array operations should be done using pointers.
Write a program to create a dynamic array of user defined size. Array should be character type. Write a function RemoveVowels() that should remove all the vowels in the array. All array operations should be done using pointers.
// This function searches through str for the character chr.
// If the chr is found, it returns a pointer into str where
// the character was first found, otherwise nullptr (not found).
const char* findTheChar(const char str[], char chr)
{
for (int k = 0; str[k] != 0; k++)
if (str[k] == chr)
return &str[k];
return nullptr;
} double mean(const double* scores, int numScores)
{
const double* ptr = scores;
double tot = 0;
while (ptr != scores + numScores)
{
tot += *ptr;
ptr++;
}
return tot/numScores;
}The strequal function is supposed to return true if and only if its two C string arguments have exactly same text. Explain what the problems with the implementation of the function are, and show a way to fix them.
// return true if two C strings are equal
bool strequal(const char str1[], const char str2[])
{
while (str1 != 0 && str2 != 0) // zero bytes at ends
{
if (str1 != str2) // compare corresponding characters
return false;
str1++; // advance to the next character
str2++;
}
return str1 == str2; // both ended at same time?
}
int main()
{
char a[15] = "Wang, A.";
char b[15] = "Wang, R.";
if (strequal(a,b))
cout << "They're the same person!\n";
}The computeCube function is correct, but the main function has a problem. Explain why it may not work and show a way to fix it. Your fix must be to the main function only; you must not change computeCube in any way.
void computeCube(int n, int* ncubed)
{
*ncubed = n * n * n;
}
int main()
{
int* ptr;
computeCube(5, ptr);
cout << "Five cubed is " << *ptr << endl;
}This program is supposed to write 30 20 10, one per line. Find all of the bugs and show a fixed version of the program:
int main()
{
int arr[3] = { 5, 10, 15 };
int* ptr = arr;
*ptr = 30; // set arr[0] to 30
*ptr + 1 = 20; // set arr[1] to 20
ptr += 2;
ptr[0] = 10; // set arr[2] to 10
while (ptr >= arr)
{
ptr--;
cout << *ptr << endl; // print values
}
}