:) 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;
Member Avatar for iamthwee

what errors do you get?

Member Avatar for iamthwee
#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;
}

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)

tnanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.