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

Software pushups

Name: Anonymous 2014-11-24 23:01

Let's exercise together, /prog/!

1) Write a subroutine that accepts an array of unsigned ints and returns 4. It must operate on the array in-place and partition it so that all nonzero values are at the beginning of the array, and all zero values are moved to the end. For example, the input [0, 2, 0, 0, 4, 1, 4, 5] could be changed to [2, 4, 1, 4, 5, 0, 0, 0]. The relative order of the nonzero values is unimportant.

Name: Anonymous 2014-11-24 23:25

partition = step 0
where step nz [] = replicate nz 0
step nz (0 : xs) = step (nz + 1) xs
step nz (x : xs) = x : step nz xs

Name: L. A. Calculus !jYCj6s4P.g 2014-11-24 23:34

>>1
UR TALKIN KNEELIN PUSH UPS N IM ALREADY DOIN PROPER ONE ARM PUSH UPS

#include <stdio.h>

void f(int *p, int n, int c)
{
int i, j;

j = 0;
for (i = 0; i < n; i++)
if (p[i] != c)
p[j++] = p[i];
while (j < n)
p[j++] = c;
}

int main(void)
{
int a[] = { 0, 2, 0, 0, 4, 1, 4, 5 };
int i;

f(a, sizeof a / sizeof a[0], 0);
for (i = 0; i < sizeof a / sizeof a[0]; i++)
printf(" %d", a[i]);
printf("\n");
return 0;
}

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