4 Replies - 474 Views - Last Post: 21 February 2011 - 11:09 PM Rate Topic: -----

#1 austinsmauston  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 29-October 09

Reading 10 values from user and finding max/min of the 10 values

Posted 21 February 2011 - 10:36 PM

using System;

namespace Assignment1
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			double[] numbers = new double[10];
			double max = numbers[0]; 
			double min = numbers[0];
			for(int i = 0; i < numbers.Length(); i++)
			{
				if(numbers[i] > max)
					numbers[i] = max;
				if(numbers[i] < min)
					numbers[i] = min;
				numbers[i] = Double.Parse(Console.ReadLine());
			}
			Console.WriteLine("The max is:" + max);
			Console.WriteLine("The min is:" + min);
		}
	}
}



I'm trying read 10 input values from the user and output the max and min. But i'm getting an error on my for loop saying that the member 'system.array.length' cannot be used as a method or delegate. Not sure how to fix it.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading 10 values from user and finding max/min of the 10 values

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4189
  • View blog
  • Posts: 11,864
  • Joined: 18-April 07

Re: Reading 10 values from user and finding max/min of the 10 values

Posted 21 February 2011 - 10:50 PM

You need to be setting max and setting min when you encounter a value in numbers which is greater than max or less than min. You have things backwards on your assignments. Also, you need to read the value FIRST before you can compare it against max/min.

for(int i = 0; i < numbers.Length(); i++)
{
     // Read into array first so it has a value
     numbers[i] = Double.Parse(Console.ReadLine());

     // If value in array is greater than max, set max to this new value
     if(numbers[i] > max)
          max = numbers[i]

     // If number is less than min, set min to this new value
     if(numbers[i] < min)
          min = numbers[i]
}

// At end here max will be max number encountered and min will be the minimum



As you go you have to compare the values to max/min and then if they are greater/less than those variables, reset the variables to the new values. :)
Was This Post Helpful? 1
  • +
  • -

#3 austinsmauston  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 29-October 09

Re: Reading 10 values from user and finding max/min of the 10 values

Posted 21 February 2011 - 11:04 PM

Alright well I arranged the variables in the right order. I'm still confused as to why my for loop gets that error I posted above.
using System;

namespace Assignment1
{
	class MainClass
	{
		public static void Main (string[] args)
		{					
			double[] numbers = new double[10];
			double max = numbers[0]; 
			double min = numbers[0];
			for(int i = 0; i < numbers.Length(); i++)
			{
					
				numbers[i] = Double.Parse(Console.ReadLine());
					
				if(numbers[i] > max)
				max = numbers[i];
				
				if(numbers[i] < min)
				min = numbers[i];
			}
			
			Console.Write("The max is:" + max);
			Console.Write("The min is:" + min);			
			
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4189
  • View blog
  • Posts: 11,864
  • Joined: 18-April 07

Re: Reading 10 values from user and finding max/min of the 10 values

Posted 21 February 2011 - 11:06 PM

Length is a property, not a method. Drop the parenthesis. :)
Was This Post Helpful? 1
  • +
  • -

#5 austinsmauston  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 29-October 09

Re: Reading 10 values from user and finding max/min of the 10 values

Posted 21 February 2011 - 11:09 PM

Thanks! Used to java i guess.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1