Thread: my professior gave us an example program for a fiter.. but I can't get it to work...

  1. #1
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55

    my professior gave us an example program for a fiter.. but I can't get it to work...

    my professor gave us an example program for a filter.. but I can't get it to work...
    This is my 1st time using C++ and before we where learning C... so I can't easily see why it doesn't work.... this is an example and shouldn't have errors but ya.. it does so can anyone help me? thanks
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int i, numpts;
        double input[25]; //store values for filtering
        cout << "enter number of points: ";
        cin >> numpts;
        for (int i = 0; i < numpts; i++)
        {
            cout << "Enter a point: ";
            cin >> input[i];
        }
        double output[25]; //store filtered results
        double alpha = .5;// 0 < alpha 1
        output[0] = input[0];
        for (int i = 1; i < numpts; i++)
            output[i] = alpha*input[i]+(1-alpha)*output[i-1];
        for (int i = 0; i < numpts; i++)
        {
            cout << "Output " <<i << " is " << output[i];
            cout << "\n";
        }
        return 0;
    }
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    .5 needs to be 0.5, but other than that, what errors does the compiler give?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    Code:
    >ch -u "simpleFilt.cpp"    
    ERROR: maybe missing ';' before '='
    ERROR: with unnecessary identifier 'i' in casting operator
    ERROR: incompatible types for casting operation, casting void to int
    ERROR: missing ';' in for-loop
    ERROR: syntax error before or at line 12 in file 'simpleFilt.cpp'
      ==>:     for (int i = 0; i < numpts; i++)
      BUG:     for (int i = 0; i <<== ???
    ERROR: multiple operators together 
    ERROR: syntax error before or at line 12 in file 'simpleFilt.cpp'
      ==>:     for (int i = 0; i < numpts; i++)
      BUG:     for (int i = 0; i <<== ???
    ERROR: multiple operators together 
    ERROR: syntax error before or at line 12 in file 'simpleFilt.cpp'
      ==>:     for (int i = 0; i < numpts; i++)
      BUG:     for (int i = 0; i <<== ???
    ERROR: multiple operators together 
    ERROR: syntax error before or at line 12 in file 'simpleFilt.cpp'
      ==>:     for (int i = 0; i < numpts; i++)
      BUG:     for (int i = 0; i <<== ???
    ERROR: multiple operators together 
    ERROR: syntax error before or at line 12 in file 'simpleFilt.cpp'
      ==>:     for (int i = 0; i < numpts; i++)
      BUG:     for (int i = 0; i <<== ???
    ERROR: multiple operators together 
    ERROR: syntax error before or at line 12 in file 'simpleFilt.cpp'
      ==>:     for (int i = 0; i < numpts; i++)
      BUG:     for (int i = 0; i <<== ???
    ERROR: multiple operators together 
    ERROR: syntax error before or at line 12 in file 'simpleFilt.cpp'
      ==>:     for (int i = 0; i < numpts; i++)
      BUG:     for (int i = 0; i <<== ???
    ERROR: multiple operators together 
    ERROR: syntax error before or at line 12 in file 'simpleFilt.cpp'
      ==>:     for (int i = 0; i < numpts; i++)
      BUG:     for (int i = 0; i <<== ???
    ERROR: multiple operators together 
    ERROR: syntax error before or at line 12 in file 'simpleFilt.cpp'
      ==>:     for (int i = 0; i < numpts; i++)
      BUG:     for (int i = 0; i <<== ???
    ERROR: multiple operators together 
    ERROR: syntax error before or at line 12 in file 'simpleFilt.cpp'
      ==>:     for (int i = 0; i < numpts; i++)
      BUG:     for (int i = 0; i <<== ???
    ERROR: maybe missing '}' at the end of program at line 12 or there is an extra '{' in other place in file 'simpleFilt.cpp' 
    ERROR: cannot execute command 'simpleFilt.cpp'
    >Exit code: -1

    its really weird... cuz I tried it in a different compiler and it work so I dunno...
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    for (int i = 0; i < numpts; i++)
    You can start by trying to get rid of those.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Code:
    //Here is the original code with comments.
    //No changes to the code, as to help you better understand the syntax
    /* Even if you manage to compile this code sample, your program will crash
       as soon as any invalid user input is entered.*/
    
    //Header files - All the stuff your program will use.
    #include <iostream>
    
    //Name spaces for api's being used. Declaired in line 7.
    using namespace std;
    
    //Main fuction. This is where your program starts. 
    // main is better as // int main(int argc, char* argv);
    int main()
    {
        int    i,              //Iterator for User Input
               numpts;         //Number of values the user will input
        double input[25];      //store values for filtering : values entered by the user
    
        cout << "enter number of points: "; //Display request to user
        cin >> numpts;                      //Get User Input: array size
        
        //User input loop
        for (int i = 0; i < numpts; i++) /* C++ allows you to create a value anytime you want, 
                                            Error 1: Please look at line 24 and 16 and tell me what you see 
                                            Error 2: numpts (line 17) is used on line 21 and again on 24 
                                            i'll help show you the problem when you start getting the basics. */
        {
            cout << "Enter a point: "; //output to user
            cin >> input[i];           //input from user
        }
    
        /*The syntax of c++ allows you to declare a value any
            time you want. But does have limits.
        */
        double output[25]; //store filtered results
        double alpha = .5;// 0 < alpha 1
        output[0] = input[0]; /* Store the first element of the user input array(line 18)
                                     into the first element of the output array(line36)*/
                                     
        /* This loop does some math on the users input then stores the results as output. */
        for (int i = 1; i < numpts; i++)                        //Please see note on line 25
            output[i] = alpha*input[i]+(1-alpha)*output[i-1];   /*Simple math. alpha*x+(1-alpha)*y
                                                                    'x' is the user input in the order given by the user
                                                                    'y' is the last output of the loop*/
                                                                    
        for (int i = 0; i < numpts; i++)    //Please see not on line 42
        {
            //Display results of the loop chain to user
            cout << "Output " <<i << " is " << output[i]; // C++ allows for you to easily compine multiple function calls.        
                 /*  Output 0 is 12.345 */ //Example output.
            
            cout << "\n"; //output a newline to the screen. Please see note on line 50
        }
        return 0; //Program is finished, exit main();
    }
    As you fix the logic problems your compiler has flaged as a "bug" you should be able to compile
    and recive more detailed error messages about the problem.
    As well as fix the possible buffer overrun error in the code.
    Last edited by Nor; 10-15-2011 at 06:42 PM. Reason: incomplete

  6. #6
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    thanks, this is very helpful! I don't understand all of it yet, but I hope to learn to soon. thanks
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  7. #7
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    ok I'm tryin to practice before doing homework...cuz I still don't get it and I was trying to do a simple program but I can't seem to fix it....
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        double data[11];
        int i;
        cout << "How many data ponits 1 to 10?" << endl;
        cin >> i ;
            if (i < 1 | i > 10)
                {
                    cout << "worng amount of data points please try again" << endl;
                    return 0;
                }
    
            if (i=0;1<i<10;i++)
                {
                    cout << "Enter data ponit:"<<endl;
                    cin >> data[i];
                }
        return 0;
    it says error line 17 ... why?
    thanks for any help!
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  8. #8
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Code:
            if (i=0;1<i<10;i++)
    you have used an if statement where it looks like you wanted a for statement
    Code:
    for( i=0; 1 < i < 10; i++)
    Also on line 11. '|' and '||' are two different things.
    you need '||' there, the first you have is a bitwise operator.
    Last edited by Nor; 10-16-2011 at 04:50 PM.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  9. #9
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    thank you again, whats a bit wise operator?
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  10. #10
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  11. #11
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Code:
    for(i=0;1<i<10;i++)
    Lets read this statement in english

    FOR this loop, 'i' starts at zero;
    loop untill the next statement is false. One is less than 'i'. And 'i' is less than ten.
    Increment 'i'.
    Loop;

    When the loop start will your statement 1 < i < 10 be true or false?
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  12. #12
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by King Mir View Post
    .5 needs to be 0.5
    Picky me: No, 0.5 can also be written as ".5", just as 1.0 can be written as "1.". It just that the full forms are more readable. So corrected, the sentence would be, ".5 should be 0.5".

    Ok, I'm done.
    Devoted my life to programming...

  13. #13
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by Cess View Post
    Code:
    if (i=0;1<i<10;i++)
    As Nor pointed out that "if" should be "for". However there is another problem here. Notation in maths often gets sloppy, and a < x < b meaning x is between a and b is a good example of this.

    When you give 1 < i < 10 to a C++ compiler, it is the same as:

    Code:
    (1 < i) < 10
    So 1 < i is evaluated to a bool (true/false) and then that bool is tested on whether it is less than 10, which is always true, because true would be converted to 1 and false would be converted to 0, and both are less than 10. In other words, this is very bad because 1 < i < 10 is always true, even if i is 100.

  14. #14
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    ok another program I was working on......
    Code:
    #include <iostream>
    
    /*  Write a program that will calculate the average of n data points which are entered by the
    user and print out the average and the data points with the average subtracted. */
    
    using namespace std;
    
      int main()
    {
        int sum, i, datapts;
        double points [51];
        double mean;
        cout << "How numbers do you want to average? " << endl;
        cout << "(please enter more values then 2 but less then 50)" << endl;
        cin >> datapts;
                for (int i = 0; i < datapts; i++)
               {
                cout << "Enter number to average?" << endl;
                cin >> points[i];
               }
               if (datapts <= 1 || datapts >= 50)
                {
                cout << "Please enter correct amount to average between 2 to 50" << endl;
                return 0;
                }
        sum = sum + points [i];
        mean = sum / datapts;
        cout << "The average of your points are " << mean << endl;
    
        return 0;
    }

    I can't get it to work right. the program won't end if the amount of points is less the 1 or greater then 50 and the average is never right.

    any help would be great
    thanks
    Last edited by Cess; 10-19-2011 at 06:37 PM.
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
               if (datapts <= 1 || datapts >= 50)
                {
                cout << "Please enter correct amount to average between 2 to 50" << endl;
                return 0;
                }
    Did you intend to check for incorrect amount of data points before trying to sample the data points?

    Code:
        sum = sum + points [i];
        mean = sum / datapts;
    Now, what the heck do you think this part of your code does?

    Also, learn to indent properly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my teacher gave it to us....
    By jacek in forum C Programming
    Replies: 3
    Last Post: 01-11-2010, 12:17 PM
  2. Othello game, gave up debugging
    By Nutshell in forum Game Programming
    Replies: 31
    Last Post: 01-26-2003, 01:05 PM
  3. gave it shot...it gave me an error!
    By LonelyPlanderWa in forum C Programming
    Replies: 3
    Last Post: 07-12-2002, 01:26 AM
  4. Please Heelp me ... I gave up
    By NANO in forum C++ Programming
    Replies: 14
    Last Post: 04-21-2002, 08:14 PM
  5. I am confused with this code that somebody gave me
    By face_master in forum C++ Programming
    Replies: 14
    Last Post: 11-16-2001, 01:43 AM