Thread: Please Help Me

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    18

    Exclamation Please Help Me

    Hello everybody am new here. Am starting with C++ and I have a homework I just cant figure it out. This may sound stupid but I need to do a program that takes 10 numbers and determines the greater and smaller number. I cant imagine how could you do that!!!???

    Please if someone has time can anybody tell me what to use (if, switch, for, while,etc) and how can I do this?? Please I need this for tomorrow.

    Thanks for the help and sorry for being a noob.

    oh and sorry for my bad english

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could use something like std::max_element(), but since you're still learning the basics, here's what you need:

    An array of 10 numbers.
    A for loop from 0 to 8.
    An if statement comparing two consecutive numbers in the array.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    sorry but am lost... I dont understand and am so frustrated. But thanks anyway.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    How would you do it in your head or on paper?

    Just write out the steps you would take with as much detail as possible (even obvious things). Then you can convert that into code.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, this sounds like "homework", which is something the forum doesn't take to mean "we'll do your homework for you".

    Instead we give advice on work that you've done, and try to help you if you are stuck on a particular point - just like a forum discussing the English language will explain how to phrase a sentence, but probably will not respond with much usefulness if you asked "I have a task to write an essay of 500 or more words for tomorrow, can someone please do it for me?".

    So, going back to your task:
    Imagine that you have a friend who does exactly what you say, but can't think for himself _at all_ - you have to tell him EVERY STEP of a process, EVERY TIME: "Fill the pot with water, place it on hob number 1, turn on the gas for hob 1, light the gas, wait until it boils, turn off the gas for hob 1, pour out enough water into a tea-cup, put a teabag in the cup with the water, wait for ten seconds, stir, wait ten seconds, stir, wait another minute, take teabag out, serve tea to me" - and if you want a second cup of tea, you can't just say "give me another cup of tea" (although you could give similar instructions to make TWO cups of tea at once!).

    So imagine that you have to describe EACH little step of getting numbers sorted.

    Then write your code based on those "instructions".

    --
    Mats

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    heres what I have, this takes 3 numbers and determines the greater number,

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	int n1, n2, n3;
    
    	cout << "enter 3 numbers:  ";
              cin  >> n1 >> n2 >> n3;
    
    	if(n1 > n2) && (n1 > n3)
    
    		cout << n1 << " is greater";
    
    	else if(n2 > n1) && (n2 > n3)
    
    	        cout << n2 << " is greater";
    
    	else if(n3 > n1) && (n3 > n2)
    
    	        cout << n3 << " is greater";
    
            return 0;
    	system ("pause");
    I know it sucks but I just don't know how to do it with 10 numbers.
    Last edited by Salem; 08-27-2007 at 11:42 PM. Reason: Added code tags, learn to use them

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so here's a hint:
    Do you have to KNOW all the numbers before you print the smallest and largest number? If not, how would that help?

    Also, did your teacher (or literature that you should have read) not teach you how to repeat the same section of code multiple times?

    --
    Mats

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    Quote Originally Posted by matsp View Post
    Ok, so here's a hint:
    Do you have to KNOW all the numbers before you print the smallest and largest number? If not, how would that help?

    Also, did your teacher (or literature that you should have read) not teach you how to repeat the same section of code multiple times?

    --
    Mats
    Thanks for your help, and no, I dont know how to repeat the same section of code multiple times.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Then you need to look that up in your literature/notes! I'm not here to tell you things, because you will either have to LEARN it or you'll fail your class (and someone saying "do this" will not make you learn how to do something very well - doing it yourself will).

    I'm sure that you can find some examples on this by googling too. Or perhaps the tutorial here...

    --
    Mats

  10. #10
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by Conspiracy View Post
    Thanks for your help, and no, I dont know how to repeat the same section of code multiple times.
    I think what matsp meant was about functions. Another hint: Make a function which compares two integers. Also you should use an array to store those ints. Last hint: perhaps the bubblesort algorithm will be the easiest way to handle this.

    All in all, IMHO you should learn more of your C++. Try the excellent tutorials in this site and others.

    PS: Did I give too much hints?
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Given the code so far, perhaps the first exercise is to create a loop which reads in 10 integers, then prints them straight back to the user.

    When that is done (and working), then consider what to do with each new integer as it arrives.

    The problem may be a particularly small elephant for us, but there is still no need for you to try and eat it all in one sitting. Break it up into little sub-problems you can solve (hint: look at previous assignments).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by g4j31a5 View Post
    I think what matsp meant was about functions. Another hint: Make a function which compares two integers. Also you should use an array to store those ints. Last hint: perhaps the bubblesort algorithm will be the easiest way to handle this.

    All in all, IMHO you should learn more of your C++. Try the excellent tutorials in this site and others.

    PS: Did I give too much hints?
    I was actually hinting on a loop of some sort.

    A function may be used too, but since we need 10 numbers read from the user, doing a loop to read those numbers would help a lot.

    --
    Mats

  13. #13
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Also remember with your code it isn't reusable or adaptable outside of three data members input by hand. If you want more data you have to change it. You need to assume that there is the possibility of varying elements. So You need something to hold this data that can be as adaptable as the number of elements themselves.

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You mean, as an array? If you are only interested in the smallest and largest value, you'll only need a variable for those to keep track of the smallest and largest so far (and a third variable to receive input).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed