[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 80 KB, 300x129, You+see+her+smiling+and+grinning+but+her+eyes+tell+_8f791e47bc76473f3b620d6dce3cee96.gif [View same] [iqdb] [saucenao] [google]
9542911 No.9542911 [Reply] [Original]

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

>> No.9542948

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

>> No.9542968

>>9542911
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

>> No.9543055

>>9542911
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.
https://en.wikipedia.org/wiki/Method_of_complements#Decimal_example

>> No.9543078

>>9542911
>The integers should be stored with one digit per location

That's retarded as fuck.

>> No.9543089 [DELETED] 

>>9543078
he just means put an integer in each element


>>9542911
so you know literally nothing about programming? you dont know what an 'if' statement is to check that one digit is larger than the other and you dont know what a while statement is to iterate through an array?

>> No.9543093

>>9542911
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?

>> No.9543151 [DELETED] 

>>9542911

#include <iostream>
#include <cstring>
using namespace std;
constexpr size_t Max_num_of_digits = 128;

constexpr short char_to_num(char c){ return static_cast<short>(c - 48); } //48 is the ASCII code for 0
constexpr char num_to_char(short c){ return static_cast<char>(c + 48); }

void subtract_uchar(const char (&num1) [Max_num_of_digits], const char (&num2) [Max_num_of_digits], char (&diff)[Max_num_of_digits]){
bool borrow=false;
size_t num1_length=strlen(num1), num2_length=strlen(num2);
size_t i1=0;
//error checking for whitespace after the numbers
while(1){
if(num1[num1_length]<48 || num1[num1_length]>57) --num1_length;
else break;
}
while(1){
if(num2[num2_length]<48 || num2[num2_length]>57) --num2_length;
else break;
}

for(size_t i2=0; i1<=num1_length && i2<=num2_length; ++i1, ++i2){
auto result = char_to_num(num1[num1_length - i1])-char_to_num(num2[num2_length - i2]) - (borrow ? 1 : 0);
if(result < 0){
result += 10;
borrow = true;
}
else borrow = false;
diff[num1_length - i1] = num_to_char(result);
}
while(i1<=num1_length){
auto result = char_to_num(num1[num1_length - i1]) - (borrow ? 1 : 0);
if(result < 0){
result += 10;
borrow = true;
}
else borrow = false;
diff[num1_length - i1] = num_to_char(result);
++i1;
}
return;
}

bool greater_than_or_equal(const char (&num1) [Max_num_of_digits], const char (&num2) [Max_num_of_digits]){
bool ans = true;
for(size_t i=0; i<Max_num_of_digits; ++i){
if( num1[i] < num2[i] ) ans = false;
if( num1[i] > num2[i] ) ans = true;
if( (num1[i] == 0) || (num2[i] == 0) ) break;
}
return ans;
}

int main(){
char num1[Max_num_of_digits] ={0}, num2[Max_num_of_digits]={0}, diff[Max_num_of_digits]={0};
while(1){
cout<<"Input number #1: "<<flush;
cin >> num1;
cout<<"Input number #2: "<<flush;
cin >> num2;
if(!greater_than_or_equal(num1, num2))
cout<<"Error! Number 1 must be bigger than Number 2. Try again.\n";
else break;
}
subtract_uchar(num1, num2, diff);
cout<<"The difference is: "<<diff;
}

>> No.9543158

>>9542911
>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

>> No.9543193

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

>> No.9543351

>>9543093
10^80 will overflow an int64_t.

>> No.9543558

>>9542911
>>>/g/

>> No.9543589

>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.

>> No.9544034
File: 55 KB, 695x378, CS can&#039;t into calc.png [View same] [iqdb] [saucenao] [google]
9544034

>>9543589
CS majors really are this stupid.

>> No.9544237

>>9543193
>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

>> No.9544271

>>9542911
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.

>> No.9544375

>>9544271
>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.

>> No.9545491

>>9544237
>>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;
>}

>> No.9545496
File: 14 KB, 250x250, 1518278171862.jpg [View same] [iqdb] [saucenao] [google]
9545496

>>9544034

>> No.9545513

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

>> No.9545604

>>9543078
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

>>9543158
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

>> No.9545640

>>9545604
>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

>> No.9545744

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

>> No.9545753
File: 9 KB, 568x161, CS calculus.png [View same] [iqdb] [saucenao] [google]
9545753

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

kek

>> No.9545906
File: 77 KB, 616x754, 1464513534740.png [View same] [iqdb] [saucenao] [google]
9545906

>>9545753
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.

>> No.9547336

>>9545906
post syllabus or gtfo

>> No.9547366

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

>> No.9548162

addition by subduction

>> No.9548167

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

>> No.9548174

>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 https://en.wikipedia.org/wiki/Subtraction and learn what cpp arrays are and you're set. Stop embarassing yourself and DIY.

>> No.9549577
File: 38 KB, 303x350, 1519629282612.jpg [View same] [iqdb] [saucenao] [google]
9549577

>>9545513
>You can major in CS without taking linear algebra

>> No.9549626

>>9548174
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.

>> No.9549710
File: 123 KB, 268x199, 1519106019557.gif [View same] [iqdb] [saucenao] [google]
9549710

>>9549577
This. Wtf. What a shit institution.

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

>> No.9549838

>>9549577
See
>>9547889
>>9547892
>>9547894

>> No.9549991

>>9547336
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