Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to define multiple variables within a loop

    I'm having trouble with a homework assignment. I'll post the broad objective of it below so you understand what the end result should be. I understand how to use if loops to compare numeric values and tell the user if a number is greater lesser than or equal to another. I understand the basic concepts of while
    loops and how to set parameters on how many times they run. Yet I have spent hours trying to figure out how I can compare two user values within a loop. My only thought of how to perhaps accomplish this is to get user input twice during the while loop(meaning I would only loop5 times instead of 10) yet I don't know if this will work or if this is the best way to tackle the problem. It only needs to be written in pseudocode at the moment which helps. But i am just at a loss of how to define and compare user inputted values that are constantly changing within a loop. I've attached a picture of the assignment objective, if my question does not make sense out of context. I would appreciate any help with this aspect of my assignment. I am almost at my wits end! Once I understand this I can easily write the pseduocode and draw the flowchart by myself. Yet I am at a standstill without knowing how to define the variables in this particular situation.




    In this assignment, you will write an algorithm that asks the user for 10 different numbers. Afier
    each number, you should print whether this number is bigger than, smaller than, or the same as
    the one that came right before it. (Note that you won’t print anything after the first number, as
    nothing came before it.) Below is an example. I have put in BLACK the information that your
    algorithm should print, and in RED information that the user is providing. (The red is just the parts that says x is bigger/smaller than y)
    Enter numberi 14
    Enter numberi 20
    20 is bigger than 14
    Enter numberi 5
    5 is smaller than 20
    Enter numberi 5
    5 is the same as 5
    Enter numberi 10
    10 is bigger than 5
    Enter numberi -100
    -100 is smaller than 10
    Enter numberi 0
    0 is bigger than -100
    Enter numberi 0
    0 is the same as 0
    Enter numberi 57
    57 is bigger than 0
    Enter numberi 25
    25 is smaller than 57

    You should type your algorithm Your solution should include:
    0 The steps to solving the problem (your steps should look similar to the examples in the
    Designing Loops lecture notes). You should use a loop to get the 10 numbers from the
    students, and you should use appropriate variables for storing the current number entered
    by the user and the previous number entered by theuser

    Sorry for the weird spelling things I used an OCR translator to get the info in txt form

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Please have a look at forum rule #1.5.

    This forum is not a free coding service. As a general rule, the people helping out in this forum don't write code for others (especially code that appears to be for homework), but try to help with fixing code that doesn't work. You may perhaps get someone to write this script for you, but you'll be far more likely to get help if you have made a substantial effort and written some code yourself. Then someone here will almost certainly help you correct/improve your work.


    The idea is that you compare each number entered by the user with the previous number. Initially the previous number is 14, the user then enters the first comparison which is 20. 20 is compared with 14 and the result given. 20 then becomes the previous number, and the user enters the new number to be compared with that, which is 5. And so on. OK?

    So your comparisons are simple:-
    if (numEntered > previous) { // greater
    if (numEntered < previous) { // smaller
    if (numEntered == previous) { // same


    You need to ensure that the user only enters numbers (not ? or Mickey Mouse). Also, be aware that values entered by the user are always strings and not numbers unless/they are converted to numbers using one of several methods. Here you can not bother with that (luckily) as Javascript will perform the type conversion automatically for you. But in some situations that will cause bad problems.

    You will also need some way of terminating the loop when the user gets tired of it (while loop) or specify that the thing goes on for a fixed 10 numbers (for... loop where final loop count = 10). So you need to inform the user in some way that "This is comparison number x out of 10".

    Please be aware that prompt(), alert() and document.write() are all obsolete. If you are being taught modern Javascript (and not a History of Javascript) you should use DOM methods to obtain data from the user and display messages/results.


    It is your responsibility to die() if necessary….. - PHP Manual
    Last edited by Philip M; 03-09-2013 at 09:31 AM. Reason: Noticed typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    defining ints?

    Quote Originally Posted by Philip M View Post
    Please have a look at forum rule #1.5.

    This forum is not a free coding service. As a general rule, the people helping out in this forum don't write code for others (especially code that appears to be for homework), but try to help with fixing code that doesn't work. You may perhaps get someone to write this script for you, but you'll be far more likely to get help if you have made a substantial effort and written some code yourself. Then someone here will almost certainly help you correct/improve your work.


    The idea is that you compare each number entered by the user with the previous number. Initially the previous number is 14, the user then enters the first comparison which is 20. 20 is compared with 14 and the result given. 20 then becomes the previous number, and the user enters the new number to be compared with that, which is 5. And so on. OK?

    So your comparisons are simple:-
    if (numEntered > previous) { // greater
    if (numEntered < previous) { // smaller
    if (numEntered == previous) { // same


    You need to ensure that the user only enters numbers (not ? or Mickey Mouse). Also, be aware that values entered by the user are always strings and not numbers unless/they are converted to numbers using one of several methods. Here you can not bother with that (luckily) as Javascript will perform the type conversion automatically for you. But in some situations that will cause bad problems.

    You will also need some way of terminating the loop when the user gets tired of it (while loop) or specify that the thing goes on for a fixed 10 numbers (for... loop where final loop count = 10). So you need to inform the user in some way that "This is comparison number x out of 10".

    Please be aware that prompt(), alert() and document.write() are all obsolete. If you are being taught modern Javascript (and not a History of Javascript) you should use DOM methods to obtain data from the user and display messages/results.


    It is your responsibility to die() if necessary….. - PHP Manual

    Phillip- I know this is not a free coding forum. That is not what I am seeking. I should have posted the pseudocode I had last night but I was getting very upset with it. I thought I could explain what I was struggling with just in words. I can't.

    So below is my current pseudocode with the part I am struggling with questioning pointed out. Thanks for your help. The part I am specifically struggling with is how to get both the current and the previous user input saved( defined) into the program and how to have them update continuously throughout the loop. I hope this makes a little more sense now. If not I can try to explain again. I've never been good at explaining things even when I understand them. lol

    import.java.util.*;
    public static void main(String[] args){

    int count = 0
    set up new scaner

    while (count<10)

    count++

    ask user for value (print)
    get user input
    (this below is the part I'm not sure is translatable to java, if it is how would i accomplish this)
    int num1 = previous user entered value
    int num2 = current user entered value

    if num1< num2
    print num1 "is less than" num2
    end if
    if num1>num2
    print num1 "is greater than" num2
    end if
    if num1=num2
    print num1 "is equal to" num2
    end if
    end while
    }
    Thanks
    Last edited by uswhovian; 03-09-2013 at 04:04 PM. Reason: space in int declaration

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Well, to start with

    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum. And use [CODE] tags.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Well, to start with

    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum. And use [CODE] tags.
    ah. thanks for the clarification phillip. i will get that fixed


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •