예제 #1
0
파일: math.c 프로젝트: priom/CCodes
int main(int argc, char* argv[]) {
    // open file
    FILE *f = fopen(argv[1],"r");

    // Throw an error if file not found
    if (f == NULL) {
        printf("Sorry, file not found.\n");
    }

    // Take the first line which is the count of number of lines
    int n;
    fscanf(f, "%d", &n);

    if (n > 4097) {
        printf("Sorry, file too large, cannot be more than 4097 lines");
    }

    if (n < 2) {
        printf("Sorry, file too short, cannot be less than 2 lines");
    }

    // Store the values from file in array
    int values[n];
    int i;
    for (i = 0; i < n; i++) {
        fscanf(f, "%d", &values[i]);
    }

    // Sort the values in ascending order
    qsort(values, n, sizeof(int), comparator);

    // Calculate the mean
    printf ("Mean = %0.2f\n", meanFunc(values, n));

    // Calculate the median
    printf ("Median = %0.2f\n", medianFunc(values, n));

    // Calculate the mode
    printf("Mode = %0.2f\n", modeFunc(values, n));

    // Find & print min value
    printf ("Min = %0.2f\n", minFunc(values));

    // Find & print max value
    printf ("Max = %0.2f\n", maxFunc(values, n));

    return 0;
}
예제 #2
0
/* @returns: 	 0 -> success
 * 				-1 -> inconsistent lower/upper specification
 *				-2 -> can't open pSampleInputFile
 *				-3 -> can't allocate enough memory
 *				-4 -> can't open pOutputParameters
 *				-5 -> can't open pInputParameters
 *				-6 -> can't open pScaledSampleOutputFile
 *				-7 -> cannot use read and store of parameters simultaneously
 *
 */
