Thread: comparing numbers, precision value

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    comparing numbers, precision value

    Hi

    1: Suppose I'm given six numbers and asked to find the largest or the smallest of them. How do I do it? I mean what would be the syntax? If I wanted to add them I would simply write a+b+c+d+e+f, where a, b, c, d, e, and f are numbers. Please guide me.

    2: How do I set the precision value - number of digits after the decimal point?

    And by the way I'm a beginner and playing around with console based programs.

    Thanks for the help and your time.

    Best wishes
    Jackson

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    1. If a is less than b and a is less than c and a is less than d and a is less than e and a is less than f... that would make it the smallest number. In other words, you could solve this with a lot of if/else statements. If you have a lot of numbers that you're dealing with, then you should probably look into using a good sorting algorithm. A bubble sort is slow but easy to implement. On the other hand, a radix sort is fast but takes more work to implement. Since you are using c++, you can use a built in sorting algorithm. See http://www.cplusplus.com/reference/algorithm/sort/

    2. You cannot set the precision values. A float has a default amount of precision which is less than a double. You can set the amount of numbers that get printed after a decimal point when you do a print though.
    Code:
    printf("%.2f", f);
    That will print a float value to 2 decimal places.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by bithub View Post
    1. If a is less than b and a is less than c and a is less than d and a is less than e and a is less than f... that would make it the smallest number. In other words, you could solve this with a lot of if/else statements. If you have a lot of numbers that you're dealing with, then you should probably look into using a good sorting algorithm. A bubble sort is slow but easy to implement. On the other hand, a radix sort is fast but takes more work to implement. Since you are using c++, you can use a built in sorting algorithm. See sort - C++ Reference

    2. You cannot set the precision values. A float has a default amount of precision which is less than a double. You can set the amount of numbers that get printed after a decimal point when you do a print though.
    Code:
    printf("%.2f", f);
    That will print a float value to 2 decimal places.
    Thanks a lot, bithub.

    Code:
    #include <iostream>
    
    int main()
    {
    	float a, b, c, d, Mean;
    	std::cout << "Enter first number: ";
    	std::cin >> a;
    	std::cout << "Enter second number: ";
    	std::cin >> b;
    	std::cout << "Enter third number: ";
    	std::cin >> c;
    	std::cout << "Enter fourth number: ";
    	std::cin >> d;
    	std::cout << "The mean is: " << (a + b + c + d) / 4;
    }
    I'm a beginner and have only programmed one program so far (and that too, with the help of others!). I would be involved in comparing at least six numbers. I don't know how to implement IF...ELSE statements in C++. Could you guide me, or could you provide me an example by comparing two or more numbers?

    What is "double"? How many 'default' number of digits would there be after the decimal point when using float? How and where to insert this code
    Code:
    printf("%.2f", f);
    ?

    Please guide me. It would be nice of you.

    Best wishes
    Jackson

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    If you don't know what an if/else statement is, then you need to read some tutorials. Do a google search for "c++ tutorial", and you will find a lot of resources to assist you.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by bithub View Post
    1. If a is less than b and a is less than c and a is less than d and a is less than e and a is less than f... that would make it the smallest number. In other words, you could solve this with a lot of if/else statements. If you have a lot of numbers that you're dealing with, then you should probably look into using a good sorting algorithm. A bubble sort is slow but easy to implement. On the other hand, a radix sort is fast but takes more work to implement. Since you are using c++, you can use a built in sorting algorithm. See sort - C++ Reference
    ???

    What is this all sorting for? Just one loop checking for the least/greatest number in each iteration.

    Quote Originally Posted by bithub View Post
    A float has a default amount of precision which is less than a double.
    double provides at least as much precision as float (float <= double <= long double).

  6. #6
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by jackson6612 View Post
    Hi

    1: Suppose I'm given six numbers and asked to find the largest or the smallest of them. How do I do it? I mean what would be the syntax? If I wanted to add them I would simply write a+b+c+d+e+f, where a, b, c, d, e, and f are numbers. Please guide me.

    2: How do I set the precision value - number of digits after the decimal point?

    And by the way I'm a beginner and playing around with console based programs.

    Thanks for the help and your time.

    Best wishes
    Jackson
    See tutorials and you will learn alot about it... Good Luck..
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The first step is going to have to be to learn about arrays. It's not practical to refer to six or more individual variables just to find the lowest and highest. With an array you can refer to the Nth value, and write very short code using small loops, otherwise you're stuck with a whole heap of wasteful copy and paste code that is harder to follow.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    ....
    You got to start somewhere, but never say PLEASE!

    While experimenting when you find examples with <iostream.h> type headers while experimenting (if it's only ONE .cpp file) you simply remove the (.h) from all header like so; <iostream>, than you test it.

    Code:
    clrscr()
    In your first thread the code used a clrscr function. clrscr() is a presumptuous Borland fetish and MSVC has its issues too. For a new users or tester I suggest that you use DevCpp as your basic compiler, than use those others and force them to compile your same working source build using DevCpp.

    Their website:
    Bloodshed Software - Dev-C++

    The download:
    Download Dev-C++ from SourceForge.net

    Anyway, make sure that you add headers that DevCpp included for you automatically if you miss one and it do it with-out your knowledge, than if it still don't compile, MSVC and others are only pretending () it don't understand. It's can be real trip sometime but now you know. They got oop's too ... "OPP'S" I'm sorry about that. I found the solution for that kind of thing, but that another story.

    Bottom line, Borland and MSVC are good for testing and can reduce the size of your executable and they CAN help to insure more truth before production time ... than you use what you want

    Anyway, I'll drop a few cool links and a few examples that can easily get you started before this week is out. Right now the old-lady throwing hints about she needs a new pair of shoes. Time to hit the bricks

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, everyone. I would post follow-on questions soon. Just wanted to offer my thanks.

    Best regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by sharris View Post
    ...For a new users or tester I suggest that you use DevCpp as your basic compiler, than use those others and force them to compile your same working source build using DevCpp...
    For off, Dev-C++ is not a compiler, it is an IDE, and an old one at that.
    Don't recommend it to people not already using it. They're better off with a maintained IDE such as Code::Blocks or Visual Studio.
    Dev-C++ also uses an outdated version of GCC adding to its problems.

    Also, GCC and Visual C++ are good compilers and Code::Blocks and Visual Studio are good IDEs which can be used for real world production. They're not some hobby IDEs that should be used only to test things with.

    jackson6612: What happened to getting a book?
    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. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Elysia View Post

    jackson6612: What happened to getting a book?
    Hi Elysia

    You have a good memory! I thought you would have forgotten about that! Just kidding. Yes, hopefully I will get a book soon but I wouldn't be able to get that C++ Accelerated one. It is by some author named Laffore. Actually my friend who lives in another city has promised me to lend his copy for some time.

    By the way, could you please tell me if I'm allowed to ask questions about the parts of scanned pages of a book I find difficult here on C++ forum?

    With best wishes
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    By the way, could you please tell me if I'm allowed to ask questions about the parts of scanned pages of a book I find difficult here on C++ forum?
    Making and posting copies of printed material here would be an infringement of the author's copyright, so don't post images. Also, I don't think anyone wants to hold your hand through the exercises. As long as you understand what your goal is, you should be able to ask a smart enough question that the exercise instructions are redundant information.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Posting images of pages in the book, but removing them shortly afterwards seems acceptable to me. It is akin to showing a book of yours to a friend.
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    But if the image contains required information in the first place, and then you remove it, the thread becomes pointless. It's like starting a thread asking a question, and then removing the question later when you have the answer. So that is stupid on top of it all.

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by whiteflags View Post
    Making and posting copies of printed material here would be an infringement of the author's copyright, so don't post images. Also, I don't think anyone wants to hold your hand through the exercises. As long as you understand what your goal is, you should be able to ask a smart enough question that the exercise instructions are redundant information.
    Quote Originally Posted by Elysia View Post
    Posting images of pages in the book, but removing them shortly afterwards seems acceptable to me. It is akin to showing a book of yours to a friend.
    Thank you, both of you.

    I won't ask anyone to help me with solving the exercises.

    Using a certain page(s) of a book at a time, in my opinion, doesn't infringe any copyrights, especially when the purpose is learning and research.

    In the US it falls under the Fair use.

    So, would someone please confirm me this? I own the book and have scanned some pages. I could upload them on Google Docs and post links to them here. I am not going to post a link to the entire book here.

    Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  2. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  3. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  4. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM