EBK DATA STRUCTURES AND ALGORITHMS IN C
EBK DATA STRUCTURES AND ALGORITHMS IN C
4th Edition
ISBN: 9781285415017
Author: DROZDEK
Publisher: YUZU
Question
Book Icon
Chapter 4, Problem 5PA
Program Plan Intro

a. Program to find sum of large numbers by adding each number to resultant:

Program Plan:

  • In function “addingLargeNumbers()”:
    • It takes two stacks corresponding to two large numbers as parameters.
    • Till any one of stack becomes empty,
      • Pop each digit from both stacks and add them with carry.
      • Push resultant digit into resultant stack and store carry digit in “carryy”.
    • If first stack is not empty, till stack becomes empty,
      • Pop each digit from stack and add them with carry.
      • Push resultant digit into resultant stack and store carry digit in “carryy”.
    • If second stack is not empty, till stack becomes empty,
      • Pop each digit from stack and add them with carry.
      • Push resultant digit into resultant stack and store carry digit in “carryy”.
    • If carry is not “0”, push it into resultant stack.
    • Return resultant stack.
  • In function “main()”:
    • Read number of large numbers to find sum and first two numbers from user.
    • Compute sum of first two numbers by calling function “addingLargeNumbers()”.
    • Read each number from user and add it with resultant number by calling function “addingLargeNumbers()”.
    • After adding all numbers. Pop each digit from resultant stack and push into other stack to get digits of result in correct order.
    • Print result.

Program Plan Intro

b. Program to find sum of larger numbers by using vector of stacks:

Program Plan:

  • In function “addingLargeNumbers()”:
    • It takes a vector of stacks and number of elements in vector as parameters.
    • Compute maximum number of digits “maxsz” a number having in vector.
    • For each digit in first position to “maxsz” position
      • Add each digit of all stacks together and pop each digit from stack.
      • Replace old stack with new stack.
      • Add result with carry. Push resultant digit into resultant stack and store carry digit in “carryy”.
    • After adding all digits of all numbers, if carry is not “0”, push it into resultant stack.
    • Return resultant stack.
  • In function “main()”:
    • Read number of large numbers to find sum.
    • Read all numbers from user and store them in a vector.
    • Call function “addingLargeNumbers()” by passing vector and vector size as parameters. It will return resultant stack.
    • Print resultant stack.

Blurred answer
Students have asked these similar questions
It's a very nice application of stacks. Consider that a freight train has n railroad cars. Each to be left at different station. They're numbered 1 through n & freight train visits these stations in order n through 1. Obviously, the railroad cars are labeled by their destination. To facilitate removal of the cars from the train, we must rearrange them in ascending order of their number (i.e. 1 through n). When cars are in this order, they can be detached at each station. We rearrange cars at a shunting yard that has input track, output track & k holding tracks between input & output tracks (i.e. holding track). The figure shows a shunting yard with three holding tracks H1, H2 & H3, also n = 9. The n cars of freight train begin in the input track & are to end up in the output track in order 1 through n from right to left. The cars initially are in the order 5,8,1,7,4,2,9,6,3 from back to front. Later cars are rearranged in desired order.     To rearrange cars, we…
Use stacks from the STL to implement a video game library functionality. In this implementation, consider a VideoGame class with title, release year, genre and publisher as variables. Provide setters and getters to these variables.  In main, create a stack of video games. Add 5 different game objects into the stack. Then in a loop, for each item that you retrieve from the stack display the game information as follows:   TITLEYEARGENREPUBLISHER
It is known that a matrix can be understood (in python) by a list of lists. In this context, make a program that receives 9 numbers, organize them in a matrix 3 x 3 and print this matrix and its transpose.   In Phyton3
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning