Thread: need help with program please

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    need help with program please

    This is a lab that i had last week but, i still can't figure it out. Problem: Allow the user to enter positive and negative integers and end the sequence with input of 0. Output the averages of positive, negative, and total integers. If no positive or negative integers are entered then display no positive or negative integers where entered.

    ex. Enter a sequence of integers, hit 0 key to stop: 4 4 4 0
    The positive average is: 4
    There were no negative integers entered
    The total average is: 2

    My problem is that I can't figure out how to display that there were no positive or negative integers entered. I'm pretty sure that it has something to do with an if statement. Any help would be greatly appreciated. Here's as far I have gotten with my code:
    Code:
    int main()
    {
    	
    	int n = 0; // user input
    	int pSum = 0, nSum = 0, pcount=0, ncount=0;
    
    	double pAverage = 0, nAverage = 0, totalAverage = 0; // positive and negative averages
    
    	cout << "Enter a sequence of integers, hit 0 key to stop: " ;
    	cin >> n; // user input
    while ( n != 0 )
    	{
    		if( n > 0 ) // if input is positive
    			pAverage = double( pSum += n) / ++pcount; // operation for average
    		if( n < 0 ) // if input is negative
    			nAverage = double( nSum += n) / ++ncount;
    		
    		cin >> n;
           
    }
    		totalAverage = double(pAverage + nAverage) / 2;
    
    		
    
    		if ( n == 0 ) // end sequence
    		{
    			
    
    			cout << "The positive average is: " << pAverage << endl;
    			cout << "The negative average is: " << nAverage << endl;
    			cout << "The total average is: " << totalAverage << endl;
    			
    		
    		}
    			
    		else 
    		{
    			cout << "Invalid integers entered" << endl;// invlaid entry
    		}
    
    
    		
    	return 0;
    	}
    Last edited by dgjenkin; 10-12-2006 at 11:29 PM. Reason: attachment file

  2. #2
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    How is the total average 2 in your above example (4 4 4)?

    Anyway, you appear to keep track of positive and negative amounts seperately, so just test if it == 0.

    If nSum == 0 then no negative numbers have been entered, if pSum == 0 no positive.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    74
    Yeah, as the above poster mentioned you are keeping a running total of positive # instances and negative # instances so testing if they equal zero would work:

    Code:
    if ( n == 0 ) // end sequence
    {
      if(pcount == 0)
         cout << "No positive integers present" << endl;
      else  
         cout << "The positive average is: " << pAverage << endl;
    
      if(ncount == 0)
         cout << "No negative integers present" << endl;
      else  
         cout << "The negative average is: " << nAverage << endl;
    
      cout << "The total average is: " << totalAverage << endl;
    }

    How is the total average 2 in your above example (4 4 4)?
    Yeah, I'm wondering the same thing... shouldn't the result be 4? Err.. maybe we both misreading the problem or something.

    Code:
    totalAverage = double(pSum + nSum) / (ncount + pcount);
    Last edited by Kurisu33; 10-13-2006 at 12:56 AM.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    34
    Code:
    you can make an int array like
    
    int neg[256];
    int pos[256]
    
    when its a negative.
    affter parsing the negative number do this.
    
    neg[ncount]=n;
    
    when its a positive.
    after parsing the positive number do this.
    
    pos[pcount]=n;
    
    to display shot out a for
    
    for(int i=0;i<ncount;i++){
    cout<<neg[ncount]<<endl;
    }
    for(int i=0;i<pcount;i++){
    cout<<pos[pcount]<<endl;
    }
    //side note i saw the same thing it shouldnt be 2 it should be 4
    Last edited by unkownname; 10-13-2006 at 01:01 AM.
    Its Really All up to you.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    maybe the total average should be 2 because half way between 0 and 4 is 2
    0,1,(2),3,4


    i dont know if thats what he ment it might be as he said the total average of the neg and pos numbers?

    so if his input was 1,2,3,4,-6,-5,-4,-3,-2,-1,0
    then his average would be

    -6,-5,-4,-3,-2,(-1),0,1,2,3,4


    so the total average would be -1 pos ave 2.5 neg ave -3.5
    i have no idea how u would do it if it was ment like this other than
    psum/2
    nsum/2

    ok i just read a bit of your code and this will probly be nothing like what you mean
    Last edited by thestien; 10-13-2006 at 01:43 AM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2
    Thanks guys for all your help, yeah i think the totalAverage was suppose to be 4. But, i think Kurisu33 had what i was looking for. thanks man!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM