[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Hey /g/, I'm a total newbie trying to make a simple cod
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 16
Thread images: 1
Hey /g/, I'm a total newbie trying to make a simple code on C++ that counts the number of time the largest digit of a number repeated itself as an assignment in school, but I can't seem to get it right. Can anybody help me?

Here's the code if anyone's interested
http://pastebin.com/h15HBW47
>>
>>51243893
/g/ is not for assignment solving, but I have one advice for you: your program is basically the translation of the procedure that you mentally use to solve the problem by hand. If you have no idea how to solve the problem by hand, you won't go far in your program.
>>
>>51243893
What the guy above me said except I was super bored.

http://pastebin.com/k44ryMnu
>>
>>51243893
The problem is easy, but C++ isn't the right language for it. It's more suited for a functional language. By recursion you can construct a list (or ordered binary tree, for more efficiency in finding the latest digit) of digits given a number, and you can then analyze that list.

Here's a quick implementation in Scheme, assuming you have remainder and integer quotient procedures defined:
(define (digit-list n)
(cond ((< n 10) (list n))
(else (cons (remainder n 10) (digit-list (quotient n 10))))))

(define (max-element list)
(if (null? (cdr list))
(car list)
(max (car list) (max-element (cdr list)))))

(define (count-in element list)
(cond ((null? list) 0)
((= element (car list)) (1+ (count-in element (cdr list)))
(else (count-in element (cdr list)))))

(define (problem number)
(let ((digits (digit-list number)))
(count-in (max-element digits) digits)))


t. sicp pro
>>
>>51243893
>int _tmain
stopped reading there
>>
>>51244062
This is infinitely more readable than OP's.
>>
Hello everyone. I have been wanting to learn how to use java script/coding and so on. so far i know how to lock files using java is there any schools i can go to. I would like to know more about it
>>
>>51244261
Codecademy
"Head first" books
"Learn X the hard way"
TheNewBoston (YT channel and website)

These and more are good starters
>>
Thank you very much.
>>
and is The onion router a good thing to use?
>>
>>51244238
I was going to give tips on how to make his code work instead of writing an implementation of my own... until I saw it.
>>
>>51244214
>FP
>or how to kill flies with nuclear bombs
>>
OPs required function in java, ultrareadable
>inb4 java sucks
I already know java sucks
    private static long function(long number) {
List<Integer> digits = new ArrayList<Integer>();
while(number != 0) {
digits.add((int) (number % 10));
number /= 10;
}
Integer largest = digits.stream().max(Comparator.<Integer>naturalOrder()).get();
long count = digits.stream().filter(integer -> integer.equals(largest)).count();
return count;
}
>>
Does anyone here use Tor?
>>
>>51244479
You seem to be lost, the thread you're looking for is /sqt/.
>>
Yes i am very lost right now i would look to know where i have stumbled on to
Thread replies: 16
Thread images: 1

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.