Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++

Name: Anonymous 2010-04-17 17:20

For an assignment I need to have a program print the inputted number, and every number before it in sequence ex:

1
12
123
1234
12345
123456

I can get it to print a list of the number but for the love of god I cant get it to print the numbers before hand.  I know theres a simple way to do it nesting a while loop in a for loop but I can't fucking figure it out.

Help for a poor retarded aspiring programmer?

Name: Anonymous 2010-04-17 17:30

SEE, THESE KIDS DON'T READ THEIR SICP

Prelude Data.List> let hw = (mapM_ print).tail.inits.enumFromTo 1
Prelude Data.List> hw 4
[1]
[1,2]
[1,2,3]
[1,2,3,4]

Name: Anonymous 2010-04-17 17:38

whats SICP?

Name: Anonymous 2010-04-17 17:40

Should I?  I shall, shalln't I?  I'm going jump ahead and assume you start at 1 always.

//x is the input
int i = 1, j = 1;
for(; i <= j && j <= x; i++)
{
   cout>>i;
   if(i == j)
   {
      i = 1;
      j = j + 1;
      count>>'\n';
   }
}

Name: Anonymous 2010-04-17 17:42

Even better, but uglier.

Prelude Data.List> let hw x = mapM_ (\xs -> (mapM_ (putStr.show) xs) >> putStrLn "") (tail.inits $ enumFromTo 1 x)
Prelude Data.List> hw 5
1
12
123
1234
12345
Prelude Data.List>

Name: Anonymous 2010-04-17 17:45

if you can't figure this out, just give up now.

Name: Anonymous 2010-04-17 17:57

>>4
Ack.  count>>'\n'; should be cout>>'\n';.

Name: Anonymous 2010-04-17 17:57

Unrelated question; why do C/C++ programmers like to put the incrementing variable definition outside of the for statement?

int i = 0;
for (; i < 5; i++) {

rather than

for (int i = 0; i < 5; i++) {

Name: Anonymous 2010-04-17 17:59

op here

#4 thank you

#6 I'm new and you're a dick

#8 I was taught to do it the second way you show there.

Name: Anonymous 2010-04-17 18:06

>>9
Excuse me, I was under the impression that >>8 was addressed to C/C++ programmers, so why did you reply?

Name: Anonymous 2010-04-17 18:15

>>10

Because he asked a C++ question in my thread about C++ in which I asked a question about C++


You're excused.  You're also not very helpful.

Name: Anonymous 2010-04-17 18:21

>>8
Historical reasons. I thought C++ was always exempt from it, but I have a rule against reading other people's Sepples code.

Name: Anonymous 2010-04-17 18:27

>8
variable declarations in for statement are only valid in C99, not older C89/C90 or earlier nonstandard versions of C (and Visual C++ doesn't support C99 at, just something like C89/C90 and C++98/1x)

>9,11
stop whining

Name: Anonymous 2010-04-17 18:42

I'm too lazy to read every post, but
>>1-13
YOU HELPED HIM!

I'm really getting tired of bitching about how much better /prog/ used to be, but really, every time I come on here these days there's nice little bundle of shitty threads to greet me. I can't help it; it's your problem, not mine.

Name: Anonymous 2010-04-17 19:13

>>14
But >>1 didn't help himself ...

Name: Anonymous 2010-04-17 19:20

>>13
variable declarations in for statement are only valid in C99
I'm surprised by that. Why bother with an initialization expression at all if you can't declare variables in it? That's its the idiomatic purpose. I wonder if there was a good reason not to support that.

Name: Anonymous 2010-04-17 19:26

>>16
in old C, variables could only be defined at the beginning of a block - this restriction was lifted in C99

Name: Anonymous 2010-04-17 19:29

>>17
I'm surprised that /prog/ has so few C programmers who know this.

Name: Anonymous 2010-04-17 19:35

>>17
I meant to say declared, not defined

>>18
it is unfortunate

Name: Anonymous 2010-04-17 19:48

>>13,17-19
I wrote the code and I didn't know that either.  It's just a habit I picked up from a project where I kept sending code to be checked to a partner and he kept complaining that his compiler wouldn't accept the for loop declarations.

Name: Anonymous 2010-04-17 21:53

Fuck you, OP. Fuck you.

You're not new, you're an idiot.

Name: Anonymous 2010-04-17 22:18

>>4
#include <stdio.h>

void hw(char n)
{ for(char i = '1'; i <= n; ++i, putchar('\n'))
    for(char j = '1'; j <= i; putchar(j++)); }

int main(void)
{ hw(getchar());
  return 0; }


>>5
Prelude List> let hw = mapM_ putStrLn . tail . inits . enumFromTo '1' . head . show :: Int -> IO ()
Prelude List> hw 5
1
12
123
1234
12345
Prelude List>

Name: Anonymous 2010-04-17 22:28

(dotimes (i 7) (dotimes (j i (terpri)) (princ (1+ j))))

Name: Anonymous 2010-04-17 22:38

/prog/ is now know as /kindergarten/

Name: Anonymous 2010-04-17 22:45

>>23
it figures LISP would have specialized functions for toy problems like this in it's standard library.

Name: Anonymous 2010-04-17 22:58

reported for underage b&

Name: Anonymous 2010-04-17 23:33

>>25
How is that different from a for loop, because it's not, besides DOTIMES is useful, and CL tries to include commonly used functions/macros in the language. While the core itself only has 25 special operators (some of which could be implemented through other special operators, but are kept as is for efficiency reasons), everything else can be built through functions and macros. There's no real reason to be minimalistic.

Here's how one could implement DOTIMES:

(defmacro dotimes ((var count-form &optional result-form) &body body)
  (with-gensyms (count)
    `(do ((,var 0 (1+ ,var))
      (,count ,count-form))
     ((>= ,var ,count) ,result-form)
       ,@body)))


DO itself can be implemented as a macro, in form of LOOP (simple version), and LOOP itself can be implemented in form of TAGBODY and GO which are special operators. Common Lisp's goals are the same as most sane language's, that is, to help the programmer, and when they didn't think of something that you might want, give you enough tools so that you can easily build it yourself.

Name: Anonymous 2010-04-17 23:38

>>27
terpri.

Name: Anonymous 2010-04-18 0:19

>>28
It just prints a newline.
(princ #\newline) would do the same, or write-char, or something even more low-level if you wish...
The name itself is probably one of the worst function names ever, but it's just historical baggage. Most of the I just use FORMAT and ~% or ~&.

Name: Anonymous 2010-04-18 0:57

>>29
it's a function that only exists for the sake of toy programs. if you find yourself writing code to print a newline by itself very often, you're doing something very wrong.

Name: Anonymous 2010-04-18 1:47

>>30
Do you write much Lisp?

Name: Anonymous 2010-04-18 2:50

>>31
Does anyone write much Lisp?

Name: Anonymous 2010-04-18 3:40

>>32
Do you write any Lisp?

Name: Anonymous 2010-04-18 4:00

>>2
If he can't solve this, how do you expect him to read SICP?

Name: Anonymous 2010-04-18 4:23

Here's two more silly solutions:


;;; what one should write after reading a chapter of SICP (this is CL, but it's trivial to rewrite this in scheme, mostly by changing DEFUN/LABELS to DEFINE/LET and removing the lambda list stuff):

(defun print-pyramid-numbers (&optional (n 6))                             
  (labels ((print-numbers (n &optional (i 1))
             (cond
               ((= n i) (terpri))
               (t
                (princ i)
                (print-numbers n (1+ i)))))
           (rec-print (n &optional (i 1))
             (when (>= n i)
               (print-numbers i)
               (rec-print n (1+ i)))))
    (rec-print (1+ n))))

;;; What one would write after learning about LOOP:
(loop for i from 1 to 6 do
      (loop for j from 1 to i do (princ j)
            finally (terpri)))

Name: Anonymous 2010-04-18 4:33

void print_pyramid_numbers(int num) {
    int i, j;
    for(i = 1; i <= num; i++) {
        for(j = 1; j <= i; j++) printf("%i", j);
        printf("\n");
    }
}

Name: Anonymous 2010-04-18 4:38

This thread has been closed and replaced with the following thread:

Subject: Data.List and List
Name:
Email:


What's the difference between the two modules? The former seems to have few functions the latter doesn't.

Name: Anonymous 2010-04-18 9:20

>>37
I reject this action.

import itertools
doyourownhomework = lambda n: ''.join(map(str, (e for i in range(n)
    for e in itertools.chain(range(1, i + 2), '\n')))).strip()

Name: Anonymous 2010-04-18 9:38

Anybody can recommend a good book on C++?

Name: Anonymous 2010-04-18 10:44

>>39
Twilight

Name: Bella Swan 2010-04-18 11:24

Hax my anus, Edward Cullen!

Name: Anonymous 2010-04-18 12:36

>>37
Hey! You can't have BBCODE in the thread subject!!!!

Name: Anonymous 2010-04-18 12:44

,------------------------------------------------>++++++++++++++++++++++++++++++++++++++++++++++++>+<<[>>[>+>+<<-]>>[<<+>>-]<[<<+.>>-]++++++++++.----------<[>+>+<<-]>>[<<+>>-]<[<<->>-]<+<< -] 

VALID BF CODESadly it only works upto 9 ;_;

Name: Anonymous 2010-04-18 12:47

>>43
BB[code] FAILURE

,------------------------------------------------>++++++++++++++++++++++++++++++++++++++++++++++++>+<<[>>[>+>+<<-]>>[<<+>>-]<[<<+.>>-]++++++++++.----------<[>+>+<<-]>>[<<+>>-]<[<<->>-]<+<< -]


VALID BF CODE Sadly it only works upto 9 ;_;

Name: sega 2010-04-18 17:50

Hey, /prog/.  Your board sucks dick.  Just sayin'.

Name: Anonymous 2010-04-18 23:38

>>45
I'm glad you think that. I am totally not being facetious.

Name: Anonymous 2010-04-19 1:39

b:\>perl -E "say 1..$_ foreach 1..7"
1
12
123
1234
12345
123456
1234567

b:\>

Can your FIOC do that? Can it?! Hah.

Name: Anonymous 2010-04-19 1:49

>>47
[print(*range(1, i+1), sep="") for i in range(1, 8)]

Name: Anonymous 2010-04-19 2:16

[code]
#include <iostream>
using namespace std;

int main() {
    int wut;
    cin >> wut;
    for (int a = 1; a <= wut; a++) {
        for (int b = 1; b <= a; b++) {
            cout << b;
        }
        cout << endl;
    }
}

Name: Anonymous 2010-04-19 3:44

>>40
Kind of cold but true
& you can also say that
Reading the other parts of this epic epic would do you even more good

Name: Anonymous 2010-04-19 5:29

>>47
Prelude Control.Monad Data.List> forM_(inits[1..7])print
[]
[1]
[1,2]
[1,2,3]
[1,2,3,4]
[1,2,3,4,5]
[1,2,3,4,5,6]
[1,2,3,4,5,6,7]
Prelude Control.Monad Data.List>


or if you want it to look nice:
Prelude Control.Monad> forM_[concatMap show[1..x]|x<-[1..7]]putStrLn
1
12
123
1234
12345
123456
1234567
Prelude Control.Monad>

Name: Anonymous 2010-04-19 5:31

>>47
My faggot hipster language can:

[code
ruby -e '(1..7).each{|i|puts((1..i).to_a.join)}'
[/code]

Name: Anonymous 2010-04-19 5:32

>>52
IN BEFORE BBCODE FAILURE DETECTED


ruby -e '(1..7).each{|i|puts((1..i).to_a.join)}'

Name: Anonymous 2010-04-19 6:08

(for-each (lambda (x) (for-each display x) (newline))
          (list-tabulate 7 (lambda (x) (iota (add1 x) 1))))

or
(do-ec (:range i 1 8)
         (begin                     
           (do-ec (:range j 1 (add1 i))
                  (display j))
           (newline)))

Name: Anonymous 2010-04-19 6:44

Can someone please tell me why all these people are printing the numbers from 1 to 6 as if the OP didn't specify the number has to be inputted and it's not always 6?

Also what IS the difference between List and Data.List?

Name: Anonymous 2010-04-19 6:49

Also what IS the difference between List and Data.List?
http://en.wikibooks.org/wiki/Haskell/Hierarchical_libraries
When Haskell 98 was standardised modules were given a flat namespace. This has proved inadequate and a hierarchical namespace has been added by allowing dots in module names. For backward compatibility the standard libraries can still be accessed by their non-hierarchical names, so the modules List and Data.List both refer to the standard list library.

Name: Anonymous 2010-04-19 7:59

>>55 HAPPY NOW FAGGOT

ruby -e '(1..ARGV[0].to_i).each{|i|puts((1..i).to_a.join)}'

Name: Anonymous 2010-04-19 8:08

I love it how perl, considered here the most line noisy language among all popular provides the most concise and readable solution for the problem. Just have a look at that punctuation-fest ruby.

Name: Anonymous 2010-04-19 8:21

>>57 YES THANK YOU

Name: Anonymous 2010-04-19 8:41

>>58
Readability depends very much on exposure to the language. When I saw the perl solution I was thinking "WTF is $_", although it was easy enough to figure out as we were given the solution. IMO, the most readable ones are in the C-style with explicit loops.

Name: Anonymous 2010-04-19 9:01

>>58
try factor:
( scratchpad ) : hw ( n -- ) [1,b] [ [1,b] [ pprint ] each nl ] each ;
( scratchpad ) 7 hw
1
12
123
1234
12345
123456
1234567
( scratchpad )

Name: Anonymous 2010-04-19 9:20

>>60
My point is that even knowing language, you still have to read the whole program to guess what it will do. The larger it is, the more you'll spend on processing it. say 1..$_ foreach 1..7 is the the easier to process for perl programmer than (1..ARGV[0].to_i).each{|i|puts((1..i).to_a.join)} is for ruby coder.

Name: Anonymous 2010-04-19 11:13

>>62
most ruby coders don't really read any code. they just copy and paste bits of code from random blogs until it does what they want.

Name: Anonymous 2010-04-19 11:55

>>63
and so life imitates art

Name: Anonymous 2010-04-19 11:58

>>62
APPLES AND ORANGES
The perl solution is limited to 1..7 , where as the ruby solutions provides ENTERPRISE GRADE FLEXIBILITY

Name: Anonymous 2010-04-19 12:26

>>47
can your line noise do this?
take <$> [1..7] <*> [[1..]]

Name: Anonymous 2010-04-19 12:30

>>66
or even better:
take 7 $ take <$> [1..] <*> [[1..]]

Name: Anonymous 2010-04-19 12:53

x := input

while x > 1:
   x = floor(x/10)
   print(x)

Not sure I understand OP's question right but that's how I'd do it.

Name: Anonymous 2010-04-19 13:13

>>68
I don't think you did..

Name: Anonymous 2010-04-19 13:18

-accelerated c++
-the c++ programming language (the bible, form Stroustrup)

Name: Anonymous 2010-04-19 13:24

@69 Lol hy think you did my anus lol lets all do twitter style replies because it's so hipster and cool and written in ruby which is also hipster and cool

Name: Anonymous 2010-04-19 16:40

>>71
Please die.

Name: Anonymous 2010-04-19 16:53

>>72
@71 lol i think you'd like me if you got to me. i've just been to the grocery store to by some supplies

Name: Anonymous 2010-04-19 16:56

>>73 see >>72

Name: Anonymous 2010-04-19 18:07

.say for[\~]1..6

Name: Anonymous 2013-09-01 14:51


Arithmetic operations similar to those given below for the extended real numbers can also be defined, though there is no distinction in the signs (therefore one exception is that infinity cannot be added to itself). On the other hand, this kind of infinity enables division by zero, namely z/0 = \infty for any nonzero complex number z. In this context it is often useful to consider meromorphic functions as maps into the Riemann sphere taking the value of \infty at the poles. The domain of a complex-valued function may be extended to include the point at infinity as well. One important example of such functions is the group of Möbius transformations.

Name: Anonymous 2013-09-01 14:57

>>75
[\~](1..6).say;

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List