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

  1. #16
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Quote Originally Posted by Elysia View Post
    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.

    Sorry Elysia. I am trying to take the average of the point and the 2 surrounding points at that point and divide by 3. If that makes sense.

  2. #17
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    @Jim: Well I want to make it open for a variable amount of elements. Because later on I will have to use File I/O.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then,
    average = (temperature[index - 1] + temperature[index] + temperature[index + 1] / 3);
    Make sure you start from index = 1.
    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.

  4. #19
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Quote Originally Posted by Elysia View Post
    Then,
    average = (temperature[index - 1] + temperature[index] + temperature[index + 1] / 3);
    Make sure you start from index = 1.
    Out of curiosity is there an easy way to do a range per say like in MATLAB you can do it because if there 20 points per say you dont want to write out the long way of doing it.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To answer your question: no. There is no such short-hand syntax in C++. You have to use a loop.

    If you want to take the average of N elements, then:
    average = std::accumulate(temperature.begin() + index, temperature.begin() + index + N, 0) / N;
    Loop from 0 to M - N (where M is number of elements and N number of elements you want to average)

    Or

    average = std::accumulate(temperature.begin() + index - N/2, temperature.begin() + index + N/2, 0) / N;
    Loop from N/2 to M - N/2 (where M is number of elements and N number of elements you want to average - 1 [ie if you want to average 3 elements, N = 2])

    Disclaimer: This is not tested code.
    Last edited by Elysia; 06-21-2012 at 08:43 AM.
    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.

  6. #21
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Quote Originally Posted by Rod Micael View Post
    @Jim: Well I want to make it open for a variable amount of elements. Because later on I will have to use File I/O.
    First I suggest you review how a moving average, or Boxcar Averaging actually works. Second if you are going to have an unknown number of data elements you should be using the push_back() method of inserting your data elements instead of trying to use a fixed sized vector.

    Jim

  7. #22
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Quote Originally Posted by Elysia View Post
    To answer your question: no. There is no such short-hand syntax in C++. You have to use a loop.

    If you want to take the average of N elements, then:
    average = std::accumulate(temperature.begin() + index, temperature.begin() + index + N, 0) / N;
    Loop from 0 to M - N (where M is number of elements and N number of elements you want to average)

    Or

    average = std::accumulate(temperature.begin() + index - N/2, temperature.begin() + index + N/2, 0) / N;
    Loop from N/2 to M - N/2 (where M is number of elements and N number of elements you want to average)

    Disclaimer: This is not tested code.
    so the loop would have to be a for loop? If I am correct?

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A for loop would make sense for such a purpose, yes.
    But it doesn't mean it has to be. All loops can be used for any type of calculation. It merely changes how the loop coding is done.
    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.

  9. #24
    Registered User
    Join Date
    Jun 2012
    Posts
    36

    Thumbs up

    Quote Originally Posted by Elysia View Post
    To answer your question: no. There is no such short-hand syntax in C++. You have to use a loop.

    If you want to take the average of N elements, then:
    average = std::accumulate(temperature.begin() + index, temperature.begin() + index + N, 0) / N;
    Loop from 0 to M - N (where M is number of elements and N number of elements you want to average)

    Or

    average = std::accumulate(temperature.begin() + index - N/2, temperature.begin() + index + N/2, 0) / N;
    Loop from N/2 to M - N/2 (where M is number of elements and N number of elements you want to average - 1 [ie if you want to average 3 elements, N = 2])

    Disclaimer: This is not tested code.
    I will do Jim's advice in am minute but here's my attempt. The only question I have that I have to adjust the number_of_elements_avg accordingly since that's going to vary on how many point averages I want otherwise it wont run. Since no value is being put to number_of_elements_avg.

    Code:
    for (index ==0; numberofterms-number_of_elements_avg)          {
             average = std::accumulate(temperature.begin()+index,temperature.begin()+index+number_of_elements_avg,0)/number_of_elements_avg;
             }
    Thanks Elysia!

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It would be better if you posted code that would actually compile...
    index == 0 should be int index = 0.
    Your are missing index++.
    You should initialize number_of_elements_avg to the number you actually want - 1. You can get this from the user or hard-code it. It's up to you.
    And be sure to test it!
    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.

  11. #26
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Quote Originally Posted by Elysia View Post
    It would be better if you posted code that would actually compile...
    index == 0 should be int index = 0.
    Your are missing index++.
    You should initialize number_of_elements_avg to the number you actually want - 1. You can get this from the user or hard-code it. It's up to you.
    And be sure to test it!
    That's the part I am confused. Why does this need to be number_of_elements_avg-1? If that's what you are saying?

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I found more errors, so again, revised code:

    average = std::accumulate(temperature.begin() + index - N/2, temperature.begin() + index + N/2 + 1, 0) / (N + 1);

    If we say we want to average 3 elements (N=2), then the code becomes:

    average = std::accumulate(temperature.begin() + index - 1, temperature.begin() + index + 2, 0) / 3;

    If index = 1, then this will average element 0 (index - 1 = 1 - 1 = 0) to 2 (index + 2 = 1 + 2 = 3). Remember that the second argument shall be the argument just after the last one you want to add.
    Divide by 3 after that and you have your avg.
    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.

  13. #28
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Quote Originally Posted by Elysia View Post
    I found more errors, so again, revised code:

    average = std::accumulate(temperature.begin() + index - N/2, temperature.begin() + index + N/2 + 1, 0) / (N + 1);

    If we say we want to average 3 elements (N=2), then the code becomes:

    average = std::accumulate(temperature.begin() + index - 1, temperature.begin() + index + 2, 0) / 3;

    If index = 1, then this will average element 0 (index - 1 = 1 - 1 = 0) to 2 (index + 2 = 1 + 2 = 3). Remember that the second argument shall be the argument just after the last one you want to add.
    Divide by 3 after that and you have your avg.
    Code:
    for (int index =0; numberofterms-number_of_elements_avg,index++) 
             {
             average = std::accumulate(temperature.begin() + index - numbers_of_elements/2, temperature.begin() + index + number_of_elements/2 + 1, 0) / (number_of_elements + 1);
             }

  14. #29
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Rod Micael View Post
    so here's what I have so far:


    Is this just part of the problem or will the inside code need to be changed as well?
    Why did you erase what I told you to do? I wasn't wrong either.

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe the current problem is that you're pushing off the work to us.
    Asking questions is fine, but try experimenting and understanding a little, too.
    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. 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