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

Pascal's triangle

Name: Anonymous 2012-01-05 17:41

Motivated by the Hacker News challenge in http://news.ycombinator.com/item?id=3429466 I just made this.


#include <stdio.h>
#include <stdlib.h>

int64_t* next_pascal_row(int64_t* previous_row, int64_t previous_size)
{
  int64_t* retval = (int64_t*)malloc(sizeof(int64_t)*(previous_size+2));
  int64_t i;
  for (i=0;i<previous_size+1;i++) {
    if (i==0 || i == previous_size) {
      retval[i] = 1;
    } else {
      retval[i] = previous_row[i-1] + previous_row[i];
    }
  }
  return retval;
}

void print_row(int64_t* row, int64_t size)
{
  int64_t i;
  for(i = 0; i < size; i++) {
    printf("%lld ",row[i]);
  }
  printf("\n");
}

int main()
{
  int64_t* first_row = (int64_t*)malloc(sizeof(int64_t));
  int64_t* previous_row;
  int64_t* next_row;
  int64_t i,size;
  first_row[0] = 1;
  size = 1;
  print_row(first_row,size);
  previous_row = first_row;
  for(i = 0; i<30;i++) {
    next_row = next_pascal_row(previous_row,size);
    size++;
    print_row(next_row,size);
    free(previous_row);
    previous_row = next_row;
  }
  free(next_row);
}

Opinions? Yeah, int64_t is probably a GNU extension, I don't care as long as it works with gcc. U mad? Also, post you're own solutions. If you can't do the Pascal, you can just go ahead and apply to your're nearest McDonald's, because you ain't worth shit.

Name: Anonymous 2012-01-07 7:12

Name: Anonymous 2012-01-07 9:40

New fizzbuzz over here

Name: Anonymous 2012-01-07 17:35

>>80 not too bad, but you can use pure Maths for that, which results in simpler code.

Name: Anonymous 2012-01-07 18:20

>>80 #!/usr/bin/env python
fixed that for you

Name: Anonymous 2012-01-07 21:00

>>81
Presburger Arithmetic? I don't know what thiat is, but it sounds like ASS BURGERS ASS SHIT.

Name: Anonymous 2012-01-07 22:53

>>79
more of them, just never 'all'.
You're too greedy.

Name: Anonymous 2012-01-07 23:43

ASPERGER ARITHMETIC

Name: Anonymous 2012-01-08 17:58

What's wrong with my code? I feel like the values are r are changing when they shouldn't...


#include <stdio.h>
#include <stdlib.h>

typedef long long size;

void main(void) {
  int i, j, n;
  size *r, *r0;
 
  printf("Row: "); scanf("%d", &n);
 
  r = (size *)malloc(sizeof(size) * (n + 1));
  r0 = (size *)malloc(sizeof(size) * (n + 1));
 
  r[0] = 1; r[1] = 1;
  puts("1 1");
  for(i = 2; i <= n; ++i) {
    r0 = r;
    r[0] = 1; r[i] = 1;
   
    for(j = 1; j < i; ++j)
      r[j] = r0[j-1] + r0[j];

    for(j = 0; j <= i; ++j)
      printf("%lld ", r[j]);
     
    putchar('\n');
  }
  free(r);
}

Name: Anonymous 2012-01-08 18:02

>>88
Should have mentioned, the output of that code is
1 1
1 2 1
1 3 4 1
1 4 8 9 1
1 5 13 22 23 1
...

Name: Anonymous 2012-01-08 18:06

>>88
void main
the space allocated in r0 = (size *)malloc(sizeof(size) * (n + 1)); is dereferenced but not freed before it's even used
[m]r0[/code] is there for no reason
Why do you do that?

Name: Anonymous 2012-01-08 18:10

lol

U MENA PASKALL

Name: Anonymous 2012-01-08 18:12

>>90
What do you mean r0 is not used? I use it in every iteration. Also, why would you free r0 before it's used? I don't get it....

I also never understood the point of declaring main as int then not returning anything.

I'm not very good at this, someone help

Name: Anonymous 2012-01-08 18:34

>>88
Read on pointers. r0 is pointing to the same address as r, so the space allocated by it is never used.

Name: Anonymous 2012-01-08 18:36

>>92
First, the first thing you do in the loop is assign r to r0, effectively discarding r0's malloced space.
Second, you can easily remove r0 altogether and your shitty code will still do the same thing it does now, just a bit better.
Third, the return value from main is returned to the OS (usually the shell or other program executing it) and is required by the C standard.

Go read your K&R already.

Name: Anonymous 2012-01-08 18:39

No one has said anything about my code. Can someone tell me if it sucks? Pretty please? I'm not very good at this C thing.
>>74

Name: Anonymous 2012-01-08 18:57

>>94
I think I understand now. I did think that the r0 = r part of my code was odd. It seems work work now. Thanks.


