
C== LANGUAGE. Only use iostream and string header file. A bunch of checks i am also giving that needs to be satisfied for the code to be successful
if (add("123", "456") != "579")
if (add("123", "4") != "127")
if (add("1234", "9") != "1243")
if (add("88", "12") != "100")
if (add("1234567890123456789", "10000000000000999")
!= "1244567890123457788")
string longone(120, '2');
longone[0] = '3';
string longother(123, '1');
longother[0] = '4';
longother[3] = '2';
string longresult(123, '3');
longresult[0] = '4';
longresult[1] = '1';
longresult[2] = '1';
longresult[3] = '5';
if (add(longone, longother) != longresult )
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#include<iostream>
#include<string>
using namespace std;
//required method, assuming inputs are numeric strings only
string add(const string& num1, const string num2){
//creating an empty string to store result
string result="";
//variables to store the carry, sum and auxiliary digits and a character0
int carry=0, sum, digit1, digit2;
char chr;
//finding last indices of both strings
int index1=num1.length()-1;
int index2=num2.length()-1;
//loops as long as at least one of the indices is valid
while(index1>=0 || index2>=0){
//setting digits to 0
digit1=0;
digit2=0;
//if index 1 is valid, extracting digit at index1 in num1 to digit1
if(index1>=0){
//subtracting a digit by 0 returns the corresponding digit value
//between 0 and 9
digit1=num1[index1]-'0';
}
//doing the same for digit at num2
if(index2>=0){
digit2=num2[index2]-'0';
}
//adding digits (and carry if any)
sum=digit1+digit2+carry;
//resetting carry
carry=0;
//if sum is greater than 9
if(sum>9){
//storing 10th digit as carry
carry=sum/10;
//storing unit digit as sum
sum=sum%10;
}
//adding a digit with character '0' converts an int to corresponding char value
//like 0 becomes '0', 1 becomes '1' etc
chr='0'+sum;
//prepending this character to result
result=chr+result;
//moving to previous indices
index1--;
index2--;
}
//at the end, if the carry is more than 0, prepending the carry to result
if(carry>0){
chr='0'+carry;
result=chr+result;
}
return result;
}
int main(){
//testing
//creating two arrays of numeric strings
string num1[]={"123","123","1234","88","1234567890123456789"};
string num2[]={"456","4","9","12","10000000000000999"};
//looping and adding element corresponding indices in both array
for(int i=0;i<5;i++){
cout<<num1[i]<<" + "<<num2[i]<<" = "<<add(num1[i],num2[i])<<endl;
}
return 0;
}
/*OUTPUT*/
123 + 456 = 579
123 + 4 = 127
1234 + 9 = 1243
88 + 12 = 100
1234567890123456789 + 10000000000000999 = 1244567890123457788
C== LANGUAGE. Only use iostream and string header file. A bunch of checks i am also...
C++ I am using visual studio for it. Make a copy of the Rational class you created in the previous Lab. Modify the class. Replace all your mathematical, input, and output functions with overloaded operators. Overload the following 12 operators: + - * / < > = = ! = <= >= >> << In order to test your class in your main function, prompt the user for a numerator and a denominator for your first object. Repeat the prompt...
I only need the "functions" NOT the header file nor the main
implementation file JUST the implementations for the
functions
Please help, if its difficult to do the complete program I would
appreciate if you could do as much functions as you can especially
for the derived class.
I am a beginer so I am only using classes and pointers while
implementing everything using simple c++ commands
thank you in advanced
Design and implement two C++ classes to provide matrix...
For Python, I am a little confused on the use of reduce,map, and
lambda. My assignment wants me to not use .join() and write a
SINGLE LINE function to join each letter in a list with another
string.(See problem below).
Using reduce, map, and lambda, write the single-expression function myJoin(L, sep) that takes a non-empty list L and a string sep, and without calling sep.join(L), works roughly the same as that returning a single string with the values in L...
Assembly language 64 bit please !
An example file for set up
==========+
;| Data Segment BEGINS Here |
;+======================================================================+
segment .data
;Code this expression: sum = num1+num2
num1 dq 0 ;left operand of the addition operation
num2 dq 0 ;right operand of the addition operation
sum dq 0 ;will hold the computed Sum value
RetVal dq 0 ;Integer value RETURNED by function calls
;can be ignored or used as determined by the programmer
;Message string prompting for the keyboard...
c++ Write a rational number class. A rational number is a number that can be written as p/q where p and q are integers. The division is not carried out, only indicated. Thus you should represent rational numbers by two int values, numerator and denominator. Constructors must be present to create objects with any legal values. You should provide constructors to make objects out of pairs of int values; that is, a constructor with two int parameters. Since very int...
Please note that I cannot use the string library function and that it must be coded in C89, thank you! Formatting: Make sure that you follow the precise recommendations for the output content and formatting: for example, do not change the text in the first problem from “Please enter a string of maximum 30 characters:” to “Enter string: ”. Your assignment will be auto-graded and any changes in formatting will result in a loss in the grade. 2.Comments: Header comments...
please do in c++Q4. Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int) that when passed a non-negative integer, returns the number of occurrences of the digit 7 in the number. So, for example: count7s(717) → 2 count75(7) →1 count7s(123) → 0 Q5. Write a C++ function of the form string mirrorEnds(string)that when given a string, looks for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or...
I need help implementing class string functions, any help would be appreciated, also any comments throughout would also be extremely helpful. Time.cpp file - #include "Time.h" #include <new> #include <string> #include <iostream> // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for the hours, // an integer variable for the minutes, and a char variable for...
please write the code in
C++
2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...