Thread: Validating arrays

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    5

    Validating arrays

    Alright so pretty much the program is supposed to have values entered in until the user inputs a "-1", then the program is supposed to stop asking. Also, no negative numbers below -1 are allowed but the code is supposed to keep asking until there is a positive number. I've done this before just never with an array. Any help is appreciated and I can post the assignment description if needed. I've been fooling around with it for a while but this is my original code i've been starting from scratch from.

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    int getBoard ();
    bool validate (int lengths[*]);
    
    int main (void)
    {
      getBoard ();
      return 0;
    }
    
    int getBoard ()
    {
      int lengths[10]; //ARRAY FOR ALL THE TRIANGLES SIDE LENGTHS
      int n; //COUNTER FOR THE ARRAY
      bool valid; //TO CHECK TO SEE IF THE INPUT IS NOT NEGATIVE
      do
      {  
        for(n = 0; n < 10; n++)
        {
          printf("Enter the length of board #%d: ", (n + 1));
          scanf("%d", &lengths[n]);
        }
        valid = validate (lengths);
        if (!valid)
        {
          printf("Invalid negative input! Please try again!");
        }
      }while(!valid);
      return lengths[10];
    }
    
    bool validate (int lengths[10])
    {
      bool valid = true; //BOOLEAN TO VALIDATE VALUES
      if (lengths[10] < -1)
      {  
        valid = false;
      }
      return valid;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Don't know if you really want to bother with passing your array back and forth from function to function just to get input.

    Anyway, you are correct that validation and getting input are somewhat separate, but you need to go back to your flowchart to figure out what's going on, I think. (In other words: the questions "Should I put this input value into my array" and "Should I stop asking for input" do not have the same answer, so your "validate" function can't be used for both.)

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    bool validate (int lengths[10])
    {
      bool valid = true; //BOOLEAN TO VALIDATE VALUES
      if (lengths[10] < -1)
      {  
        valid = false;
      }
      return valid;
    }
    There is no such thing as lengths[10]. "lengths" is an array with ten members, numbered 0-9.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    5
    yea i was a little confused on what to put in place of lengths[10], but heres the problem to maybe clarify why I did what I did.

    Problem: Given the lengths of a number (a minimum of 3 and maximum of 50) of boards, determine the number of triangles that could be created from those boards and print the largest possible perimeter of a triangle that can be created with any three unique boards. A triangle can be constructed from three boards if the sum of any two sides is greater than the third (you will need to test all such combinations).
    For example, can the sides of length 4, 8, and 10 create a triangle?
    • 4 + 8 > 10
    • 4 + 10 > 8
    • 8 + 10 > 4
    • Yes!
    Example #2, can the sides of length 7, 8, and 15 create a triangle?
    • 15 + 8 > 7
    • 15 + 7 > 8
    • 8 + 7 > 15 (NOT TRUE)
    • No!
    Example Execution #1:
    Enter the length of board #1: 6
    Enter the length of board #2: 5
    Enter the length of board #3: 7
    Enter the length of board #4: 20
    Enter the length of board #5: 4
    Enter the length of board #6: -1
    The maximum perimeter is 18
    The total number of triangles possible: 4
    • The input will continue until the user enters -1. All other negative values should be rejected!
    • The maximum number of boards that can be entered is 50.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    5
    pretty much I understand how to get the values into the array inputted, I am just not sure how to validate that they are not negative, and how to get the array input to stop when the user inputs -1

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> pretty much I understand how to get the values into the array inputted, I am just not sure how to validate that they are not negative, and how to get the array input to stop when the user inputs -1

    Just do all of the validation within the loop. If scanf doesn't return 1 or the value scanned is less than -1 don't increment 'n'. Otherwise, if the value equals -1 break out of the loop. Otherwise, increment 'n'.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    I am just not sure how to validate that they are not negative
    Just have a test to see if the current input is greater than 0, otherwise ask for input again.

    how to get the array input to stop when the user inputs -1
    In the above test, if the number is -1, exit the program.
    Last edited by Spidey; 07-30-2009 at 12:30 AM.
    Spidey out!

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    5
    Thank you for the input, I actually got my head out of my ass and got the inputs to work. Now what I am troubled with is how the heck do I take all these inputs and put them in a loop that sorts them into three's and then checking if those three sides can make a triangle. The problem is posted above if you don't understand.
    Code:
    #include <stdio.h>
    
    int getBoard ();
    
    int main (void)
    {
      getBoard ();
      return 0;
    }
    
    int getBoard ()
    {
      int lengths[10]; //ARRAY FOR ALL THE TRIANGLES SIDE LENGTHS
      int n = 0; //COUNTER FOR THE ARRAY
      int temp;
        while (n < 10 && temp != -1)
        {
          printf("Enter the length of board #%d: ", (n + 1));
          scanf("%d", &temp);
          if (temp > 0)
          {  
            n++;
            temp = lengths[n];
          }
          else if (temp < -1)
          {
            printf("\n\nInvalid negative input! Please try again!\n");
          }
        }
      return lengths[10];
    }

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    5
    alright, so I got the loop figured out, but when i call the function in main, it says my arraySize counter is uninitialized. I'm pretty sure it has to do with the * and & ordeal. This is the error I receive: In function 'main', 'arraySize' is used uninitialized in this function. Any help would be great.

    Code:
    #include <stdio.h>
    
    int getBoard ();
    void triangleTests (int length[] , int arraySize);
    
    int main (void)
    {
      int arraySize;
      int length[10];
      
      getBoard ();
      triangleTests (length, arraySize);
      return 0;
    }
    
    int getBoard ()
    {
      int length[10]; //ARRAY FOR ALL THE TRIANGLES SIDE LENGTHS
      int arraySize = 0; //COUNTER FOR THE ARRAY
      int temp;
        while (arraySize < 10 && temp != -1)
        {
          printf("Enter the length of board #%d: ", (arraySize + 1));
          scanf("%d", &temp);
          if (temp > 0)
          {  
            arraySize++;
            temp = length[arraySize];
          }
          else if (temp < -1)
          {
            printf("\n\nInvalid negative input! Please try again!\n");
          }
        }
      return arraySize;
    }
    
    
    void triangleTests(int length[], int arraySize)
    {
      int counterA;
      int counterB;
      int counterC;
      int perimLongest;    //KEEPS TRACK OF LARGEST PERIMETER COUNTER
      int triCount;        //COUNTS NUMBER OF TRIANGLES THAT CAN BE MADE
    
      perimLongest = 0;
      triCount = 0;
    
      counterA = 1;
      counterB = 2;
      counterC = 3;
    
      while(counterA <= arraySize)
      {
        while(counterB <= arraySize)
        {
          while(counterC <= arraySize)
          {
            if((length[counterA] + length[counterB]) > length[counterC])
            {
              if((length[counterA] + length[counterC]) > length[counterB])
              {
                if((length[counterB] + length[counterC]) > length[counterA])
                {
                  triCount = triCount + 1;
                  if(perimLongest < (length[counterA] + length[counterB] + length[counterC]))
                  {
                    perimLongest = length[counterA] + length[counterB] + length[counterC];
                  }
                }
              }
            }
    
            counterC = counterC + 1;
          }
        
          counterB = counterB + 1;
        }
      
        counterA = counterA + 1;
        counterB = counterA + 1;
        counterC = counterB + 1;
      }
      printf("The maximum perimeter is %d",perimLongest);
      printf("The total number of triangles possible is: %d",triCount);
      return;
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You seem to be laboring under the assumption that two things with the same name are the same thing. As any John Smith could tell you, this is not true -- the arraySize in main and the arraySize in getBoard are not related by blood, marriage, or friendship.

  11. #11
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    You have to set your int arraySize to what your getBoard() is returning.

    arraySize = getBoard();


    Also, say you have 5 boards to contend with on the floor, so how would you actually go about verifying that you can make a triangle from 3 of them? Can each board be used more than once as well? Since your array of boards contains the lengths, simply check each element against each other, a possible recursion funtion for this? Though the more boards you have the factorial will skyrocket with the number of possibilities.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM