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

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