aboutsummaryrefslogtreecommitdiff
path: root/challenge-163/duncan-c-white/C/ch-2.c
blob: 3a77daf26c2d7a5c2a306b5de9fbee5f73549ca2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* 
 * TASK #2 - Summations
 * 
 * GUEST LANGUAGE: THIS IS THE C VERSION OF ch-2.pl.
 */ 

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <ctype.h>




bool debug=false;


// process_args( argc, argv, &nel, &v );
//	Process the -d flag, set nel to the number of
//	remaining arguments, check that all those
//	remaining arguments are +ve numbers, copy remaining
//	args to a new integer dynamic array v.
void process_args( int argc, char **argv, int *nel, int **v )
{
	int arg=1;
	if( argc>1 && strcmp( argv[arg], "-d" ) == 0 )
	{
		debug++;
		arg++;
	}

	*nel = argc-arg;
	if( *nel < 2 )
	{
		fprintf( stderr,
			"Usage: summations [-d] list of +ve numbers\n" );
		exit(1);
	}

	int lastpos = argc-1;

	// elements are in argv[arg..lastpos], copy them to new int v[] array

	// allocate the array
	*v = malloc( *nel * sizeof(int) );
	assert( *v != NULL );

	if( debug )
	{
		printf( "debug: arg=%d, lastpos=%d, nel=%d\n",
			arg, lastpos, *nel );
	}

	// check that all remaining arguments are +ve integers,
	// and then copy them to v[]
	for( int i=arg; i<=lastpos; i++ )
	{
		if( ! isdigit(argv[i][0]) )
		{
			fprintf( stderr,
			 "sum-bitwise-pairs: arg %d (%s) is not a +ve number\n",
			 i, argv[i] );
			exit(1);
		}
		(*v)[i-arg] = atoi( argv[i] );
		//printf( "debug: v[%d] = %d\n", i-arg, (*v)[i-arg] );
	}
}


int main( int argc, char **argv )
{
	int nel;
	int *v;
	process_args( argc, argv, &nel, &v );

	int r = 1;
	if( debug )
	{
		printf( "row %d:", r );
		for( int i=0; i<nel; i++ )
		{
			printf( " %d", v[i] );
		}
		putchar( '\n' );
	}
	while( nel > 1 )
	{
		// shift v array down
		for( int i=0; i<nel; i++ )
		{
			v[i] = v[i+1];
		}
		v[nel-1] = -1;
		nel--;

		for( int pos = 1; pos<nel; pos++ )
		{
			v[pos] += v[pos-1];
		}
		r++;

		if( debug )
		{
			printf( "row %d:", r );
			for( int i=0; i<nel; i++ )
			{
				printf( " %d", v[i] );
			}
			putchar( '\n' );
		}
	}
	printf( "%d\n", v[0] );

	free( v );
}