Arrays

Finding the Maximum and minimum values

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2646 Views - Last Post: 01 October 2008 - 02:29 PM Rate Topic: -----

#1 wakemalibu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 30-September 08

Arrays

Posted 30 September 2008 - 06:57 PM

In a program assignment I have to make a program that scans an array of numbers that are in a file defined by the user such as a notepad document. The program will scan the array of numbers and then define which numbers are the maximum and minimum values of the set of numbers. I have no idea how to set the code up. This part of the code is but 1 of like 7 options I have to work on. The current code that i have defines the option as

void maxminArray (double x[], int n)- this is just a function of the whole program attached is the whole program thus far.

obiviously this part reads the array that the user defines but I need it to compare the numbers inside the file and then use couts to display the

Max Value =
Min Value =

Im not sure wether or not im suppose to use if statements. If so, can anyone give me a setup of the code?

/****************************************************************
 *															  *
 *	  Program 2 - First Skeleton							  *
 *  The program is made modular by use of separate functions.   *
 *  A switch statement is used for the menu.					*
 *  File input is used to read in numbers of an array.		  *
 *															  *
 *  Author: David Hume										  *
 *  Date of Creation: September 23, 2008						*
 *															  *
 *															  *
 ****************************************************************/


#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;

   /* Function prototypes allow the main function to call		  */
   /* those functions without knowing their internal details.	  */
   /* The prototype describes the argument list and return value.  */

void printArray (double x[], int  n); // function prototype
void displayMenu ();				  // function prototype
void readArray (double x [], int &n); // function prototype
void sumArray (double x[], int n);	// function prototype
void maxminArray (double x[], int n); // function prototype

int main ()
{
   /* The following variables are visible in the main function.  */

   bool	processAnotherFile = true;
   char	response;
   bool	menuActive;
   char	menuChoice;

   double   x[100];  //array of up to 100 numbers
   int	  n;  // counts array elts x[0],x[1],...,x[n-1]

   while (processAnotherFile)
   {

	  readArray  (x, n);  // call the readArray function

	  menuActive = true;
	  while (menuActive)
	  {
		 displayMenu ();   // call displayMenu function
		 cin >> menuChoice; cin.ignore(80,'\n');
		 cout << endl;

		 switch (menuChoice)
		 {
			case 'P': case 'p': case '1':
			   printArray (x, n);
			   break;
			case '2':
			   //cout << "Menu Option 2 executed" << endl << endl;
			   break;
			case 'S': case 's': case '3':
			   //cout << "Menu Option 3 executed" << endl << endl;
			   sumArray (x, n);
		   break;
			case '4':
			   cout << "Menu Option 4 executed" << endl << endl;
			   break;
			case 'Q': case 'q': case '5':
			   menuActive = false; // Finish this series of menu choices
			   break;
			default:
			   cout << "	   Invalid menu choice: try again!" << endl << endl;
		 }  // end switch statement
	  } 
	  
	  cout << "Process another file? y/n : ";
	  cin  >> response; cin.ignore(80,'\n');
	  if (response == 'n' || response == 'N') processAnotherFile = false;
	  cout << endl << endl;
   }  // end while (goAgain) loop

   return 0;
}  // end main function

/****************************************************************
 *															  *
 *	 This function handles details of reading a 1-dimensional *
 * array of elements of type double.  After prompting for a	 *
 * filename, the file is opened, and the numbers are read and   *
 * stored in an array as x[0],x[1],x[2],...,x[n-1].			 *
 *															  *
 * First argument: the array where numbers are stored -- passed *
 *	by reference (as are all arrays by default).			  *
 *															  *
 * Second argument: n = number of elements in the array -- also *
 *	passed by reference using the "&" notation.			   *
 *															  *
 ****************************************************************/
void readArray (double x [], int &n)
{
   int	  i;

   char	 filename[51]; // room for 50-character path/file name
   ifstream inputFile;	// object declaration: input file stream

   cout << "Enter filename (and path, if needed):" << endl;
   cin.getline (filename, 51);
   cout << endl << endl;
   
   inputFile.open(filename);

   i = 0;
   
   while (inputFile.eof() == false)
   {	  
	  inputFile >> x[i];
	  if (!inputFile.eof())  // means that x[i] was successfully read
		 i++;   
   }
   inputFile.clear();  // clear flags associated with the file
   inputFile.close();
   n = i;  // n = number of elements in array

}  // end readArray function

