All pastes #739422 Raw Edit

Miscellany

public c v1 · immutable
·published 2007-10-17 04:05 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>int main() {		float *stocks, total, avg;		/*	m = the number of total stocks that we will be taking in	n = the number of stocks that we will use for each average	m - n = the number of averages that will be calculated	*/	int m, n, i, si, x;		printf("Please insert the number of stock prices to use for each average (n): ");	scanf("%d", &n);		printf("Please insert the number of total stock prices that will be used (m): ");	scanf("%d", &m);		/* error, dunno why we have this restriction... */	if(m <= (n+4)) {		printf("Condition m > (n+4) was not satisfied.");		return 0;	}		/* allocate the array */	stocks = (float*) malloc(n * sizeof(float));		/* get stock prices */	printf("Please print one number per line:\n");	for(i = 0; i < n; i++) {		scanf("%f", &stocks[i]);		total += stocks[i];	}		printf("\n\nStarting average is: %f\n", avg = total / n);		/* Loop over a few times to get a new stock price */	for(i = 0; i < (m - n); i++) {				/* remember the old first one */		x = stocks[0];					/* shifting all elements left.. wow this seems like the worst way to do things. */		for(si = 0; si < n-1; si++) {			stocks[si] = stocks[si+1];		}				printf("Today's stock price: ");		scanf("%f", &stocks[n-1]);		/*		printf("\nstocks[0] = %f", stocks[0]);		printf("\nstocks[1] = %f", stocks[1]);		printf("\nstocks[2] = %f", stocks[2]);		printf("\nx = %f", x);		printf("\nn = %d\n\n", n);*/		printf("AVG = %f - ( %f / %d ) + ( %f / %d )\n", avg, x, n, stocks[n-1], n);				avg -= (x/n) - (stocks[n-1]/n);				printf("Today's average: %f\n", avg);	}		return 0;}