1 Replies - 4297 Views - Last Post: 26 June 2009 - 01:00 PM Rate Topic: -----

#1 bockmawn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-March 09

Largest, Smallest, Median program

Post icon  Posted 26 June 2009 - 10:35 AM

Hello,

I needed help with part of my program, which reads integers from a text file, whose name is given as a command-line argument, and finds the largest number, the smallest number, and the median in the integer array made by the text file.



Here is my code thus far:
#include <stdio.h>

#define MAX_LEN 10000


/*Prototypes */
void selection_sort(int a[], int n);
int find_largest(int b[], int N);
int find_smallest(int c[], int m);
int find_median(int d[], int M);


int main(int argc, char *argv[])
{
	int i, count = 0;

	int list[MAX_LEN] = {0};

	FILE* pFile;

	pFile = fopen(argv[1], "r");


/* Checks if file opens correctly from command line. */
	if (argc != 2)
	{
		printf("usage: can open filename\n");
		return 1;
	}

	if ((pFile = fopen(argv[1], "r")) == NULL)
	{
		printf("%s can't be opened\n", argv[1]);
		return 1;
	}


/* Read in numbers from file. */
	for (i = 0; i < MAX_LEN; i++)
	{
		fscanf(pFile, "%d", &list[i]);

		count++;		 

		if (feof(pFile))
		{
			break;
		}

		if (ferror(pFile))
		{
			break;
		}
	}


/* Rearrange values in array */
	selection_sort(list, count);


/* Print out results. */
	printf("Largest: %d	Smallest: %d	Median: %d\n", find_largest(list, count), find_smallest(list, count), find_median(list, count));
   


/* Closes file. */
	fclose(pFile);
	return 0;
}


/* Uses selection sort to arrange array from smallest to */
/* largest.											  */
void selection_sort(int a[], int n)
{
	int i, j;
	int  temp, max;

	if (n <= 1)
	{
		return;
	}

	for (i = 0; i < n - 1; i++)
	{
		max = i;

		for (j = n - 1; j > i; j--)
		{
			if (a[j] < a[max])
			{
				max = j;
			}
		}

		temp = a[i];
		a[i] = a[max];
		a[max] = temp;
	}

	return selection_sort(a, n - 1);
}


/* Returns largest element in array. */
int find_largest(int b[], int N)
{
	int largest;

	largest = b[N - 1];

	return largest;
}


/* Returns smallest element in array. */
int find_smallest(int c[], int m)
{
	int smallest;

	smallest = c[m - m];

	return smallest;
}


/* Returns median in array. */
int find_median(int d[], int M)
{
	int index, another_index, median, average;

	if ((M % 2) == 1) /* Count is odd. */
	{
		index = ((M - 1) / 2);

		median = d[index];
	} 

	else if ((M % 2) == 0) /* Count is even. */
	{
		index = (M / 2);

		another_index = index - 1;

		average = ((d[index] + d[another_index]) / 2);

		median = average;
	}

	return median;
}





I have managed to get the program the way I wanted to work, so far. It is showing the right outputs for largest, smallest and median.


For example, "Largest: 13002 Smallest: 4 Median: 93"


However, I did not take into account that when I use fscanf to read in each integer, it does not ignore any blank space characters that may be entered in each line before or after it reads in integers. So having a space in any of the lines in my text file would make:

"Largest: 13002 Smallest: 0 Median: 93"

This is something I do not want, and I'm kind of lost on how to fix this. Is there any approach I can use to ignore white spaces while reading in the integers? Any help would be appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Largest, Smallest, Median program

#2 athlon32   User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: Largest, Smallest, Median program

Posted 26 June 2009 - 01:00 PM

View Postbockmawn, on 26 Jun, 2009 - 09:35 AM, said:

Hello,

I needed help with part of my program, which reads integers from a text file, whose name is given as a command-line argument, and finds the largest number, the smallest number, and the median in the integer array made by the text file.



Here is my code thus far:
#include <stdio.h>

#define MAX_LEN 10000


/*Prototypes */
void selection_sort(int a[], int n);
int find_largest(int b[], int N);
int find_smallest(int c[], int m);
int find_median(int d[], int M);


int main(int argc, char *argv[])
{
	int i, count = 0;

	int list[MAX_LEN] = {0};

	FILE* pFile;

	pFile = fopen(argv[1], "r");


/* Checks if file opens correctly from command line. */
	if (argc != 2)
	{
		printf("usage: can open filename\n");
		return 1;
	}

	if ((pFile = fopen(argv[1], "r")) == NULL)
	{
		printf("%s can't be opened\n", argv[1]);
		return 1;
	}


/* Read in numbers from file. */
	for (i = 0; i < MAX_LEN; i++)
	{
		fscanf(pFile, "%d", &list[i]);

		count++;		 

		if (feof(pFile))
		{
			break;
		}

		if (ferror(pFile))
		{
			break;
		}
	}


/* Rearrange values in array */
	selection_sort(list, count);


/* Print out results. */
	printf("Largest: %d	Smallest: %d	Median: %d\n", find_largest(list, count), find_smallest(list, count), find_median(list, count));
   


/* Closes file. */
	fclose(pFile);
	return 0;
}


/* Uses selection sort to arrange array from smallest to */
/* largest.											  */
void selection_sort(int a[], int n)
{
	int i, j;
	int  temp, max;

	if (n <= 1)
	{
		return;
	}

	for (i = 0; i < n - 1; i++)
	{
		max = i;

		for (j = n - 1; j > i; j--)
		{
			if (a[j] < a[max])
			{
				max = j;
			}
		}

		temp = a[i];
		a[i] = a[max];
		a[max] = temp;
	}

	return selection_sort(a, n - 1);
}


/* Returns largest element in array. */
int find_largest(int b[], int N)
{
	int largest;

	largest = b[N - 1];

	return largest;
}


/* Returns smallest element in array. */
int find_smallest(int c[], int m)
{
	int smallest;

	smallest = c[m - m];

	return smallest;
}


/* Returns median in array. */
int find_median(int d[], int M)
{
	int index, another_index, median, average;

	if ((M % 2) == 1) /* Count is odd. */
	{
		index = ((M - 1) / 2);

		median = d[index];
	} 

	else if ((M % 2) == 0) /* Count is even. */
	{
		index = (M / 2);

		another_index = index - 1;

		average = ((d[index] + d[another_index]) / 2);

		median = average;
	}

	return median;
}





I have managed to get the program the way I wanted to work, so far. It is showing the right outputs for largest, smallest and median.


For example, "Largest: 13002 Smallest: 4 Median: 93"


However, I did not take into account that when I use fscanf to read in each integer, it does not ignore any blank space characters that may be entered in each line before or after it reads in integers. So having a space in any of the lines in my text file would make:

"Largest: 13002 Smallest: 0 Median: 93"

This is something I do not want, and I'm kind of lost on how to fix this. Is there any approach I can use to ignore white spaces while reading in the integers? Any help would be appreciated!


You could do things the hard way in C++; you could read the file contents into a string, and using some string functions of the standard library you could work with the data

http://www.cplusplus.../string/string/

hope i helped :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1