Count the number of pos/neg integers, but doesn't include the 1st

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 535 Views - Last Post: 10 February 2012 - 03:31 PM Rate Topic: -----

#1 computer_sorry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 10-February 12

Count the number of pos/neg integers, but doesn't include the 1st

Posted 10 February 2012 - 12:48 PM

import java.util.Scanner;
public class AlqudA2Q2 {
**///created by computer_sorry///*
    public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    System.out.println("This calculates the amount of positive and negative values,\n"
            + "the total, and the average of the integers you enter.");
    System.out.println("Enter an integer value, the program exits if it is 0.");
    int data=input.nextInt();
    
    int sum = 0;
    int count =0;
    int totpos = 0;
    int totneg = 0;
    double average = 0;
    *////computer_sorry 2012////*
    while (data!=0){
         
        sum += data;
        average = sum/data;
        System.out.println("Enter an integer value, the program exits if it is 0.");
        data=input.nextInt();
        
        count++;
         *////computer_sorry 2012////*
        if (data>0)
            totpos++;
        if (data<0)
            totneg++;
    }
    System.out.println("The amount of positive integers is" +totpos);
    System.out.println("The amount of negative integers is" +totneg);
    System.out.println("The total of the integers is" +sum);
    System.out.println("The average of the integers is" +average);
    }   
}

This post has been edited by smohd: 10 February 2012 - 12:52 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: Count the number of pos/neg integers, but doesn't include the 1st

#2 HalfBlind  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 08-February 12

Re: Count the number of pos/neg integers, but doesn't include the 1st

Posted 10 February 2012 - 12:50 PM

Can you please edit your post to put your code inside the code tags.
Was This Post Helpful? 0
  • +
  • -

#3 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Count the number of pos/neg integers, but doesn't include the 1st

Posted 10 February 2012 - 12:51 PM

Please edit your post and :code:
Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Count the number of pos/neg integers, but doesn't include the 1st

Posted 10 February 2012 - 12:52 PM

Please remember to use :code:

Now, is it only not counting the first one?

Solution - Create data variable outside of the while loop like you have, then change your loop to a do...while loop. Add the prompt and then read it in and perform the calculations. Have the while part the conditional of (data !=0) and it will loop nicely like you would like until they enter 0.
Was This Post Helpful? 0
  • +
  • -

#5 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Count the number of pos/neg integers, but doesn't include the 1st

Posted 10 February 2012 - 12:54 PM

The reason your program skips the pos/neg count, is because you read a new piece of data in before you process the count.

What you did
System.out.println("Enter an integer value, the program exits if it is 0.");
data=input.nextInt();
count++;
if (data>0)
	totpos++;
if (data<0)
        totneg++;


What you should do
count++;
if (data>0)
	totpos++;
if (data<0)
        totneg++;
System.out.println("Enter an integer value, the program exits if it is 0.");
data=input.nextInt();

Was This Post Helpful? 1
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Count the number of pos/neg integers, but doesn't include the 1st

Posted 10 February 2012 - 01:00 PM

The problem here is that you set counters to 0 while user has entered the first value:
int count =0;
    int totpos = 0;
    int totneg = 0;

For this problem it is simple to use do while() and also it reduce redundant of code. Or set data != 0 and then enter loop and start asking for the input
Was This Post Helpful? 0
  • +
  • -

#7 computer_sorry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 10-February 12

Re: Count the number of pos/neg integers, but doesn't include the 1st

Posted 10 February 2012 - 01:02 PM

Thanks so much! It's been fixed! Now, all I worry is for the average output, is it supposed to have a [-]negative sign in front of it? Is there a way it can just be the positive average?

:bananaman:

View PostSwiftStriker00, on 10 February 2012 - 12:54 PM, said:

The reason your program skips the pos/neg count, is because you read a new piece of data in before you process the count.

What you did
System.out.println("Enter an integer value, the program exits if it is 0.");
data=input.nextInt();
count++;
if (data>0)
	totpos++;
if (data<0)
        totneg++;


What you should do
count++;
if (data>0)
	totpos++;
if (data<0)
        totneg++;
System.out.println("Enter an integer value, the program exits if it is 0.");
data=input.nextInt();

Was This Post Helpful? 0
  • +
  • -

