Thread: Temperature Average and Median using Vectors

  1. #46
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Osman Zakir View Post
    By the way, why is it that it's now telling me that the two numbers are equal, even when it's supposed to say they're almost equal, and even when they're half a number apart (e.g. 45 and 45.5)?
    You're casting both numbers to int, thereby removing the decimals! How about you try it on paper before putting it in your program? This is basic math!
    In mathematics, how do you know if two numbers are relatively the same? Hint: |x - y| < ϵ.
    I also just told you a few posts above that to see if two floating point numbers are equal, you need to do if (std::abs(x - y) < ϵ) where YOU define the ϵ.
    Redo it.

    Quote Originally Posted by Osman Zakir View Post
    I type-cast them to int for abs() because, apparently, it's not legal to use floating-point numbers as arguments to that function (would it have to hurt to have warned me about that beforehand?).
    Err, how about it is legal?
    Code:
    int main()
    {
    	double x = 1.0;
    	double y = 2.0;
    	std::cout << std::abs(x - y) << " " << std::abs(y - x) << "\n";
    }
    Quote Originally Posted by Osman Zakir View Post
    I tried making a string variable for the distance input, and putting the input into a string vector, but I didn't get where to put in the conversion factors, when to convert them, or get the number and distance measurements to go together. Also, how do I make it so that whether the user enters a number and a measurement with or without spaces doesn't matter?
    For the first part, show us how you'd implement it using a flowchart or pseudo code. You're not going further until you do that and show us that you can do that.
    Logic you should be able to do yourself. How the language works and what facilities are available we'll be happy to help you with.

    Quote Originally Posted by Guest View Post
    Casting to int turns both 45 and 45.5 into 45. Try std::fabs() instead.
    I believe fabs is a legacy function from C (remember C can't support overloads, so it needed a function for floating-point too). In C++, std::abs is overloaded, so it should work for all integral and floating point types.
    Last edited by Elysia; 04-15-2015 at 11:07 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.

  2. #47
    Guest
    Guest
    Quote Originally Posted by Elysia View Post
    I believe fabs is a legacy function from C (remember C can't support overloads, so it needed a function for floating-point too). In C++, std::abs is overloaded, so it should work for all integral and floating point types.
    You're right. I remembered running into a problem with floating point arguments as well and fabs saved the day. I think I probably forgot the std:: and the compiler fell back to cstdlib's abs.

    p.s. The forum has come to a complete crawl here, I assume I'm not the only one?
    Last edited by Guest; 04-15-2015 at 11:54 AM.

  3. #48
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    I need to list the first prime numbers starting from 2 and going up until 100, and I'm getting this output (lots of them are composite numbers, like 4 and 25):
    2
    3
    4
    5
    7
    9
    11
    13
    17
    19
    23
    25
    29
    31
    37
    41
    43
    47
    49
    53
    59
    61
    67
    71
    73
    79
    83
    89
    97
    And this is the code I ran:
    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    bool isDivisible(int number, int divisor);
    bool isPrime(int number);
    void keep_window_open();
    
    int main()
    {
        for (int i = 2; i < 100; i++)
        {
            if (isPrime(i))
            {
                cout << i << "\n";
            }
        }
        keep_window_open();
    }
    
    bool isPrime(int number)
    {
        for (int i = 2; i < sqrt((double)number); i++)
        {
            if (isDivisible(number, i))
            {
                return false;
            }
        }
        return true;
    }
    
    bool isDivisible(int number, int divisor)
    {
        if (number % divisor != 0)
        {
            return false;
        }
        return true;
    }
    
    void keep_window_open()
    {
        cin.clear();
        char ch;
        cout << "Please enter a character to exit: ";
        cin >> ch;
        cin.ignore();
        return;
    }
    And it's giving me the error "i does not define a type" whenever I try to do
    Code:
    auto i = 0;
    in my for-loop header, plus there's the problem of it saying there should be a closing brace before numeric constant or a comma or semicolon or something before it and stuff when I try to initialize a vector of integers 2 through 100 in curly-braces separated by commas. I've got the flag for std=c++14 turned on; maybe I should change it to c++1y or c++11, although it's never bothered by like this before now. Something strange is going on.

    Anyway, the one for checking whether two numbers are almost equal or not is doing fine now, I think. I just need to fix the one where I have to take in one number as input each time through the loop and check which is one is the smallest or largest so far, and the incorporate the distance measurements into it.

    The program where I have to take in one number as input each time through the loop and check for the largest and smallest numbers is giving me an output like this:
    10 0 100 -1|
    0: the smallest so far
    0: the smallest so far
    100: the largest so far
    100: the largest so far
    0: the smallest so far
    100: the largest so far
    100: the largest so far
    -1: the smallest so far
    -1: the smallest so far
    -1: the smallest so far
    .....
    The rest after that is as it should be, but why is it repeating the numbers more than once like that? How do I fix it?
    This is the code.
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    void keep_window_open();
    
    int main()
    {
        double larger;
        double smaller;
        double number = 0;
        vector<double> user_input;
        cout << "Keep entering numbers; to exit the program, just enter something that isn't a number.\n";
        while (cin >> number)
        {
            user_input.push_back(number);
            for (unsigned i = 0; i < user_input.size(); i++)
            {
                for (unsigned j = 0; j < i; j++)
                {
                    if (user_input[i] < user_input[j])
                    {
                        smaller = user_input[i];
                        cout << smaller << ": the smallest so far\n";
                    }
                    else if (user_input[i] > user_input[j])
                    {
                        larger = user_input[i];
                        cout << larger << ": the largest so far\n";
                    }
                }
            }
        }
        int number_of_elements = user_input.size();
        cout << "The smallest value is: " << smaller << "\n";
        cout << "The largest value is: " << larger << "\n";
        cout << "The number of items is: " << number_of_elements << "\n";
        keep_window_open();
        cin.ignore();
    }
    
    void keep_window_open()
    {
        cin.clear();
        cout << "Please enter a character to exit: ";
        char ch;
        cin >> ch;
        cin.ignore();
        return;
    }
    I'll try to do the pseudo code for the distance incorporation in a bit here.

    Edit: Never mind; that code is still producing weird output.
    Last edited by Osman Zakir; 04-15-2015 at 03:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 27
    Last Post: 02-14-2015, 11:17 AM
  2. Pointers (Average and Median)
    By blackqueen212 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2014, 11:33 AM
  3. Replies: 17
    Last Post: 04-17-2012, 05:46 PM
  4. CPU temperature
    By Kempelen in forum Windows Programming
    Replies: 1
    Last Post: 05-23-2011, 04:06 AM
  5. Vectors in vectors - pointers and iterators
    By fisherking in forum C++ Programming
    Replies: 8
    Last Post: 07-27-2010, 09:34 AM