Thread: Question about using an vector iteration loop in C++---Running Mean with three values

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

    Exclamation Question about using an vector iteration loop in C++---Running Mean with three values

    I am trying to figure out a general form of this hypothetical example using C++.

    I have a vector with any three numbes. I want the first numbers and third numbers in the vectors to not be changed, however for the second number, I would like the number to be average with the first number, the second number, and the third number in the vector which will replace the second number in the final output.

    I was wondering if iostream <vector> approach is necessary?

    Code:
    #include <iostream>
    #include<vectors>?
    using namespace std;
    
    int main()
    
    {
    
    const int numberofterms=3
    double Temperature[numberofterms]={22,24,16.2}
    float average
    
    If (first vector number and third vector number are chosen:) THEN
    
    The first vector and the third vector are kept. 
    
    Else The second vector will be replaced by the average.
    
    average = (Temperature[0]+Temperature [1]+Temperature [2])/numberofterms;
    
    }
    The displaying part would not be a problem but just the way to write this in C++ I am having a really difficult time to find examples of what I am looking for. Thanks again.!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What do you mean by "iostream <vector> approach"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    I see some of the examples using iostream <vector> is that necessary?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What examples, and where?
    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.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry, but I don't understand what you mean by "iostream <vector>". Basically, if you have a vector of 3 numbers named x, you can use them by accessing x[0], x[1] and x[2]. You can print them like that too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User R41D3N's Avatar
    Join Date
    Mar 2012
    Location
    ...Beyond Infinity, Behind a KeyBoard...
    Posts
    29
    I guess he means something like:

    Code:
    #include <iostream>
    #include <vector>

    And nop that wouldnt be necessary, since its just 3(size fixed) values. You might as well use an array of size 3.

    a[0], a[1], a[2] would be the elements.

    Code:
    a[2] = (a[0]+a[1]+a[2])/3; // for the mean
    -- reserved for when I come up with 1 --

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    let me show you what I am talking about: for loop and vectors - C++ Forum

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, as in use a loop to print the elements of a vector? Sure, you can use that. You don't have to, but you can.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    so how would the if and else loops work in this case @R41D3N?

  10. #10
    Registered User R41D3N's Avatar
    Join Date
    Mar 2012
    Location
    ...Beyond Infinity, Behind a KeyBoard...
    Posts
    29
    just tell the user to enter the element he wants to switch with the average ... lets say x(0/1/2)

    then your lhs would become

    Code:
    a[x] = ....

    You could work vectors the same way, if thats what you are aiming for ...
    Last edited by R41D3N; 06-19-2012 at 10:40 AM.
    -- reserved for when I come up with 1 --

  11. #11
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Code:
    #include <iostream>
    
    #include<vectors>?
    using namespace std;
     
    int main()
     
    {
     
    const int numberofterms=3
    double T[numberofterms]={22,24,16.2}
    
    float average
    
     
    If (T[1], T[3]) 
    
     
    
    cout<<T[1]<<endl;
    cout<<T[2]<<endl;
    
    Else If (which should assume T[2] if I am correct)
    T[2] = (T[0]+T[1]+T[2])/3
    
    }
    of course System("PAUSE") Return0.

  12. #12
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    actually I meant for line 24: T[2] = (T[0]+T[1]+T[2])/numberofterms

  13. #13
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    At least that's my way of "understanding" what you are trying to say. I know its wrong. LOL.

  14. #14
    Registered User R41D3N's Avatar
    Join Date
    Mar 2012
    Location
    ...Beyond Infinity, Behind a KeyBoard...
    Posts
    29
    Quote Originally Posted by Rod Micael View Post
    [
    of course System("PAUSE") Return0.
    dont use system() . If you want to hold the display screen, better use cin.get();

    the way I suggested it, you wouldnt need an if-else block. Rather than user choosing which 2 to keep as is, let them choose which element to modify into the average. So as to get the index.
    -- reserved for when I come up with 1 --

  15. #15
    Registered User
    Join Date
    Jun 2012
    Posts
    36
    Quote Originally Posted by R41D3N View Post
    dont use system() . If you want to hold the display screen, better use cin.get();

    the way I suggested it, you wouldnt need an if-else block. Rather than user choosing which 2 to keep as is, let them choose which element to modify into the average. So as to get the index.
    Code:
    #include <iostream>
     
    #include<vectors>?
    using namespace std;
      
    int main()
      
    {
      
    const int numberofterms=3
    double T[numberofterms]={22,24,16.2}
     
    float average
     
      
    If (T[1], T[3]) 
     
      
     
    cout<<T[1]<<endl;
    cout<<T[2]<<endl;
     
    Else If 
    
    T[2] = (T[0]+T[1]+T[2])/numberofterms
    
    cin.get()
    return 0;
    
    
     
    }
    Would this be a correct approach?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Iteration of loop
    By funky in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 07:26 AM
  2. List iteration problem, it resets values
    By Mike Borozdin in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2006, 09:42 AM
  3. kernel iteration through for loop
    By sononix in forum C++ Programming
    Replies: 4
    Last Post: 08-11-2004, 06:16 AM
  4. Vector Iteration Problems
    By PunkyBunny300 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2004, 05:20 AM
  5. Vector Iteration Problems
    By PunkyBunny300 in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2004, 09:26 PM

Tags for this Thread