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.
  • 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
            • 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
              #25

              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
              • 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
                #26

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

                1 Reply Last reply
                0
                • D DavodM

                  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 Offline
                  S Offline
                  Sivyo
                  wrote on last edited by
                  #27

                  I will do just that... Going to go to the school and see if I can work on it there and get some more help. Thanks everyone for trying to help me out, if anyone else can help out still that would be great, will pop in here often, C# is killing me

                  D 1 Reply Last reply
                  0
                  • S Sivyo

                    I will do just that... Going to go to the school and see if I can work on it there and get some more help. Thanks everyone for trying to help me out, if anyone else can help out still that would be great, will pop in here often, C# is killing me

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

                    You'll get your head round it eventually! Its just a matter of getting used to the way of thinking. I'm assuming this is your first programming language?

                    S 2 Replies Last reply
                    0
                    • D DavodM

                      You'll get your head round it eventually! Its just a matter of getting used to the way of thinking. I'm assuming this is your first programming language?

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

                      we learned C and C++ but I was never very good at it, and every time we learn a new language the problems become more difficult. Also, we only study each language up to 2 or 3 months, its a 2 year college.

                      1 Reply Last reply
                      0
                      • D DavodM

                        You'll get your head round it eventually! Its just a matter of getting used to the way of thinking. I'm assuming this is your first programming language?

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

                        well, thanks to everyone for their help, I finished the assingment and now just hoping I did it well... got things done easier but don't think it was a very efficiant way. well, lets hope for the best

                        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