/****************************************************************
 *															  *
 *	 This function displays a 1-dimensional array of numbers  *
 * of type double in four columns of field width 10 with two	*
 * decimals to the right of the decimal point.				  *
 *															  *
 * First argument: the array of numbers is passed by reference, *
 *	as is the case for all arrays are.						*
 *															  *
 * Second argument: n = number of elements in the array; this   *
 *	argument is passed by value, meaning that the value comes *
 *	in to the function, but cannot be sent back out.		  *
 *															  *
 *															  *
 ****************************************************************/
void printArray (double x[], int n)
{
   int i;
   int columnCount;

   cout << endl << endl;
   cout << "Echo of Array Values from Input File:" << endl << endl;
   cout << setprecision(2) << fixed << right;
   columnCount = 0;
   for (i=0; i < n; i++)
   {
	  cout << setw(12) << x[i];
	  columnCount++;
	  if (columnCount == 6) 
	  {
		 columnCount = 0;
		 cout << endl << endl;
	  }

   }
   cout << endl << endl;
   cout << "Number of elements in array: " << n << endl << endl;
}  // end printarray function

/****************************************************************
 *															  *
 *	This function determines the algebraic sum of all		 *
 * elements in the array and the sum of the absolute values	 *
 * of all elements in the array.								*
 *															  *
 * First Argument: the array of numbers are added together	  *
 *															  *
 * Second Argument: the array of numbers are added together	 *
 *	but with their absolute figures added together.		   *
 *															  *
 *															  *
 ****************************************************************/

void sumArray (double x[], int n)
{ 
   double sum, sumAbsolute;
   int i;
   sum = 0.0;
   sumAbsolute = 0.0;

   for (i=0; i <= n-1; i++)
   {   sum = sum + x[i];
		  
   }

  cout << " Sum of values		  = " 
  << setprecision(2) << setw(5) << right << fixed << sum << endl;

   for (i=0; i <= n-1; i++)
   {   sumAbsolute = sumAbsolute + fabs(x[i]);
  
   }	  
  
  cout << " Sum of absolute values = "
  << setprecision(2) << setw(5) << right << fixed << sumAbsolute << endl << endl;
}

/****************************************************************
 *															  *
 *	 This function determines the maximum and minimum values  *
 *   in an array.											   *
 *															  *
 *   First argument: the array of numbers are compared.		 *
 *															  *
 *   Second argument: two numbers are pulled out as the		 *
 *	  algebraically maximum and minimum values and displayed. *
 *															  *
 *															  *
 ****************************************************************/

void maxminArray (double x[], int n)
  

/****************************************************************
 *															  *
 *		 This function displays the main menu.  There are no  *
 *   input arguments to the function, and no data is returned.  *
 *															  *
 ****************************************************************/

void displayMenu ()
{
   cout << "   *************************************************" << endl;
   cout << "   *											   *" << endl;
   cout << "   *		   M A I N	M E N U				  *" << endl;
   cout << "   *											   *" << endl;
   cout << "   *   1) (P)rint Array							*" << endl;
   cout << "   *   2) (M)ax and Minimum Array				  *" << endl;
   cout << "   *   3) (S)um Array							  *" << endl;
   cout << "   *   4) option 4								 *" << endl;
   cout << "   *   5) (Q)uit: Finish processing this array	 *" << endl;
   cout << "   *											   *" << endl;
   cout << "   *************************************************" << endl;
   cout << "	   Enter choice: ";
}  // end displayMenu function





Is This A Good Question/Topic? 0
  • +

Replies To: Arrays

#2 wakemalibu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 30-September 08

Re: Arrays

Posted 30 September 2008 - 07:51 PM

For those looking at the code and going omg lol. the main question is how do I write the code that will scan an array of numbers for the maximum and minimum values?
Was This Post Helpful? 1

#3 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Arrays

Posted 30 September 2008 - 08:48 PM

just do a for loop comping each value

*not the real code just a outline of what needs to be done
// Store the min and max values
int min,max
// Set the min and max to the first value in the array
max = min = array[0]
// Do a for loop to look at every value in the array
for(int i=0; i<=Array_Size; i++)
	//if the value in array[i] is less then min then set the new min
	if(array[i] < min)
		min = array[i]
	//if the value in array[i] is bigger then max then set the new max
	if(array[i] > max)
		max = array[i]

Was This Post Helpful? 0
  • +
  • -

#4 wakemalibu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 30-September 08

Re: Arrays

Posted 30 September 2008 - 10:27 PM

okay i tried out some of the coding but with my code some of my dummie sets of data work and then some of them dont. Its suppose to compare each number in the data set and decide as its reading the values a maximum and a minimum value.

 void maxminArray (double x[], int n)
{
   double min, max;
   int i;
	  

   for (i=0; i <= n-1; i++)
   { 
	  if (x[i] >= max)
	   max = x[i];
	 
   }

cout << " Max value = " << setprecision(2) << setw(5) << right
	 << fixed << max << endl;
	 
   for (i=0; i <= n-1; i++)
   { 
	  if (x[i] <= min)
	   min = x[i]; 
	 
   }

cout << " Min value = " << setprecision(2) << setw(5) << right 
	 << fixed << min << endl;
}