#8 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Count the number of pos/neg integers, but doesn't include the 1st

Posted 10 February 2012 - 01:08 PM

If your input is negative, sure why not. However your not properly calculating the average.

Remember data is your current input variable, not the total count. (This is why you should rename the variable to inputNum). You should be doing sum divided by total count of numbers
Was This Post Helpful? 0
  • +
  • -

#9 computer_sorry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 10-February 12

Re: Count the number of pos/neg integers, but doesn't include the 1st

Posted 10 February 2012 - 01:33 PM

Ok, well I changed
 sum += data;
        average = data/sum; 
Idk what other variables would be beneficial to me, because if I add another one, say inputNum, then I have to assign it=0, and the whole code doesn't mess up. Besides, the names don't matter, correct? I get what you're saying, but if you could speak code..

View PostSwiftStriker00, on 10 February 2012 - 01:08 PM, said:

If your input is negative, sure why not. However your not properly calculating the average.

Remember data is your current input variable, not the total count. (This is why you should rename the variable to inputNum). You should be doing sum divided by total count of numbers

Was This Post Helpful? 0
  • +
  • -

#10 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Count the number of pos/neg integers, but doesn't include the 1st

Posted 10 February 2012 - 01:58 PM

Do you even know the code you typed?

you have a variable count that is incremented every time you add a new number. Try using that to find the average

This post has been edited by SwiftStriker00: 10 February 2012 - 02:09 PM

Was This Post Helpful? 0
  • +
  • -

#11 computer_sorry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 10-February 12

Re: Count the number of pos/neg integers, but doesn't include the 1st

Posted 10 February 2012 - 02:16 PM

Okay I changed
 sum += data;
        average = sum/count; 

and I also tried
 sum += data;
        average = data/count; 

Neither of them work and I
and get this:
Exception in thread "main" java.lang.ArithmeticException: / by zero

View PostSwiftStriker00, on 10 February 2012 - 01:58 PM, said:

Do you even know the code you typed?

you have a variable count that is incremented every time you add a new number. Try using that to find the average

Was This Post Helpful? 0
  • +
  • -

#12 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Count the number of pos/neg integers, but doesn't include the 1st

Posted 10 February 2012 - 02:23 PM

Average is the sum of the terms over the quantity. Therefore, its sum divided by count.

calculate the average after the loop, there is no reason why you need to calculate every single time

This post has been edited by SwiftStriker00: 10 February 2012 - 02:24 PM

Was This Post Helpful? 0
  • +
  • -

#13 computer_sorry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 10-February 12

Re: Count the number of pos/neg integers, but doesn't include the 1st

Posted 10 February 2012 - 02:28 PM

Okay, I made the changes as you suggest. Now see the result output I get from the new code:
The total of the integers is0
The average of the integers is0.0

int data=input.nextInt();
    
    int sum = 0;
    int count =0;
    int totpos = 0;
    int totneg = 0;
    double average = 0;
    
    while (data!=0){
         
        sum += data; 
        count++;
        
        if (data>0)
            totpos++;
        if (data<0)
            totneg++;
        System.out.println("Enter an integer value, the program exits if it is 0.");
        data=input.nextInt();
    }
    average = sum/count;
    System.out.println("The amount of positive integers is" +totpos);
    System.out.println("The amount of negative integers is" +totneg);
    System.out.println("The total of the integers is" +sum);
    System.out.println("The average of the integers is" +average);
    }   
}
 


View PostSwiftStriker00, on 10 February 2012 - 02:23 PM, said:

Average is the sum of the terms over the quantity. Therefore, its sum divided by count.

calculate the average after the loop, there is no reason why you need to calculate every single time

Was This Post Helpful? 0
  • +
  • -

#14 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Count the number of pos/neg integers, but doesn't include the 1st

Posted 10 February 2012 - 02:34 PM

Whats your input, because it seems to me that your code is running fine
Was This Post Helpful? 0
  • +
  • -

#15 computer_sorry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 10-February 12

Re: Count the number of pos/neg integers, but doesn't include the 1st

Posted 10 February 2012 - 02:45 PM

I put 2. Trust me, it's not working.


View PostSwiftStriker00, on 10 February 2012 - 02:34 PM, said:

Whats your input, because it seems to me that your code is running fine

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2