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

  1. #46
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Wow it fixed the majority of the problem! Now I dropped 4 errors down to just one it looks like. Visual Basic agrees with Dev C++ Bloodshead.

    So apparently sometimes if you store to many variables in one function it can mess up which I suspect what promoted that error. If I am correct?

    Code:
    for (int j=0; j<numberofterms-number_of_elements_avg; j++)        {
                // j - number_of_elements_avg/2.0 
                unsigned int start = j - number_of_elements_avg/2;
                average = accumulate(temperature.begin() + start, temperature.begin() + j + number_of_elements_avg/2 + 1, 0.0) / (number_of_elements_avg + 1);
            }
    Now...the one error ONLY.

    1>------ Build started: Project: RunningMean, Configuration: Debug Win32 ------
    1> RunningMean.cpp
    1>c:\users\futurenws4caster\documents\visual studio 2010\projects\runningmean\runningmean\RunningMean. h(50): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
    1> RunningMean.vcxproj -> c:\users\futurenws4caster\documents\visual studio 2010\Projects\RunningMean\Debug\RunningMean.dll
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

  2. #47
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I would recommend that your change your "float" to "double", you will get several benefits, more precession an less round off problems. Today in my opinion the use of the float data type for desktop applications should be avoided. There are situations where use of float is useful but in most cases using doubles is much better.

    The other option would be to make sure the last parameter is a float by using 0.0f or casting it to a float. Also I noticed that you took part of the calculations out of the function call I recommend you think about finishing it up by replacing the other calculation as well.

    Jim

  3. #48
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by jimblumberg View Post
    Today in my opinion the use of the float data type for desktop applications should be avoided. There are situations where use of float is useful but in most cases using doubles is much better.
    That's new to me, thanks.
    Where (&& why) do you think, "use of float is useful" ?
    (I'm learning OpenGL now, and every text seems to use float, though.)

  4. #49
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    By the way in addition on Dev Blooshead C++ why doesnt the #include "stdafx.h" work?

  5. #50
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Interesting where can I find more info about that? Any links Jim?

  6. #51
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    That statement was too broad to resolve to a useful "rule of thumb".

    Use a type that is appropriate to the precision necessary versus the performance of the hardware with variables of that precision.

    For graphics using "OpenGL" or "Direct3D" you should usually be using single-precision even where the API offers double-precision possibilities because most hardware doesn't do anything with the extra precision while still costing a little extra time do to the conversion. Where the hardware does honor the request for double-precision calculations it is usually much slower than single-precision which is sufficient for the majority of rendering. Finally, even hardware that does support double-precision for some parts of the API don't support it the way you may think which can cause rendering problems.

    Soma

  7. #52
    Registered User
    Join Date
    Jun 2012
    Posts
    36

    Crashing Display Not going through the for loops

    Now that I got the code running. Here's what the executable is displaying. The bold is the input.

    Enter the number of terms you want:
    4
    Enter how many point average you want take:
    3
    Enter Temp input in C 1: 20

    AND THEN IT CRASHES AT THAT POINT. Why isnt it going through the for loop?


    Code:
    //Running Mean for 3 points with even weights
     
    #include <iostream>
    #include <iomanip>
    #include <vector>
    #include <numeric>
    #include <functional>
    using namespace std;
     
    int main ()
    {
        int numberofterms;
        int number_of_elements_avg; 
        float tempstorage;
        vector<float> temperature; //Define a vector of 5 floats
        vector<float> movingavg;
        int i; //Loop counter
     
        cout<<"Enter the number of terms you want: " <<endl;
        cin>>numberofterms;
        cout<<"Enter how many pt average you would like to take: "<<endl;
        cin>>number_of_elements_avg;
     
     
        for (i=0; i<numberofterms; i++)
        {  
            float average;
     
            cout<<"Enter temperature you want to input in Celsius "<<(i+1);
            cout<<": ";
            cin>>tempstorage;
            temperature.push_back(tempstorage);
     
            if (i=0)
            {
                movingavg[i]=temperature[i];
                cout<<"Temperature [1]= "<<temperature[0]<<" C"<<endl;
                continue;
            }
            else if (i=numberofterms-1)
            {
                movingavg[i]=temperature[i];
                cout<<"Temperature ["<<numberofterms<<"]= "<<temperature[numberofterms-1]<<" C"<<endl;
                continue;
            }
            for (int j=0; j<numberofterms-number_of_elements_avg; j++)
            {
                // j - number_of_elements_avg/2.0 
                unsigned int start = j - number_of_elements_avg/2;
                average = accumulate(temperature.begin() + start, temperature.begin() + j + number_of_elements_avg/2 + 1, 0.0) / (number_of_elements_avg + 1);
            }
            //NOT PART OF THE CODE average= (temperature[index-1]+temperature[index]+temperature[index+1]/3);
            //NOT PART OF THE CODE movingavg[index]=average;
            cout<<"Temperature ["<<i<<"] = "<<average<<" C"<<endl;
            cout<<"The original temperature ["<< i+1<<"]"<<tempstorage<<"] C has been replaced." <<endl;
        }
     
        system ("PAUSE");
        return 0;
    }

  8. #53
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Because you vector movingavg has no elements. You can't use assignment with an empty vector.

    Jim

  9. #54
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Quote Originally Posted by jimblumberg View Post
    Because you vector movingavg has no elements. You can't use assignment with an empty vector.

    Jim
    I thought the movinavg has elements because its dependent on the temperature on this loop part:

    Code:
    if (i=0)
    { movingavg[i]=temperature[i]; cout<<"Temperature [1]= "<<temperature[0]<<" C"<<endl; continue; } else if (i=numberofterms-1) { movingavg[i]=temperature[i]; cout<<"Temperature ["<<numberofterms<<"]= "<<temperature[numberofterms-1]<<" C"<<endl;
    continue;

  10. #55
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I thought the movinavg has elements because its dependent on the temperature on this loop part:
    That is not how vector works, as you have been told. Unless you use pointers (and I am not telling you to use them) there is no such thing as a vector dependent on another vector. If you construct two vectors you must insert elements into both properly. The [] operator is merely for accessing elements. In my opinion the two most helpful modifiers for vector are vector::push_back() for adding one element to the end, and vector::insert() for adding many elements at once starting from a certain place. You can click on the red to see the appropriate references.


    hth

  11. #56
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I thought the movinavg has elements because its dependent on the temperature on this loop part:
    As I have already said, several times... Re-read posts 5, 7, 11 and 53.....
    You can't use assignment with an empty vector.
    Jim

  12. #57
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by Rod Micael View Post
    I thought the movinavg has elements because its dependent on the temperature on this loop part:

    Code:
    if (i=0)
            {            movingavg[i]=temperature[i];
    Think post #11 said it best
    Question about a certain code with the C++ syntax

    To repeat something else Jim said - it's far far easier to figure out what's wrong if you use a debugger. I see you've built your code with both Visual Studio and GCC -- both Visual Studio and gdb would have been able to tell you the exact line with the problem.
    If you get friendly with a debugger, you'll spend a lot less time being confounded by weird logic errors. Honestly, it's worth the time.

    Couple of other comments:
    Code:
            if (i=0)
    ....
            else if (i=numberofterms-1)
    These should be "==" not "=".

    I'm a bit confused about what you want your program to do. Say the user wants to input 5 values. Do you intend them all to be input, then the moving averages are computed after input is finished? Or do you want to calculate the moving averages on the fly as the user enters the data?

    This loop looks like it's meant to calculate averages for all the data:
    Code:
    for (int j=0; j<numberofterms-number_of_elements_avg; j++)
    {
         // j - number_of_elements_avg/2.0 
         unsigned int start = j - number_of_elements_avg/2;
         average = accumulate(temperature.begin() + start, temperature.begin() + j + number_of_elements_avg/2 + 1, 0.0) / (number_of_elements_avg + 1);
    }
    but this loop is nested inside your for loop for input, so will be run every time the user enters a number. If you want to do it 'on the fly' you don't need a second loop: as soon as you have number_of_elements_avg elements you can do an accumulate using 'i' to work out the start and end. However that'd mean you'd get (if I put in 10,20,30,40,50 for 3 avg points):
    Code:
    element  value  average
    1             10       10
    2             20       20
    3             30       (10+20+30)/3
    4             40       (20+30+40)/3
    5             50       (30+40+50)/3
    and it looks like you want
    Code:
    element  value  average
    1             10       10
    2             20       (10+20+30)/3
    3             30       (20+30+40)/3
    4             40       (30+40+50)/3
    5             50       50
    In which case you do need a second loop, but you don't want it inside your input loop.

    Once you've fixed your array allocation I think you'll get another crash:
    Code:
    for (int j=0; j<numberofterms-number_of_elements_avg; j++)
            {
                // j - number_of_elements_avg/2.0 
                unsigned int start = j - number_of_elements_avg/2;
    If j=0 and number_of_elements_avg=3, start is 0 - (3/2) = 0-1 = -1. Since start is unsigned, you'll get the unsigned representation of the signed number -1, which is probably around about 4 billion. Not going to work!

    You could change the initial value of j, or change the calculation of start and end.

  13. #58
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I get the feeling that you have absolutely no idea what you're doing and is pushing all the work back onto us. Stop this!
    I sincerely utterly extremely recommend you go read up on what std::accumulate is, how it works, and how to use it. Make another project! Experiment! Get used to it! And secondly, get a bloody debugger! Your logic is utterly wrong, and everything is a mess! You complain it crashes, and yet you have specified absolutely no details on where the error is or why it occurs, implying that you have not even attempted to solve this on your own. Using a debugger, you should have at least been able to figure out where and why. You still have many embarrassingly simple errors that should easily be solved by just upping warning levels (google it!), or using a debugger.
    So once again, go get a debugger and figure out how how std::accumulate works. This is stuff you absolutely must know to program. If you get stuck, ask specific questions and we'll guide you through that, but we're not here to fix all the code for you.
    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. #59
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I also recommend that you also write a small program to experiment with vectors and try using your debugger to debug the problems you find, and if you don't find any problems, create some. The sooner you figure out your debugger the better. Without knowing how to use your debugger you will never become even a semi-accomplished programmer.

    You also need to learn to read and understand the documentation for the features you are trying to use. Today, there is a wealth of information available to aid in the understanding of the standard features. If one web site doesn't explain the feature to your satisfaction, try another, or another until you exhaust the possibilities. After you exhaust the possibilities and you come to ask questions be sure you read and understand the answers provided. If you don't understand the answer, say so. Maybe we can restate the answer in another way that you will understand. But as it is you seem to either not be understanding the answers, or not learning from your mistakes.

    Jim

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