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.
  • S Sivyo

    this is the constructor

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

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

    I noticed that i is always equal to zero Also..

        public override string ToString()
        {
            Console.Write("this is the array:  ");
            int i = 0;
            
            while( i < boolSet.Length)
            {
                if (boolSet\[i\])
                {
                    Console.Write("  " + (i+1));
    
                }
                i++;
            }
            Console.WriteLine("");
            return base.ToString();
        }
    

    This makes the boolean entry into a string to be printed, I have a random numbers coming through, there is always a 1 appearing in the array, if I take out the + 1 it will just be zero... any help would be appreciated. And yes, I know List would be good but this is an assignment and they want us to use array and to overwrite the ToString. I am at a loss.

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

    Hi, I didn't get your question? What is your concrete problem? Regards Sebastian

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

    1 Reply Last reply
    0
    • S Sivyo

      this is the constructor

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

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

      I noticed that i is always equal to zero Also..

          public override string ToString()
          {
              Console.Write("this is the array:  ");
              int i = 0;
              
              while( i < boolSet.Length)
              {
                  if (boolSet\[i\])
                  {
                      Console.Write("  " + (i+1));
      
                  }
                  i++;
              }
              Console.WriteLine("");
              return base.ToString();
          }
      

      This makes the boolean entry into a string to be printed, I have a random numbers coming through, there is always a 1 appearing in the array, if I take out the + 1 it will just be zero... any help would be appreciated. And yes, I know List would be good but this is an assignment and they want us to use array and to overwrite the ToString. I am at a loss.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #3

      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 1 Reply Last reply
      0
      • S Sivyo

        this is the constructor

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

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

        I noticed that i is always equal to zero Also..

            public override string ToString()
            {
                Console.Write("this is the array:  ");
                int i = 0;
                
                while( i < boolSet.Length)
                {
                    if (boolSet\[i\])
                    {
                        Console.Write("  " + (i+1));
        
                    }
                    i++;
                }
                Console.WriteLine("");
                return base.ToString();
            }
        

        This makes the boolean entry into a string to be printed, I have a random numbers coming through, there is always a 1 appearing in the array, if I take out the + 1 it will just be zero... any help would be appreciated. And yes, I know List would be good but this is an assignment and they want us to use array and to overwrite the ToString. I am at a loss.

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

        Where do I start? None of the code you gave will do what you want. Notable problems:

        Sivyo wrote:

        if (!(intSet[i] < 0 && intSet[i] > 100))

        No varaible can be Less than zero and greater than 100. Maths does not allow it. Look at "||" for OR rather than AND. If you correct this you will never exit the for loop:

        Sivyo wrote:

        for (int i = 0; i < intSet.Length; )//change to while maybe

        Why? Because you only increment i when you set boolset[i] to true. If I alter your constructor to this:

        public set(int[] intSet)
        {
        boolSet = new bool[inset.Length];
        for (int i = 0; i < intSet.Length; i++)
        {
        if (!((intSet[i] < 0) || (intSet[i] > 100)))
        {
        boolSet[intSet[i]] = true;
        }
        else
        {
        boolSet[intSet[i]] = false;
        }
        }
        }

        it will work a little better, and I will leave the rest to you!

        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
        • OriginalGriffO OriginalGriff

          Where do I start? None of the code you gave will do what you want. Notable problems:

          Sivyo wrote:

          if (!(intSet[i] < 0 && intSet[i] > 100))

          No varaible can be Less than zero and greater than 100. Maths does not allow it. Look at "||" for OR rather than AND. If you correct this you will never exit the for loop:

          Sivyo wrote:

          for (int i = 0; i < intSet.Length; )//change to while maybe

          Why? Because you only increment i when you set boolset[i] to true. If I alter your constructor to this:

          public set(int[] intSet)
          {
          boolSet = new bool[inset.Length];
          for (int i = 0; i < intSet.Length; i++)
          {
          if (!((intSet[i] < 0) || (intSet[i] > 100)))
          {
          boolSet[intSet[i]] = true;
          }
          else
          {
          boolSet[intSet[i]] = false;
          }
          }
          }

          it will work a little better, and I will leave the rest to you!

          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
          #5

          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...

          S OriginalGriffO 3 Replies 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...

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

            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 2 Replies Last reply
            0
            • 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
                                          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