int SVM::scaleSamples(char *pSampleInputFile, char* pScaledSampleOutputFile, double dLowerBound, double dUpperBound, char* pInputParameters, char* pOutputParameters)
{
	char *line = NULL;
	int max_line_len = 1024;
	double lower=-1.0,upper=1.0,y_lower,y_upper;
	int y_scaling = 0;
	double *feature_max;
	double *feature_min;
	double y_max = -DBL_MAX;
	double y_min = DBL_MAX;
	int max_index;
	long int num_nonzeros = 0;
	int i,index;
	FILE *fp, *fp_restore = NULL;

	if(!(upper > lower) )
	{
		fprintf(stderr,"inconsistent lower/upper specification\n");
		return -1;
	}

	if(pInputParameters != NULL && pOutputParameters != NULL)
	{
		fprintf(stderr,"cannot use read and store of parameters simultaneously\n");
		return -7;
	}

	fp=fopen(pSampleInputFile,"r");

	if(fp==NULL)
	{
		fprintf(stderr,"can't open file %s\n", pSampleInputFile);
		return -2;
	}

	line = (char *) malloc(max_line_len*sizeof(char));

	/* assumption: min index of attributes is 1 */
	/* pass 1: find out max index of attributes */
	max_index = 0;

	if(pInputParameters != NULL)
	{
		int idx, c;

		fp_restore = fopen(pInputParameters,"r");
		if(fp_restore==NULL)
		{
			fprintf(stderr,"can't open file %s\n", pInputParameters);
			return -5;
		}

		c = fgetc(fp_restore);
		if(c == 'y')
		{
			line = this->read_line(fp_restore, 1024);
			line = this->read_line(fp_restore, 1024);
			line = this->read_line(fp_restore, 1024);
		}
		line = this->read_line(fp_restore, 1024);
		line = this->read_line(fp_restore, 1024);

		while(fscanf(fp_restore,"%d %*f %*f\n",&idx) == 1)
			max_index = max(idx,max_index);
		rewind(fp_restore);
	}

	while((line = this->read_line(fp, 1024))!=NULL)
	{
		char *p=line;

		SKIP_TARGET

		while(sscanf(p,"%d:%*f",&index)==1)
		{
			max_index = max(max_index, index);
			SKIP_ELEMENT
			num_nonzeros++;
		}
	}
	rewind(fp);

	feature_max = (double *)malloc((max_index+1)* sizeof(double));
	feature_min = (double *)malloc((max_index+1)* sizeof(double));

	if(feature_max == NULL || feature_min == NULL)
	{
		fprintf(stderr,"can't allocate enough memory\n");
		return -3;
	}

	for(i=0;i<=max_index;i++)
	{
		feature_max[i]=-DBL_MAX;
		feature_min[i]=DBL_MAX;
	}

	/* pass 2: find out min/max value */
	while((line = this->read_line(fp, 1024))!=NULL)
	{
		char *p=line;
		int next_index=1;
		double target;
		double value;

		sscanf(p,"%lf",&target);
		y_max = max(y_max,target);
		y_min = min(y_min,target);

		SKIP_TARGET

		while(sscanf(p,"%d:%lf",&index,&value)==2)
		{
			for(i=next_index;i<index;i++)
			{
				feature_max[i]=maxFunc(feature_max[i],0);
				feature_min[i]=minFunc(feature_min[i],0);
			}

			feature_max[index]=maxFunc(feature_max[index],value);
			feature_min[index]=minFunc(feature_min[index],value);

			SKIP_ELEMENT
			next_index=index+1;
		}

		for(i=next_index;i<=max_index;i++)
		{
			feature_max[i]=maxFunc(feature_max[i],0);
			feature_min[i]=minFunc(feature_min[i],0);
		}
	}

	rewind(fp);

	/* pass 2.5: save/restore feature_min/feature_max */

	if(pInputParameters != NULL)
	{
		/* fp_restore rewinded in finding max_index */
		int idx, c;
		double fmin, fmax;

		if((c = fgetc(fp_restore)) == 'y')
		{
			fscanf(fp_restore, "%lf %lf\n", &y_lower, &y_upper);
			fscanf(fp_restore, "%lf %lf\n", &y_min, &y_max);
			y_scaling = 1;
		}
		else
			ungetc(c, fp_restore);

		if (fgetc(fp_restore) == 'x') {
			fscanf(fp_restore, "%lf %lf\n", &lower, &upper);
			while(fscanf(fp_restore,"%d %lf %lf\n",&idx,&fmin,&fmax)==3)
			{
				if(idx<=max_index)
				{
					feature_min[idx] = fmin;
					feature_max[idx] = fmax;
				}
			}
		}
		fclose(fp_restore);
	}

	if(pOutputParameters != NULL)
	{
		FILE *fp_save = fopen(pOutputParameters,"w");
		if(fp_save==NULL)
		{
			fprintf(stderr,"can't open file %s\n", pOutputParameters);
			exit(1);
		}
		if(y_scaling)
		{
			fprintf(fp_save, "y\n");
			fprintf(fp_save, "%.16g %.16g\n", y_lower, y_upper);
			fprintf(fp_save, "%.16g %.16g\n", y_min, y_max);
		}
		fprintf(fp_save, "x\n");
		fprintf(fp_save, "%.16g %.16g\n", lower, upper);
		for(i=1;i<=max_index;i++)
		{
			if(feature_min[i]!=feature_max[i])
				fprintf(fp_save,"%d %.16g %.16g\n",i,feature_min[i],feature_max[i]);
		}
		fclose(fp_save);
	}

	/* pass 3: scale */
	FILE *fp_scaledOutput = fopen(pScaledSampleOutputFile,"w");
	if(fp_scaledOutput==NULL)
	{
		fprintf(stderr,"can't open file %s\n", pScaledSampleOutputFile);
		return -6;
	}

	while((line = this->read_line(fp,1024))!=NULL)
	{
		char *p=line;
		int next_index=1;
		double target;
		double value;

		sscanf(p,"%lf",&target);
		fprintf(fp_scaledOutput, "%s", this->output_target(target, y_scaling, y_lower, y_upper, y_min, y_max));

		SKIP_TARGET

		while(sscanf(p,"%d:%lf",&index,&value)==2)
		{
			for(i=next_index;i<index;i++)
				fprintf(fp_scaledOutput, "%s", this->output(i,0, feature_min, feature_max, lower, upper));

			fprintf(fp_scaledOutput, "%s", this->output(index, value, feature_min, feature_max, lower, upper));

			SKIP_ELEMENT
			next_index=index+1;
		}

		for(i=next_index;i<=max_index;i++)
			fprintf(fp_scaledOutput, "%s", this->output(i, 0, feature_min, feature_max, lower, upper));

		fprintf(fp_scaledOutput, "\n");
	}

	/*
	if (new_num_nonzeros > num_nonzeros)
		fprintf(stderr,
			"Warning: original #nonzeros %ld\n"
			"         new      #nonzeros %ld\n"
			"Use -l 0 if many original feature values are zeros\n",
			num_nonzeros, new_num_nonzeros);
			*/

	free(line);
	free(feature_max);
	free(feature_min);
	fclose(fp);
	fclose(fp_scaledOutput);
	return 0;
}