All pastes #739880 Raw Edit

k4st

public c v1 · immutable
·published 2007-10-17 15:21 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>void digestline(void) {	scanf("%*[^\n]"); /* Skip to the End of the Line */	scanf("%*1[\n]"); /* Skip One Newline */}int main() {		int *nums_to_encode, *temp_nums, num_items = 0;	int input, args;	int i, p;		/* take in input by dynamically increasing the size of the array */	while(1) {		args = scanf("%d", &input);				if(args == 0) {			digestline();			continue;		} else {			/* found a number, reallocate space to the array */			if(args == 1) {								/* re-allocate memory for an array push */				if ((temp_nums = realloc(nums_to_encode, sizeof(int) * (++num_items))) == NULL) {					printf("An error occured while trying to allocate more space for the array");					break;				}				nums_to_encode = temp_nums;								/* add the number to the array */				nums_to_encode[num_items - 1] = input;							} else {				break;			}		}	}		printf("Encoded Version:\n");		for(i = 0; i < num_items; i++) {				/* go ahead and peek at future numbers */		for(p = i+1; p < num_items; p++) {						/* move i ahead */			if(nums_to_encode[i] != nums_to_encode[p]) {				//i = p;				break;			}		}				printf("%d %d\n", (p-i), nums_to_encode[i]);				/* 		 when i++ is called, i == p, and since p started as i+1,		 i = ((i+1)-1)++, so if the above loop goes anywhere, the		 value of i will take account of us peeking ahead to future		 numbers.		*/		i = p-1;	}		return 0;}