Online lab help please.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • td0g03
    New Member
    • Jan 2007
    • 64

    Online lab help please.

    Hello, I been working at this for about 30 minutes now and can't seem to get it. This part is called One-Way selection. Here is the question...

    Given the integer variables x , y , and z , write a fragment of code that assigns the smallest of x , y , and z to another integer variable min .

    Assume that all the variables have already been declared and that x , y , and z have been assigned values).
    Here is the code the provide

    Code:
    1 int main() {
    2    int x, y, z, min;
    Your Code here...

    Code:
    5 
    6    return 0;
    7 }

    Now my question is how wold you find which one has the lowest amount?
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by td0g03
    Hello, I been working at this for about 30 minutes now and can't seem to get it. This part is called One-Way selection. Here is the question...



    Here is the code the provide

    Code:
    1 int main() {
    2    int x, y, z, min;
    Your Code here...

    Code:
    5 
    6    return 0;
    7 }

    Now my question is how wold you find which one has the lowest amount?
    How would you find the lowest amount? Without a computer program?

    Comment

    • td0g03
      New Member
      • Jan 2007
      • 64

      #3
      Originally posted by sicarie
      How would you find the lowest amount? Without a computer program?
      I would first look at all the numbers and see which one is the lowest. Or I would do if A<B and A<C then A is the lowest. If not then etc etc.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by td0g03
        I would first look at all the numbers and see which one is the lowest. Or I would do if A<B and A<C then A is the lowest. If not then etc etc.
        So you would compare one to the other, store the smaller of the two, compare it to the next, and keep doing that until you have completed the program.

        You have your algorithm right there, where are you stuck? On the comparisons?

        Comment

        • td0g03
          New Member
          • Jan 2007
          • 64

          #5
          Originally posted by sicarie
          So you would compare one to the other, store the smaller of the two, compare it to the next, and keep doing that until you have completed the program.

          You have your algorithm right there, where are you stuck? On the comparisons?
          Well, when I try this

          Code:
          if (x<y && x<z)
              min= x;
          else
          if (y<x && y<z)
              min= y;
          else
          if (z<x && z<y)
              min= z;
          I get the error
          Suggestion: You are not assigning anything to min.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by td0g03
            Well, when I try this

            Code:
            if (x<y && x<z)
                min= x;
            else
            if (y<x && y<z)
                min= y;
            else
            if (z<x && z<y)
                min= z;
            I get the error
            I would try removing the else statements, see what that does.

            Comment

            • td0g03
              New Member
              • Jan 2007
              • 64

              #7
              Originally posted by sicarie
              I would try removing the else statements, see what that does.
              No luck darn.

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by td0g03
                No luck darn.
                If that didn't work, I would try each one by itself.

                Comment

                • nmadct
                  Recognized Expert New Member
                  • Jan 2007
                  • 83

                  #9
                  It looks fine, with the else statements. The problem is that the compiler can't tell that in all cases one of the three must be true, so it thinks that in some cases, min might be unassigned. Make it shut up by declaring min=0 at the top of your program. That way, it's satisfied because min will always be defined.
                  Last edited by nmadct; Feb 20 '07, 05:29 AM. Reason: Made an addition

                  Comment

                  Working...