0

I'm having trouble compiling some code for a class....
this is where i'm having trouble

 aavg=0; int count=0;
    for(int i=0; i<num;i++)
    {
    if ((math.abs(a[i])<25.0)
    {
 count++;
 aavg+=a[i];                            
     }
     } 
 aavg=aavg/count;
 cout << "The average a value in range, absolute value of a is less than 25.0 is: "<< aavg <<endl;

This is my whole program

 #include <iostream>
    #include <fstream>
    #include <math.h>
    #include <cmath>
    using namespace std;
    int main (void)
    {
        ifstream input ("measurements");
        int num=0;
        input >> num;
        float a[num];
        float b[num];
        float c[num];
        for(int i=0; i<num;i++)
        input >> a[i] >> b[i] >> c[i];
        //All DATA IN
        //Do A AVERAGE
        float aavg =0;
        for(int i=0; i<num;i++)
        aavg+=a[i]; 
        aavg=aavg/num;
        cout << "A average: " << aavg <<endl;
        //DO SMALLEST B
        float smallb=b[0];
        for(int i=1; i<num; i++)
        if(smallb>b[i])
        smallb=b[i];
        cout <<"Smallest b: " <<smallb <<endl;
        //PRINT ALL GISMO NUMBERS WHERE "a+c<60.0
            for(int i=0; i<num;i++)
            if((a[i]+c[i])<60.0)
         cout <<"Gismo number " <<i <<" has a and c values that total less than 60" <<endl;
        //PRINT SMALLEST C VALUE BETWEEN 25.0 AND 50.0
          float smallc=51;
          for(int i=0; i<num;i++)  
          if((25.0<c[i])&&(c[i]<50))
          if(smallc>c[i])
          smallc=c[i];
          if(smallc>50)
          cout <<"No values in range" <<endl;
          else
          cout <<"Smallest c in range was: "<<smallc <<endl;
          //LAST PART! woot!
          aavg=0; int count=0;
        for(int i=0; i<num;i++)
        {
        if ((math.abs(a[i])<25.0)
        {
     count++;
     aavg+=a[i];                            
         }
         } 
     aavg=aavg/count;
     cout << "The average a value in range, absolute value of a is less than 25.0 is: "<< aavg <<endl;
     //system("PAUSE");
    }
David
  • 1

2 Answers2

4
math.abs

math is not an object, it's the name of the library.

The function is just std::abs (if you include <cmath>) or abs (if you include <math.h>). You only have to include one of the two headers.


Your program has several other problems as well. The size of an array must be a compile-time constant in C++, so your declarations of a, b, and c are invalid. The easiest way to do what you are trying to do is to use a std::vector<float>. std::vector is a container that can be resized at runtime.

While your program may be okay as a learning exercise, you should always check any input operation to be sure that it succeeded. If the first thing in your "measurements" file is Hello, the first extraction will fail. num will remain 0, your program won't read any further input, and you will end up dividing by zero (aavg=aavg/num;). You can find out more about the stream error flags and how to correctly check the result of an input operation in the answers to another question.

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Thank you very much. That got it for me. I feel slightly silly. (i'm learning c from another language....and that's how you did abs. – David Mar 31 '11 at 03:47
0

You have two problems. The one you highlight is the expression:

math.abs(a[i])

All you want is:

fabs(a[i])

The other is using the non-const num as an array size. This is not legal. You need dynamically allocated arrays:

float* a = new float[num];
Keith
  • 6,756
  • 19
  • 23