Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. User Input Into Array, Need To Check Array Member If It Is Greater Then 100 Or Less Then 0

User Input Into Array, Need To Check Array Member If It Is Greater Then 100 Or Less Then 0

Scheduled Pinned Locked Moved C#
cssdata-structureshelplounge
30 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Christian Graus

    Sivyo wrote:

    intSet[i] < 0 && intSet[i] > 100

    How can a number be less than zero AND greater than 100 ?

    Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

    S Offline
    S Offline
    Sivyo
    wrote on last edited by
    #7

    your right, I made a very obvious mistake. its supposed to be i > 0 and i <100

    1 Reply Last reply
    0
    • S SeMartens

      Well then you should put away the ! in your if-statement...

      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

      S Offline
      S Offline
      Sivyo
      wrote on last edited by
      #8

      it still goes through "This doesn't work " a whole lot but does insert the numbers into the array

      1 Reply Last reply
      0
      • S Sivyo

        I did mess up with the whole greater and less then thing... its suppossed to be i<100 && i> 0 and your right, it is an endless loop of "it doesn't work" ... and if I make those changes

        public set(int[] intSet)
        {
        this.boolSet = new bool[intSet.Length];

                for (int i = 0; i < intSet.Length;i++)//change to while maybe
                {
        
                    if (!(intSet\[i\] > 0 && intSet\[i\] < 100))
                    {   
                       
                        boolSet\[intSet\[i\]\] = true;
                         
                    }
                    else
                    {
                        Console.WriteLine("This doesn't work");
                       boolSet\[intSet\[i\]\] = false; 
                    }
                }
            }
        

        it goes through "this doesn't work" the whole time...

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #9

        I'm betting it doesn't do it "the whole time" - just about 100 times! I think if you put a similar Console.WriteLine into the "boolSet[intSet[i]] = true" part of the if statement, then it will not happen often. Why? Because your input data is nearly all negative or greater than 100. Do you know how to use the debugger? If so then put a breakpoint on the "boolSet = new bool..." line, and single step you way round the loop a few times - see what you have in the data, and what your code it doing with it. It is well worth getting used to the debugger, it will save you a lot of time in the future! P.S. you don't need the "this." part of "this.boolSet = new bool..." - it is implied and makes the code harder to read... It's not wrong to include it, but it isn't needed and you will find out later why you can do it.

        No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • S Sivyo

          I did mess up with the whole greater and less then thing... its suppossed to be i<100 && i> 0 and your right, it is an endless loop of "it doesn't work" ... and if I make those changes

          public set(int[] intSet)
          {
          this.boolSet = new bool[intSet.Length];

                  for (int i = 0; i < intSet.Length;i++)//change to while maybe
                  {
          
                      if (!(intSet\[i\] > 0 && intSet\[i\] < 100))
                      {   
                         
                          boolSet\[intSet\[i\]\] = true;
                           
                      }
                      else
                      {
                          Console.WriteLine("This doesn't work");
                         boolSet\[intSet\[i\]\] = false; 
                      }
                  }
              }
          

          it goes through "this doesn't work" the whole time...

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #10

          Addendum: Now that you have changed to "boolSet[intSet[i]]" you need to swap the "new bool[inset.Length]" back to "new bool[101]" or you will have problems if the number of elements in your data is less than 101!

          No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          S 1 Reply Last reply
          0
          • S SeMartens

            Well then you should put away the ! in your if-statement...

            It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

            S Offline
            S Offline
            Sivyo
            wrote on last edited by
            #11

            it still doesn't stop my program from crashing when the user inserts a number over those numbers

            S 1 Reply Last reply
            0
            • S Sivyo

              it still doesn't stop my program from crashing when the user inserts a number over those numbers

              S Offline
              S Offline
              SeMartens
              wrote on last edited by
              #12

              Do you receive an exception? If yes, what message does the exception has?

              It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

              S 1 Reply Last reply
              0
              • S SeMartens

                Do you receive an exception? If yes, what message does the exception has?

                It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                S Offline
                S Offline
                Sivyo
                wrote on last edited by
                #13

                I noticed when I debug that it doesn't get i... i is always equal to 0

                1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  Addendum: Now that you have changed to "boolSet[intSet[i]]" you need to swap the "new bool[inset.Length]" back to "new bool[101]" or you will have problems if the number of elements in your data is less than 101!

                  No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                  S Offline
                  S Offline
                  Sivyo
                  wrote on last edited by
                  #14

                  I noticed when debugging that i is always equal to 0...

                  S 1 Reply Last reply
                  0
                  • S Sivyo

                    I noticed when debugging that i is always equal to 0...

                    S Offline
                    S Offline
                    SeMartens
                    wrote on last edited by
                    #15

                    Okay, could you post the code with all your changes again. I'm not sure what you changed and what not... Did you forgot the i++ in the for-loop?

                    It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                    S 1 Reply Last reply
                    0
                    • S SeMartens

                      Okay, could you post the code with all your changes again. I'm not sure what you changed and what not... Did you forgot the i++ in the for-loop?

                      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                      S Offline
                      S Offline
                      Sivyo
                      wrote on last edited by
                      #16

                      public set(int[] intSet)
                      {
                      this.boolSet = new bool[101];

                              for (int i = 0; i < intSet.Length;i++)
                      
                                  if (intSet\[i\] > 0 && intSet\[i\] < 100)
                                  {   
                                     
                                      boolSet\[intSet\[i\]\] = true;
                                       
                                  }
                                  else
                                  {
                                   
                                     boolSet\[intSet\[i\]\] = false; 
                                  }
                              }
                          }
                      

                      would it be easier to check the range when user inserts numbers?

                         int\[\] userIn = new int\[3\];
                      
                              bool flag = false;
                              while (flag ==false)
                              {
                                  Console.WriteLine("User, please enter 3 number 0 - 100");
                                  for (int i = 0; i < userIn.Length; i++)
                                  {
                                      if (Int32.TryParse(Console.ReadLine(), out userIn\[i\])) 
                                          flag = true;
                      
                                  }
                              }
                      

                      That is in main

                      S 1 Reply Last reply
                      0
                      • S Sivyo

                        public set(int[] intSet)
                        {
                        this.boolSet = new bool[101];

                                for (int i = 0; i < intSet.Length;i++)
                        
                                    if (intSet\[i\] > 0 && intSet\[i\] < 100)
                                    {   
                                       
                                        boolSet\[intSet\[i\]\] = true;
                                         
                                    }
                                    else
                                    {
                                     
                                       boolSet\[intSet\[i\]\] = false; 
                                    }
                                }
                            }
                        

                        would it be easier to check the range when user inserts numbers?

                           int\[\] userIn = new int\[3\];
                        
                                bool flag = false;
                                while (flag ==false)
                                {
                                    Console.WriteLine("User, please enter 3 number 0 - 100");
                                    for (int i = 0; i < userIn.Length; i++)
                                    {
                                        if (Int32.TryParse(Console.ReadLine(), out userIn\[i\])) 
                                            flag = true;
                        
                                    }
                                }
                        

                        That is in main

                        S Offline
                        S Offline
                        SeMartens
                        wrote on last edited by
                        #17

                        Is the app crashing in the main-method or within the set-method? The set-method seems to be okay...

                        It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                        S 1 Reply Last reply
                        0
                        • S SeMartens

                          Is the app crashing in the main-method or within the set-method? The set-method seems to be okay...

                          It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                          S Offline
                          S Offline
                          Sivyo
                          wrote on last edited by
                          #18

                          its crashing as soon as after the 3 numbers are entered. Maybe it would be better if one of the numbers is checked first then inserted into the array...

                          D 1 Reply Last reply
                          0
                          • S Sivyo

                            its crashing as soon as after the 3 numbers are entered. Maybe it would be better if one of the numbers is checked first then inserted into the array...

                            D Offline
                            D Offline
                            DavodM
                            wrote on last edited by
                            #19

                            Your crash is likely coming from here:

                            else
                            {

                            boolSet\[intSet\[i\]\] = false; 
                            

                            }

                            If intSet[i] is greater than 100 or less then 0(lets say its 103) we go into the else block. Your boolSet array only has 101 items in it so trying to access boolSet[103] will raise and arrayOutOfBoundsException (or something similar) is that the message you see? Also can you state the programs goals more clearly? What information should boolSet hold?

                            S 1 Reply Last reply
                            0
                            • D DavodM

                              Your crash is likely coming from here:

                              else
                              {

                              boolSet\[intSet\[i\]\] = false; 
                              

                              }

                              If intSet[i] is greater than 100 or less then 0(lets say its 103) we go into the else block. Your boolSet array only has 101 items in it so trying to access boolSet[103] will raise and arrayOutOfBoundsException (or something similar) is that the message you see? Also can you state the programs goals more clearly? What information should boolSet hold?

                              S Offline
                              S Offline
                              Sivyo
                              wrote on last edited by
                              #20

                              boolSet should hold weather or no each array has been chosen. Example in an array of 5 and the numbers 1 2 3 5 were chosen then... 0 = False, 1 = False, 2 = true, 3 = True, 4 = False, 5 = True so the program will show on the Console Write line 1 2 3 5 SO I have random numbers from 0 to 100 chosen in order to create 2 separate sets. Then the user must insert 3 numbers to create a userSet. This is where I am having problems. If the user stays in range ect. then it works fine. But when he/she doesn't... well... I get out of bounds error. I know there must be an easier way of doing it... but I really don't have any experience

                              D 1 Reply Last reply
                              0
                              • S Sivyo

                                boolSet should hold weather or no each array has been chosen. Example in an array of 5 and the numbers 1 2 3 5 were chosen then... 0 = False, 1 = False, 2 = true, 3 = True, 4 = False, 5 = True so the program will show on the Console Write line 1 2 3 5 SO I have random numbers from 0 to 100 chosen in order to create 2 separate sets. Then the user must insert 3 numbers to create a userSet. This is where I am having problems. If the user stays in range ect. then it works fine. But when he/she doesn't... well... I get out of bounds error. I know there must be an easier way of doing it... but I really don't have any experience

                                D Offline
                                D Offline
                                DavodM
                                wrote on last edited by
                                #21

                                I see. It should be a fairly easy fix then. (although you have some other problems in your user input code) The easiest way to fix the crash is to remove the else block completely. Think about it, if intSet[i] is not between 0 and 100 there is not an entry in the boolSet array to hold the information, and in fact you don't need one if you only care about the numbers 0 to 100 anyway! The best way with your current code would be to have all the elements in boolSet set to false to start with then let the code you already have set to true the ones the user entered. For the problems with your input code, try entering 1, 2, d. :cool: Also if you have the requirements from the assignment posting them here (or the relevant parts) would give us a clear idea of what you have to achieve. (note I have no intention of doing your homework for you but im quite happy to you help you with problems as you have been doing the work yourself so far! :laugh: )

                                S 2 Replies Last reply
                                0
                                • D DavodM

                                  I see. It should be a fairly easy fix then. (although you have some other problems in your user input code) The easiest way to fix the crash is to remove the else block completely. Think about it, if intSet[i] is not between 0 and 100 there is not an entry in the boolSet array to hold the information, and in fact you don't need one if you only care about the numbers 0 to 100 anyway! The best way with your current code would be to have all the elements in boolSet set to false to start with then let the code you already have set to true the ones the user entered. For the problems with your input code, try entering 1, 2, d. :cool: Also if you have the requirements from the assignment posting them here (or the relevant parts) would give us a clear idea of what you have to achieve. (note I have no intention of doing your homework for you but im quite happy to you help you with problems as you have been doing the work yourself so far! :laugh: )

                                  S Offline
                                  S Offline
                                  Sivyo
                                  wrote on last edited by
                                  #22

                                  yea I figured that, there are two parts, the first part... Write a class that will be used for creating sets of whole numbers in the range of 0…100. Implement the sets with a Boolean array. Define in the class the following operations: 1. A constructor without parameters, which initialize the set as an empty set. 2. A constructor that gets an array of wholes and creates a set that contains those array members. 9. Override the methods ToString and Equals that inherit from Object in the following way: 10. ToString – will return a string representation of the set. Part two is where the user comes in Write a program that checks the sets class in the following way: • Create 2 sets by drawing 10 random numbers in the range of 0…100 for each set, and display the sets contents. • Perform intersection and union of the two sets and display the received sets. • Get from the user 3 numbers and create a third set. Check whether this set is a sub-set of one of the sets and display the result. I am also having problems that after I create union and intersection, there is only one set and not two and the originals die... I haven't played around with that yet so not going to ask for help with it yet.

                                  D 1 Reply Last reply
                                  0
                                  • D DavodM

                                    I see. It should be a fairly easy fix then. (although you have some other problems in your user input code) The easiest way to fix the crash is to remove the else block completely. Think about it, if intSet[i] is not between 0 and 100 there is not an entry in the boolSet array to hold the information, and in fact you don't need one if you only care about the numbers 0 to 100 anyway! The best way with your current code would be to have all the elements in boolSet set to false to start with then let the code you already have set to true the ones the user entered. For the problems with your input code, try entering 1, 2, d. :cool: Also if you have the requirements from the assignment posting them here (or the relevant parts) would give us a clear idea of what you have to achieve. (note I have no intention of doing your homework for you but im quite happy to you help you with problems as you have been doing the work yourself so far! :laugh: )

                                    S Offline
                                    S Offline
                                    Sivyo
                                    wrote on last edited by
                                    #23

                                    found another issue... I insert 3 6 fg and get an output of 4 7....

                                    D 1 Reply Last reply
                                    0
                                    • S Sivyo

                                      yea I figured that, there are two parts, the first part... Write a class that will be used for creating sets of whole numbers in the range of 0…100. Implement the sets with a Boolean array. Define in the class the following operations: 1. A constructor without parameters, which initialize the set as an empty set. 2. A constructor that gets an array of wholes and creates a set that contains those array members. 9. Override the methods ToString and Equals that inherit from Object in the following way: 10. ToString – will return a string representation of the set. Part two is where the user comes in Write a program that checks the sets class in the following way: • Create 2 sets by drawing 10 random numbers in the range of 0…100 for each set, and display the sets contents. • Perform intersection and union of the two sets and display the received sets. • Get from the user 3 numbers and create a third set. Check whether this set is a sub-set of one of the sets and display the result. I am also having problems that after I create union and intersection, there is only one set and not two and the originals die... I haven't played around with that yet so not going to ask for help with it yet.

                                      D Offline
                                      D Offline
                                      DavodM
                                      wrote on last edited by
                                      #24

                                      Looks like an interesting assignment. Are you still having problems with the previous bits?

                                      S 1 Reply Last reply
                                      0
                                      • D DavodM

                                        Looks like an interesting assignment. Are you still having problems with the previous bits?

                                        S Offline
                                        S Offline
                                        Sivyo
                                        wrote on last edited by
                                        #25

                                        nah, don't have a problem with the strange 0, for some reason its not there anymore.

                                        1 Reply Last reply
                                        0
                                        • S Sivyo

                                          found another issue... I insert 3 6 fg and get an output of 4 7....

                                          D Offline
                                          D Offline
                                          DavodM
                                          wrote on last edited by
                                          #26

                                          Im guessing you had been round the for loop a couple of times before you put the right input in? I would suggest scrapping the user input code and starting again, sometimes its easier than fixing something. You might want to break it down into smaller chunks, i.e get the input and store as a string, then try parsing it to an int and then if that works add it to the array, if not ask the user again. Repeat the above 3 times and bingo! The code will be more verbose but it will be easier to debug and understand and once you have it working you can always go back over it and refactor it. Terse code can look better but it can also be hard to understand and maintain.

                                          S 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups