Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Little qick question.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Caceres




PostPosted: Sun Jul 23, 2006 9:57 pm   Post subject: Little qick question.

Hey, I've bene working on this program for HOURS! I'm a noob so it take sme forever. Razz
Anyways.. I just ran into a very minor error.
I do a statement to find the minimum value, but the termaining code is "-1". So it always returns the -1 as the minimum value.

My whole program is as follows:
code:
import TerminalIO.KeyboardReader;
import becker.io.*;
import java.io.*;

public class ass52 {
   public static void main(String[] args) {
      KeyboardReader reader = new KeyboardReader();
      double[] number = new double[21];
      double sum = 0;
      int count = 0;
      double average = 0;
      double maximum=number[0];
      double minimum=number[0]; 

      System.out.println("\nType -1 when you're finished entering your marks.");
      System.out.println("\nEnter up to 20 marks and the program will calculate the average.");

      while(number[count]!=-1&&count!=20) {           
              count=count+1;
              System.out.print("Enter mark " + (count) + ": ");            
                number[count] = reader.readDouble();         
         if(number[count]!=-1)
                sum = sum + number[count];
       
        if(number[count] > maximum) {
                maximum=number[count];
        }
       
       
        if(number[count] < minimum) {
                minimum=number[count];
        }
                               
   }
        if(count==20)
                average=sum/count;
        else if(count<=19)
                average=sum/(count-1);
         
           
      System.out.println("\n\nThe average is " +average+".");
      System.out.println("\n\nThe sum of the numbers is " +sum+".");
      System.out.println("\nThe maximum value entered is " +maximum+".");
      System.out.println("\nThe minimum value entered is " +minimum+".");
   }   
}


I was just wondeing how to fix the "minimum" problem. Thanks.
Baically: a statement that would not include the "-1" into the minimum. Thanks.
~Caceres
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: Sun Jul 23, 2006 10:21 pm   Post subject: (No subject)

You could try using a break statement:

Java:
if (number[count] == -1) break;

basically tells the computer to "break out of the loop" if you get -1.

That should also simplify your loop-condition.

Be careful to review how adding the break statement changes your program.
Caceres




PostPosted: Sun Jul 23, 2006 10:22 pm   Post subject: (No subject)

Where would I add that statemtn in? Right before the minimum?
OneOffDriveByPoster




PostPosted: Sun Jul 23, 2006 10:27 pm   Post subject: (No subject)

Caceres wrote:
Where would I add that statemtn in? Right before the minimum?

Part of programming is knowing how to use the constructs you learn to get the desired behaviour. I believe that pointing you to "break" is about as much as I would do.
wtd




PostPosted: Sun Jul 23, 2006 10:40 pm   Post subject: (No subject)

Amazingly enough, well-formatted code is easier to reason about.

code:
import TerminalIO.KeyboardReader;
import becker.io.*;
import java.io.*;

public class ass52 {
   public static void main(String[] args) {
      KeyboardReader reader = new KeyboardReader();
      double[] number = new double[21];
      double sum = 0;
      int count = 0;
      double average = 0;
      double maximum = number[0];
      double minimum = number[0];

      System.out.println("\nType -1 when you're finished entering your marks.");
      System.out.println("\nEnter up to 20 marks and the program will calculate the average.");

      while (number[count] != -1 && count != 20) {         
         count += 1;
         System.out.print("Enter mark " + count + ": ");             
         number[count] = reader.readDouble();         
         
         if (number[count] != -1) {
            sum = sum + number[count];
         }
       
         if (number[count] > maximum) {
            maximum = number[count];
         }
       
         if (number[count] < minimum) {
            minimum=number[count];
         }                           
      }
 
      if (count == 20) {
         average = sum / count;
      } else if (count <= 19) {
         average = sum / (count - 1);
      }
           
      System.out.println("\n\nThe average is " + average + ".");
      System.out.println("\n\nThe sum of the numbers is " + sum + ".");
      System.out.println("\nThe maximum value entered is " + maximum + ".");
      System.out.println("\nThe minimum value entered is " + minimum + ".");
   }   
}


You have a major flaw in your logic, though.

You keep referring to elements in an array before you've initialized them.
Caceres




PostPosted: Sun Jul 23, 2006 10:42 pm   Post subject: (No subject)

Thanks wtd.

I'm just curious if there's such thing as:

if(number[count] < minimum) {
minimum!=-1;
minimum=number[count];
}

The minimum!=-1 doesn't work, but I was wondeirng if there's a statement I can add there, that'll erase the -1 data. THanks.

And by the way, does wtd..mean What the duck?

~Caceres
wtd




PostPosted: Sun Jul 23, 2006 11:23 pm   Post subject: (No subject)

Caceres wrote:
Thanks wtd.

I'm just curious if there's such thing as:

if(number[count] < minimum) {
minimum!=-1;
minimum=number[count];
}

The minimum!=-1 doesn't work, but I was wondeirng if there's a statement I can add there, that'll erase the -1 data. THanks.


No. You can only set it to another value. You cannot set it to some undefined state.

Quote:
And by the way, does wtd..mean What the duck?

~Caceres


No, and yes.

As for your Java problem... perhaps you should concentrate on reading in the grades, and then worry about finding the average, min and max.

You will need at least one value known, after all, if you wish to make comparisons to figure out the minimum and maximum.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: