# comparing numbers, precision value

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-22-2011
jackson6612
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
• 03-22-2011
bithub
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.
• 03-23-2011
jackson6612
Quote:

Originally Posted by bithub
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
• 03-23-2011
bithub
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.
• 03-23-2011
kmdv
Quote:

Originally Posted by bithub
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
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).
• 03-23-2011
Mr.777
Quote:

Originally Posted by jackson6612
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..
• 03-23-2011
iMalc
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.
• 03-25-2011
sharris
....
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 :)
• 03-26-2011
jackson6612
Thanks a lot, everyone. I would post follow-on questions soon. Just wanted to offer my thanks.

Best regards
Jackson
• 03-26-2011
Elysia
Quote:

Originally Posted by sharris
...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?
• 03-26-2011
jackson6612
Quote:

Originally Posted by Elysia

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
• 03-26-2011
whiteflags
Quote:

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.
• 03-26-2011
Elysia
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.
• 03-26-2011
whiteflags
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.
• 03-26-2011
jackson6612
Quote:

Originally Posted by whiteflags
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
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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last