Thread: Question about a certain code with the C++ syntax

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    36

    Question about a certain code with the C++ syntax

    I was wondering if it is possible to do a sum of vectors with a range.

    The bold is where I am concerned about the syntax. Thanks.

    Code:
    //Running Mean for 3 points with even weights
    
    #include <iostream>
    #include <iomanip>
    #include <vector>
    #include <cmath>
    using namespace std;
    
    
    
    
    int main ()
    
    
    {
    float numberofterms; 
    cout<<"Enter the number of terms you want: " <<endl;
    cin>>numberofterms;   
    vector<float> temperature(numberofterms); //Define a vector of 5 floats
    int vectorlength;
    vector<float> movingavg;
    
    
    int index; //Loop counter
    float average;
    
    
    for (index==0; index<numberofterms; index++)
    {   
        cout<<"Enter temperature you want to input in Celsius"<<(index+1);
        cout<<": ";
        cin>>temperature[index];
         
         if (index==0)
         { movingavg[index]=temperature[index];
           cout<<"Temperature [1]= "<<temperature[0]<<" C"<<endl;
           continue;
         }
         else if (index==numberofterms-1)
         {
         movingavg[index]=temperature[index];
         cout<<"Temperature ["<<numberofterms<<"]= "<<temperature[numberofterms-1]<<" C"<<endl;
         continue;
         }
          average= sum((temperature[index-1:index+1]/3));
        movingavg[index]=average;
        cout<<"Temperature ["<<index+1<<"] = "<<average<<" C"<<endl;
        cout<<"The original temperature ["<< index+1<<"]"<<temperature[index]<<"] C has been replaced." <<endl;
    }
    
    
    
    
    
    
    system ("PAUSE");
    
    
    return 0;
    
    
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> average= sum((temperature[index-1:index+1]/3));

    Couple things about this. vectors do not have python-like slicing, but you can call a function called accumulate(). Also it is mathmatically important to sum before you divide.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Oh! Can this work then?

    sum = accumulate(temperature.begin(),temperature.end());
    average=sum/3;

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you read the help page, it says accumulate() has to have a third argument that specifies where to start adding from. It also specifies the type of the return value, so you want it to be float. Other than that, it's ok.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You should probably use the size of your vector instead of the magic number 3 when doing your average. You also can't use array notation [] to insert elements into an empty vector, you need to either construct movingavg with a size, like you did with temperature, or use push_back() to insert a new element.

    Jim

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Quote Originally Posted by jimblumberg View Post
    You should probably use the size of your vector instead of the magic number 3 when doing your average. You also can't use array notation [] to insert elements into an empty vector, you need to either construct movingavg with a size, like you did with temperature, or use push_back() to insert a new element.

    Jim
    Confused on what your saying Jim. Getting an error. The user inputs the number but the cmd prompt exits. Instead of doing the rest of the program. What's wrong?


    Code:
    //Running Mean for many points with even weights
    
    
    #include <iostream>
    #include <iomanip>
    #include <vector>
    #include <cmath>
    using namespace std;
    
    
    
    
    int main ()
    
    
    {
    float numberofterms; 
    cout<<"Enter the number of terms you want: " <<endl;
    cin>>numberofterms;   
    vector<float> temperature(numberofterms); //Define a vector of 5 floats
    int vectorlength;
    vector<float> movingavg;
    
    
    int index; //Loop counter
    float average;
    
    
    for (index==0; index<numberofterms; index++)
    {   
        cout<<"Enter temperature you want to input in Celsius"<<(index+1);
        cout<<": ";
        cin>>temperature[index];
         
         if (index==0)
         { movingavg[index]=temperature[index];
           cout<<"Temperature [1]= "<<temperature[0]<<" C"<<endl;
           continue;
         }
         else if (index==numberofterms-1)
         {
         movingavg[index]=temperature[index];
         cout<<"Temperature ["<<numberofterms<<"]= "<<temperature[numberofterms-1]<<" C"<<endl;
         continue;
         }
          average= ((temperature[index-1+index+index+1]/temperature.size()));
        movingavg[index]=average;
        cout<<"Temperature ["<<index+1<<"] = "<<average<<" C"<<endl;
        cout<<"The original temperature ["<< index+1<<"]"<<temperature[index]<<"] C has been replaced." <<endl;
    }
    
    
    
    
    
    
    system ("PAUSE");
    
    
    return 0;
    
    
    }

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Part of your problem is you are trying to use your vector movingavg that contains no elements. You can't use the operator= to insert elements into a vector.

    Getting an error.
    And your error is???

    Jim

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    @JIM: There's no error now. It just says a warning. It's says enter the number of terms of you want. And the executable program closes for some reason.




    //Running Mean for many points with even weights#include <iostream>#include <iomanip>#include <vector>#include <cmath>using namespace std;int main (){float numberofterms; cout<<"Enter the number of terms you want: " <<endl;cin>>numberofterms; vector<float> temperature(numberofterms); //Define a vector of 5 floatsint vectorlength;vector<float> movingavg;int index; //Loop counterfloat average;for (index==0; index<numberofterms; index++){ cout<<"Enter temperature you want to input in Celsius"<<(index+1); cout<<": "; cin>>temperature[index]; if (index==0) { movingavg[index]=temperature[index]; cout<<"Temperature [1]= "<<temperature[0]<<" C"<<endl; continue; } else if (index==numberofterms-1) { movingavg[index]=temperature[index]; cout<<"Temperature ["<<numberofterms<<"]= "<<temperature[numberofterms-1]<<" C"<<endl; continue; } average= ((temperature[index-1+index+index+1]/temperature.size())); movingavg[index]=average; cout<<"Temperature ["<<index+1<<"] = "<<average<<" C"<<endl; cout<<"The original temperature ["<< index+1<<"]"<<temperature[index]<<"] C has been replaced." <<endl;}The Bold are wrong?system ("PAUSE");return 0;}

    Last edited by Rod Micael; 06-20-2012 at 05:27 PM.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Have you tried running this program through your debugger? If your program is crashing the debugger will tell you exactly where it detected the problem. Then you can view the values of your variables at the time of the crash to help isolate the problem.

    However, unless you fixed the problem with your movingavg vector, this is where you need to start.

    Jim

  10. #10
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Is there any examples I can look at that can help me fix the problem?

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Yes, look at the difference between:
    Code:
    vector<float> temperature(numberofterms); //Define a vector of 5 floats
    vector<float> movingavg;
    Your temperature vector has numberofterms elements. Your movingavg vector has no elements. Before you can use a vector element it must exist. Therefore in the following:
    Code:
       temperature[0] = 100.0;  // This will work.
       movingavg[0] = 100.0;   // This will not work.
    Your vector element must exist before you can access it with the array notation. There are three ways to add elements to your vector, when you construct the vector use the size option, (like you did with temperature), the resize() member function, or use the push_back() member function. Since you know how many elements you want your vector to contain I recommend the first option.

    Jim

  12. #12
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    oh ok! So here's what I did in the beginning...

    vector<float>movingavg(numberofterms);


    Would this resolve the problem?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It solves a problem.
    But what is this supposed to do:

    average= ((temperature[index-1+index+index+1]/temperature.size()));

    Also, please do indent your code properly. It's hard to read.
    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.

  14. #14
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    so here's what I have so far:

    Code:
    
    //Running Mean for 3 points with even weights
    
    
    #include <iostream>
    #include <iomanip>
    #include <vector>
    #include <cmath>
    using namespace std;
    
    
    
    
    int main ()
    
    
    {
    float numberofterms; 
    cout<<"Enter the number of terms you want: " <<endl;
    cin>>numberofterms;   
    vector<float> temperature(numberofterms); //Define a vector of 5 floats
    int vectorlength;
    vector<float> movingavg(numberofterms);
    
    
    int index; //Loop counter
    float average;
    
    
    for (index==0; index<numberofterms; index++)
    {   
        cout<<"Enter temperature you want to input in Celsius"<<(index+1);
        cout<<": ";
        cin>>temperature[index];
         
         if (index==0)
         { movingavg[index]=temperature[index];
           cout<<"Temperature [1]= "<<temperature[0]<<" C"<<endl;
           continue;
         }
         else if (index==numberofterms-1)
         {
         movingavg[index]=temperature[index];
         cout<<"Temperature ["<<numberofterms<<"]= "<<temperature[numberofterms-1]<<" C"<<endl;
         continue;
         }
          average= ((temperature[index-1+index+index+1]/temperature.size()));
        movingavg[index]=average;
        cout<<"Temperature ["<<index+1<<"] = "<<average<<" C"<<endl;
        cout<<"The original temperature ["<< index+1<<"]"<<temperature[index]<<"] C has been replaced." <<endl;
    }
    
    
    
    
    
    
    system ("PAUSE");
    
    
    return 0;
    
    
    }
    Is this just part of the problem or will the inside code need to be changed as well?

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    oh ok! So here's what I did in the beginning...

    vector<float>movingavg(numberofterms);


    Would this resolve the problem?
    Yes, but only if your movingavg vector will only have numberofterms or fewer elements. What are you going to use this vector for? How many elements do you desire to have for your moving average?

    Jim
    Last edited by jimblumberg; 06-21-2012 at 08:29 AM. Reason: Added quote

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 20
    Last Post: 12-09-2011, 03:28 PM
  2. Syntax Adjustment in C code block
    By Dr Diablo in forum C Programming
    Replies: 5
    Last Post: 11-26-2008, 05:16 AM
  3. gcc asm code syntax
    By Uberapa in forum C Programming
    Replies: 4
    Last Post: 06-15-2007, 01:16 AM
  4. code syntax help
    By 182 in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2006, 12:57 PM
  5. What is the syntax for a #define for several lines of code?
    By Jonas_Valleskog in forum C Programming
    Replies: 7
    Last Post: 01-31-2003, 12:22 PM