:) 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;

Dani AI

Generated

Quick diagnosis and a compact, safe fix.

The original post by shows several common problems: floating-point input stored in int (loss of fractional part), sums and counters left uninitialized (undefined behaviour), input-reading and processing in separate broken loops, 1-based indexing and no bounds checks, missing semicolons/braces, and use of non‑portable conio.h/clrscr(). Also, dividing by a counter that might be zero will crash or give nonsense.

Checklist to make the program correct and robust

  • Store numbers in double (or float) and keep sums as double.
  • Initialize sums and counters to 0 before use.
  • Read and process each value in a single loop (no need to store all values).
  • Use 0-based loops or std::vector for dynamic size; always validate size and check bounds.
  • Before computing an average, check that the corresponding counter is > 0 to avoid division by zero.
  • Prefer standard headers (<iostream>, <iomanip>) and int main() returning 0; avoid conio.h.

Notes on earlier replies

  • helped by switching to double and adding counters; however their code used 1-based indexing and a fixed num[100] buffer, which can still lead to out-of-bounds access if size > 100.
  • correctly warned about indexing starting at 0 and array sizing.

Minimal example (process while reading)

#include <iostream>
#include <iomanip>

int main() {
    int n;
    if (!(std::cin >> n) || n <= 0) return 0;
    double sumPos = 0.0, sumNeg = 0.0;
    int cntPos = 0, cntNeg = 0;
    for (int i = 0; i < n; ++i) {
        double x; if (!(std::cin >> x)) break;
        if (x >= 0.0) { sumPos += x; ++cntPos; }
        else { sumNeg += x; ++cntNeg; }
    }
    if (cntPos) std::cout << "Ave pos: " << std::fixed << std::setprecision(2) << sumPos / cntPos << "\n";
    else std::cout << "No positive numbers\n";
    if (cntNeg) std::cout << "Ave neg: " << sumNeg / cntNeg << "\n";
    else std::cout << "No negative numbers\n";
    return 0;
}

Clarifying note: decide whether zero counts as positive (>= 0) or should be excluded (> 0) and use the comparison that matches the intended definition.

Recommended Answers

All 4 Replies

Member Avatar for Member #46692

what errors do you get?

Member Avatar for Member #46692
#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.