#include <stdio.h>
#include <stdlib.h>

typedef long long size;
void copy(size*, size*, int);

int main(void) {
  int i, j, n;
  size *r, *r0;
 
  printf("Row: "); scanf("%d", &n);
 
  r = (size *)malloc(sizeof(size) * (n + 1));
  r0 = (size *)malloc(sizeof(size) * (n + 1));
 
  r[0] = 1; r[1] = 1;
  puts("1 1");
  for(i = 2; i <= n; ++i) {
    copy(r0, r, i);
    r[0] = 1; r[i] = 1;
   
    for(j = 1; j < i; ++j)
      r[j] = r0[j-1] + r0[j];

    for(j = 0; j <= i; ++j)
      printf("%lld ", r[j]);
     
    putchar('\n');
  }
  free(r);
  free(r0);
}

void copy(size *r0, size *r, int n) {
  int i = 0;
  for(; i <= n; ++i)
    r0[i] = r[i];
}


Is there a better way to copy the contents by the way?

Name: Anonymous 2012-01-08 18:59

Third, the return value from main is returned to the OS
It still returns a value, even if main is void

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-08 19:03

>>94

First, the first thing you do in the loop is assign r to r0, effectively discarding r0's malloced space

That's incorect. r0's malloced space doesn't get discarded at this point in the loop. This space actually gets used. Cripes, relearn pointers you fucking stupid shit.

Third, the return value from main is returned to the OS (usually the shell or other program executing it) and is required by the C standard.

The return value is undefine according to both ANSI/ISO C89 and C99.

Go read your K&R already.

I think you need to go practice what you preach you mental midget.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-08 19:05

>>98

*The return value is undefined*

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-08 19:07

>>97

No, assuming this could compile on a standard ANSI/ISO C compiler, the function would return. It just wouldn't return a value.

Name: Anonymous 2012-01-08 19:09

>>98
>>99
>>100
I think you need to find a new job jobless_programmer!!kCq+A64Losi56ze-san that will teach you how to code properly

Name: Anonymous 2012-01-08 19:10

>>100
But the OS still gets a return value, right?

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-08 19:11

>>101

I think you need to find a programming job you dumb toilet scrubber.

Name: Anonymous 2012-01-08 19:14

>>103
i'm sorry did i hurt your feelings? It's okay, I'm sure it was 100% your fault the company failed and went under. Maybe next time you'll learn better.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-08 19:14

>>102
Depending on the OS, the return value could possibly be the exit status of the process. But I don't know because what you have is non standard C code. In other words, I can't get this thing to compile on either my Linux or Windows XP box.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-08 19:15

>>104
Nah. I think you just need to shut the fuck up and learn C before you criticize others.

Name: Anonymous 2012-01-08 19:16

So much FIOC and C ITT, and only one Scheme solution at >>29, and it just so happens to be the best.

I am disappoint, /frog/.

Name: Anonymous 2012-01-08 19:17

>>98-100

kodak-san! you are being helpful!

Name: Anonymous 2012-01-08 19:21

>>1
the Hacker News challenge
It's not a fucking challenge. It was mentioned as an example of a simple programming problem to weed out weak job candidates. Only you talentless autistic hacks would ever consider something like this a challenge, and then actually do it to ``prove yourself''.

If you want a challenge, build a facial recognition system from scratch using only training data that you've gathered yourself. That's a fucking challenge. Not this two-lines-of-C Pascal triangle shit, Jesus Christ.

Name: Anonymous 2012-01-08 19:24

>>102
return 0;

stop listening to kodak_jobless_moron, he's just like all the other tripfags who have no clue what they are talking about and act like elitists because they can.

Name: Anonymous 2012-01-08 19:26

>>109
Here's one that we use to weed out some of the week candidates.

We show the person a balanced binary tree. Then we ask them to write a function in either C or C++ that would convert this tree to a doubly linked list. The restrictions being that you can't use an external stack and you can't use any kind of global variables.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-08 19:27

>>110
Cite the exact passage in C89 or C99 you stupid shit. Oh wait, you can't! Now I see why you don't work as a computer programmer.

Name: Anonymous 2012-01-08 19:28

>>111
er *weak candidates*

Name: <- checkem trips 2012-01-08 19:29

Name: oh no, failure 2012-01-08 19:30

Name: your mom 2012-01-08 19:31

Name: that whore 2012-01-08 19:45

Name: Anonymous 2012-01-08 19:49

>>111
Huh, Brian Harvey at Berkeley has an anecdote involving almost the exact same problem.

http://www.youtube.com/watch?v=lMJNQIrWDJk#t=225

Name: Anonymous 2012-01-08 19:50

>>112
Now I see why you're unemployed

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-08 19:51

>>119
Huh? You still can't find the passage to support your incorrect assertions can you? Also, I still have a job. Now shut up and go scrub another toilet.

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