Can anyone offer any help please? Trying to learn C++, I'm stuck on this problem

Can anyone offer any help please? Trying to learn C++, I'm stuck on this problem

For this assignment you will write a program to subtract large unsigned
integers. Your program should prompt and read in two large unsigned integers.
The two large integers should be stored arrays, one array for each
integer. The integers should be stored with one digit per location in the two
arrays. The first integer should be no smaller than the second. Your program
should subtract the second integer from the first. The result should be stored
in an array, again one digit per location in the array. Your program should
then output the difference.
Your program should be able to handle integers with up to 80 digits each.
You should not use any concepts from object oriented programming. In
particular please do not use any classes (or struct) or any class methods. The
one exception is that you may use the iostream class method cin.get().
Test data:
10000000000000000000000000000000000
- 9
1111111111111111111111111111111111
- 10000010001
123456789123456789123456789
- 99999
9999999999
- 9999999999

Other urls found in this thread:

en.wikipedia.org/wiki/Method_of_complements#Decimal_example
en.wikipedia.org/wiki/Subtraction
twitter.com/AnonBabble

Implement the standard subtraction algorithm you learned in middle school basically.

at first i thought the problem was you had to read it as a long long or something and didnt know how to separate the digits but then i realized you need to accept input per character anyway and you just cant write an algorithm which is second grade math

this is worrying. i mean, if you had at least said where you were stuck i'd give you a few pointers but it sounds like you havent even tried

Just figure out how to do the "borrow" part.
You can do it like you would by hand.
You can also do 9's complement.
en.wikipedia.org/wiki/Method_of_complements#Decimal_example

>The integers should be stored with one digit per location

That's retarded as fuck.

you can use modulo operator to get each digit out of each integer. is that basically all you're asking? or do you also not know how to do everything else?

>You should not use any concepts from object oriented programming. In
>particular please do not use any classes (or struct) or any class methods. The
>one exception is that you may use the iostream class method cin.get().

Then why even use C++ if you can't even use std::string?

Solution:
cpp.sh/6brpe

Fixed version: cpp.sh/7yeni
A cleaner c++ version: cpp.sh/6gesc

10^80 will overflow an int64_t.

...

>he actually struggles with 2nd grade subtraction
Can't tell if you're a biofag LARPing as a dumb CS major meme or a fa/g/got who's about to drop out from freshman becausa all you can do is Lisp fizzbuzz.

CS majors really are this stupid.

>A cleaner c++ version: cpp.sh/6gesc

If you're going to over do it, go all the way and use classes and provide all the operations.

cpp.sh/9fw7

I don't even understand why this string isn't converted to some integer format, operated on, and the result is converted back into a string for output. Do not write arithmetic operations for strings. Wasting cycles.

I also don't get why you are calling it an array and not a string.

>I don't even understand why this string isn't converted to some integer format

Integers are finite precision. 32/64/128bit numbers can only store 9/19/38 digits and OP's professor wants 80. The point is to implement your own simplified version of boost::multiprecision::cpp_int.

>I also don't get why you are calling it an array and not a string.

Because OP's professor doesn't want him to.

>>provide all the operations
>no conversion back to ints

Your class is worthless if you can't get numbers back out

>explicit operator unsigned long long() const{
>>unsigned long long n=0;
>>for(const auto& digit : num){
>>>n*=10;
>>>n+=char_to_num(digit);
>>}
>>return n;
>}

...

My uni requires all CS to take a choice of two: Calc 3, linear algebra, or diffy q

its sounds like an entry level programming course ie. introduction to programming

yes, it is a retarded, inefficient data representation, but they aren't exactly writing a high performance arbitrary length integral math library, its only to serve as an example to them, mainly as a way so they can implement an elementary school algorithm

i imagine its purely for the sake of this assignment, and while they could just use C, it wouldn't help at all. personally i think schools/unis should start by teaching C programming so students get a good handle on procedural programming, as well as the c standard library, before migrating over to C++

this is what my university did and it worked great in my opinion

>i imagine its purely for the sake of this assignment, and while they could just use C, it wouldn't help at all. personally i think schools/unis should start by teaching C programming so students get a good handle on procedural programming, as well as the c standard library, before migrating over to C++

C++ standard library is better. Why fuck around with cstring and char* when you have std::string

Anywhere but in the US you can't do CS without at the very least calc 1 (calc 3 for reputable schools). Try again.

>implying calculus 3 is impressive
>implying cs majors retain a working knowledge of calculus after they barely pass the course

kek

Oh I'm sorry. You probably live in Detroit, so you're comparing American calc 3 to civilized real analysis which contains calc 3, topology, integration and summability theory and asymptotical analysis.

post syllabus or gtfo

I don't know if the people who post this picture are memeing or they're just actual idiots. That's the Veeky Forums dilemma I guess.

addition by subduction

in country you can't do comp sci if you haven't done clac 2 and linear algebra

>Don't use one of the most powerful tools in the language
>Waste memory by using 1 array cell for a digit
Brainlet fucking problem
>What is 2nd grade subtraction
Brainlet fucking thread. Seriously man, just read this en.wikipedia.org/wiki/Subtraction and learn what cpp arrays are and you're set. Stop embarassing yourself and DIY.

>You can major in CS without taking linear algebra

Yeah.. I don't even understand how this is considered a problem at all. ASCII already represents numbers in a sequence. There is zero work to this.

This. Wtf. What a shit institution.

>I once thought that I would have to compete with people from Britain in the workforce

See

First two years, while you physishits were busy wrapping your head around muh small variation of x
>Complex numbers and trigonometry
>Integral calculus
>Linear diff eqs
>Sequences
>Continuous functions, differentiable functions
>Asymptotical analysis (you know, the fundamental thing that nobody here knows because muh l'hospital's rule)
>Naive set theory
>General algebra
>Number theory
>Polynomials
>Linear algebra in finite and infinite dimensions
>Inner product spaces
>Numerical series
>Combinatorics and discrete probability theory
>Countability
>Sequences and series of functions
>Improper integrals and parameter integrals
>Summability theory and integrability theory
>Normed vector spaces and Banach spaces
>Topology in metric spaces
>Compacity
>Power series
>Partial derivatives
>Differentials
>Point mechanics
>Rotating solid mechanics
>Charged particle mechanics
>Schrodinger's equation
>Electromagnetism (integral and local laws)
>Vector calculus
>Maxwell's equations
>Circuit analysis
>Optics
>Thermodynamics (1st and 2nd laws, state changes, statistics)
>Chemistry (thermodynamics, acids, redox, kinetics)
>Signal processing
>Logic and program proofs
>Tree and graph theory
>Numerical methods
>Dynamic programming
>Language theory and finite state automata
There. That's the bare minimum to enroll for CS in a civilized country after doing two years of these at 44+ hours per week.

inb4 physics boi rationalizes his uneducated situation by pretending he did more at freshman and I don't remember 1% of that