This post has been edited by wakemalibu: 30 September 2008 - 10:32 PM

Was This Post Helpful? 0
  • +
  • -

#5 red_4900  Icon User is offline

  • Code T(h)inkers
  • member icon

Reputation: 21
  • View blog
  • Posts: 1,120
  • Joined: 22-February 08

Re: Arrays

Posted 01 October 2008 - 01:54 AM

View PostrealNoName, on 30 Sep, 2008 - 08:48 PM, said:

// Set the min and max to the first value in the array
max = min = array[0]

It would be better if you initialize max to INT_MIN, and min to INT_MAX. Does that make sense to you mathematically? ;)
Was This Post Helpful? 0
  • +
  • -

#6 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Arrays

Posted 01 October 2008 - 04:16 AM

your problem might be because you really need to initialize min and max to something before you start your for loop, if you dont your going to be unknown random results, just to show you what i am talking about do a little debugging and put a cout << min << ' ' << max << endl; before you start your for loop and you will see that there are random values set

you have 2 options you could set min and max to 0 but you still could get some problems with that so the best way is to set the min and max to the first value in your array


@red_4900
???
the only point of that statement was to initialize the values and they are both going to be the same so why does it matter
Was This Post Helpful? 0
  • +
  • -

#7 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Arrays

Posted 01 October 2008 - 04:28 AM

It's just a way to guarantee it'll work~ Think about it, the minimum that the user enters can't be below INT_MIN, (this minimum int value) can it?
And the same goes for INT_MAX~

:)
Was This Post Helpful? 0
  • +
  • -

#8 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Arrays

Posted 01 October 2008 - 04:46 AM

it must just be way to early and i will look at this post when i wake up... just for the hell of it doesn't my way guarantee that it will work because it is setting both min and max as the first value the user entered?
Was This Post Helpful? 0
  • +
  • -

#9 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Arrays

Posted 01 October 2008 - 05:01 AM

Yeah, it would work~ Your method is just less common than red's method.

:)
Was This Post Helpful? 0
  • +
  • -

#10 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Arrays

Posted 01 October 2008 - 05:17 AM

good, why would i want to do it the common way :)

This post has been edited by realNoName: 01 October 2008 - 05:17 AM

Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Arrays

Posted 01 October 2008 - 05:46 AM

I'm going to have to second realNoName's method. It is the preferred way. The exception is when you are forced to initialize before you have any values.

I'd make a minor change to the code:

int min,max;
max = min = array[0];
// start at 1
// also, in a zero indexed array, the size should always be last_element + 1
for(int i=1; i<Array_Size; i++)


Was This Post Helpful? 0
  • +
  • -

#12 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Arrays

Posted 01 October 2008 - 05:52 AM

I would back realNoName's method too. If you already know that the first element we are going to compare will always be greater than or equal to INT_MIN and less than or equal to INT_MAX then why not assign it as min and max? that also reduces 1 iteration of for loop. :)
Was This Post Helpful? 0
  • +
  • -

#13 wakemalibu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 30-September 08

Re: Arrays

Posted 01 October 2008 - 06:52 AM

well you can't really use int_Max and min because all the numbers wil not be integers. there will be decimals and numbers that are outside the int denomination. int only covers
-32567 to 32657 i think. thats why i used double to allow for a much broader range and numbers that include decimals. also if im trying to set Max and min to the first number in the array, i will not know those numbers since the numbers are going to be coming from a random file my professor sets when he enters in the file at the beginning of the program. But just to check anyways, wouldn't i just initialize Max = Min = x[0]? or would it be Max = min = x[i]?
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Arrays

Posted 01 October 2008 - 07:44 AM

View Postwakemalibu, on 1 Oct, 2008 - 09:52 AM, said:

wouldn't i just initialize Max = Min = x[0]?


Yes. You're loading the first value in your list into both place holders. If your array is length 1, the min and max are the same. All your doing is this:

Index  Value  Min  Max
0	  5	  5	5
1	  9	  5	9
2	  7	  5	9
3	  2	  2	9



So, you load the value from the 0 index, and start looping at 1.
Was This Post Helpful? 1
  • +
  • -

#15 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Arrays

Posted 01 October 2008 - 08:53 AM

wow i just looked at what red_4900 said again and well i finally get lol... i still like my way better :)

This post has been edited by realNoName: 01 October 2008 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2