0

:) guys im a firt timer. im making a program which reads floating point numbers from cin and compute 2 averages: the average of the positive and negative nos. im wonderin wats wrong in my program>>

#include<iostream.h>
#include<conio.h>
int size,i,sumpos,sumneg;
int num;


main()
{
clrscr();
cout<<"input size\n";
cin>> size;
for (i=1; i<=size; i++)
cin>> num;
for (i=1; i<=size;i++)
{
if (num>=0)
{sumpos=sumpos+num;
countpos++;
}
else if
{sumneg=sumneg+num
countneg++;
sumpos=sumpos/countpos; cout<< ave.pos;
aveneg=sumneg/countneg; cout<<aver.neg;
]
return 0;

Edited by happygeek: fixed formatting

3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by aeroll
0
#include <iostream>
using namespace std;
int main()
{
   int size;
   double num[100];
   double sumpos = 0;
   double sumneg = 0;
   int counterPos = 0;
   int counterNeg = 0;
   cout << "input size:";
   cin >> size;
   for ( int i = 1; i <= size; i++ )
      cin >> num[i];
   for ( int i = 1; i <= size; i++ )
   {
      if ( num[i] >= 0 )
      {
         sumpos = sumpos + num[i];
         counterPos++;
      }
      else
      {
         sumneg = sumneg + num[i];
         counterNeg++;
      }
   }
   cout << " ave pos " << sumpos / counterPos << endl;
   cout << " ave neg " << sumneg / counterNeg << endl;
   cin.get();
   cin.get();
   return 0;
}
0

Look at your array declaration.(int num, what that means???You must initialize the array with its size) Ensure that the "size" variable should less or equal than the array size and start your loop 0 < size instead 1 <= size so when the user enters the array's size for size variable you will be able to deposit all of them in the array...(And you will guarantee that the user can't get over the array's size because the array's last members's index is array not array)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.