Need help getting started with a C application

This is a discussion on Need help getting started with a C application within the C Programming forums, part of the General Programming Boards category; Hey there, I'd like to firstly say that I don't want anyone to solve the entire question for me, but ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    10

    Need help getting started with a C application

    Hey there, I'd like to firstly say that I don't want anyone to solve the entire question for me, but there is something that has been really bugging me for a while.

    Problem: Given the length of four sides determine whether they can be used to create a polygon and determine if that polygon is a square. A polygon can be created if no single side is greater than the sum of the other three sides. Display a ONE when the status (polygon or square) is confirmed and ZERO when the status cannot be confirmed.

    It's simple enough, but there is a restriction- we cannot use selection. (logic, if/else, while, that jazz). Without this I'm really confused.

    I would say that I can do the code on my own with ease, but I just need help getting started with this problem. How would I go about doing this without selection? I mean everything I think of has some sort of selection in it. (less than, greater than, even those are prohibited)

    This is what I have so far:
    Code:
    
    
    //Global Declarations
    #include <stdio.h>
    
    
    
    
    //Main function
    int main(void)
    {
      //Variable Declarations
      int side1;
      int side2;
      int side3;
      int side4;
    
    
      //Ask the user for the four sides
      printf("Enter the length of the four sides:");
      scanf("%d", &side1);
      scanf("%d", &side2);
      scanf("%d", &side3);
      scanf("%d", &side4);
    
    
      //Print the user input on screen
      printf("Length of sides: %d %d %d %d\n",side1,side2,side3,side4);
    
    
      //Check if valid polygon
    
    
      return(0);
    }
    Any help is greatly appreciated.
    Thank you.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    Sounds like homework to me - This is ok, but you are going to have to provide your own solution

    What have you attempted to "//Check if valid polygon"?
    Fact - Beethoven wrote his first symphony in C

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > Display a ONE when the status (polygon or square) is confirmed and ZERO when the status cannot be confirmed.
    Well how would you print this?

    Code:
    truth = isPolygon(s1,s2,s3,s4);
    
    // the obvious
    if ( truth ) {
        printf("ONE\n");
    } else {
        printf("ZERO\n");
    }
    
    // the obfuscated (but still uses "selection" as you put it)
    printf("%s\n", truth ? "ONE" : "ZERO" );
    
    // the devious - only if truth is specifically 0 or 1
    const char *messages[] = { "ZERO", "ONE" };
    printf("%s\n", messages[truth] );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Sorry for not replying, really long week at college. I've thought about the question and came up with a solution that somewhat works. It can determine if something is a polygon (I haven't tested this long enough to ensure it works with every combination), but now I'm stumped at determining if the polygon is a square.

    Heres my updated code, please tell me what you think:
    Code:
    //Global Declarations
    #include <stdio.h>
     
     
    //Main function
    int main(void)
    {
      //Variable Declarations
      int side1;
      int side2;
      int side3;
      int side4;
      int sidechecker_1;
      int sidechecker_2;
      int sidechecker_3;
      int sidechecker_4;
      int total_1;
      int total_2;
      int total_3;
      int total_4;
      int sidecheckresult_1;
      int sidecheckresult_2;
      int sidecheckresult_3;
      int sidecheckresult_4;
      int verdict;
      int squarecheck1;
      int squarecheck2;
      int squareresult;
      //Ask the user for the four sides
      printf("Enter the length of the four sides:");
      scanf("%d", &side1);
      scanf("%d", &side2);
      scanf("%d", &side3);
      scanf("%d", &side4);
     
      //Print the user input on screen
      printf("Length of sides: %d %d %d %d \n",side1,side2,side3,side4);
     
      //Check if valid polygon
      total_1 = side2 + side3 + side4;
      sidechecker_1 = 1 - (side1 / total_1);
      sidecheckresult_1 = sidechecker_1;
     
      total_2 = side1 + side3 + side4;
      sidechecker_2 = 1 - (side2 / total_2);
      sidecheckresult_2 = sidechecker_2;
     
      total_3 = side1 + side2 + side4;
    
      sidechecker_3 = 1 - (side3 / total_3);
      sidecheckresult_3 = sidechecker_3;
     
      total_4 = side1 + side2 + side3;
      sidechecker_4 = 1 - (side4 / total_4);
      sidecheckresult_4 = sidechecker_4;
     
      verdict = 1 - ((sidecheckresult_1 + sidecheckresult_2) % (sidecheckresult_3 + sidecheckresult_4));
     
      //Check if square
     
      squarecheck1 = (side1+side2)/(side3+side4);
      squarecheck2 = side1+side2+side3+side4;
      squareresult = 1 - (squarecheck1 % squarecheck2);
     
      //Debug for top - added to make logical thinking easier for myself
         printf("Sidechecker results- 1: %d 2: %d 3: %d 4: %d || Final verdict: %d || Square status: %d\n", sidecheckresult_1, sidecheckresult_2, sidecheckresult_3, sidecheckresult_4, verdict, squareresult);
     
     
      // Print results
      printf("Polygon status: %d\n", verdict);
      printf("Square status: %d\n", squareresult);
     
      return(0);
    }
    The square determining part is just something I did, it doesn't really work.

    Edit: I don't have an option to change my coding color to C? I can't find it anywhere.
    Last edited by tuxdeep; 02-02-2013 at 09:58 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    The code was fine, but you also made it a list with [LIST = 1] [ * ] tags inside it (I fixed it).

    Well it might be a square if all the sides are of equal length.
    But then that's also true for a diamond shape as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Thanks for fixing the color problem!

    Hmm, I see where you're coming from, but would it really matter in this question? They specifically ask for squares, and the easiest way to check if a polygon is a square would be to check if side1 = side2 = side3 = side4. Unfortunately, I can't use equal signs so I have to devise a procedure using mathematics.

    I was thinking maybe doing the same thing I did to check if the polygon was a valid polygon or not, but instead of doing the modulo operator in verdict, just multiply them together. (i.e: (1*1*1*1) = 1 and (1*0*1*1) = 0)

    Just something I came up with, I'll code it up and test it later today.

    Edit: Ignore what I said above, it doesn't work. Instead, I came up with a different way that works perfectly so far.

    Here is the completed first draft of the code, still have to comment it out and remove unnecessary parts (debug line):

    Code:
    //Global Declarations
    #include <stdio.h>
    
    
    //Main function
    int main(void)
    {
      //Variable Declarations
      int side1;
      int side2;
      int side3;
      int side4;
      int sidechecker_1;
      int sidechecker_2;
      int sidechecker_3;
      int sidechecker_4;
      int total_1;
      int total_2;
      int total_3;
      int total_4;
      int sidetotal;
      int sidecheckresult_1;
      int sidecheckresult_2;
      int sidecheckresult_3;
      int sidecheckresult_4;
      int verdict;
      int squarecheck1;
      int squarecheck2;
      int squarecheck3;
      int squarecheck4;
      int squareresult;
      //Ask the user for the four sides
      printf("Enter the length of the four sides:");
      scanf("%d", &side1);
      scanf("%d", &side2);
      scanf("%d", &side3);
      scanf("%d", &side4);
    
      //Print the user input on screen
      printf("Length of sides: %d %d %d %d \n",side1,side2,side3,side4);
    
      //Check if valid polygon
      total_1 = side2 + side3 + side4;
      sidechecker_1 = 1 - (side1 / total_1);
      sidecheckresult_1 = sidechecker_1;
    
      total_2 = side1 + side3 + side4;
      sidechecker_2 = 1 - (side2 / total_2);
      sidecheckresult_2 = sidechecker_2;
    
      total_3 = side1 + side2 + side4;
      sidechecker_3 = 1 - (side3 / total_3);
      sidecheckresult_3 = sidechecker_3;
    
      total_4 = side1 + side2 + side3;
      sidechecker_4 = 1 - (side4 / total_4);
      sidecheckresult_4 = sidechecker_4;
    
      verdict = 1 - ((sidecheckresult_1 + sidecheckresult_2) % (sidecheckresult_3 + sidecheckresult_4));
    
      //Check if square
      sidetotal = side1+side2+side3+side4;
      squarecheck1 = (4*(side1))/sidetotal;
      squarecheck2 = (4*(side2))/sidetotal;
      squarecheck3 = (4*(side3))/sidetotal;
      squarecheck4 = (4*(side4))/sidetotal;
      squareresult = squarecheck1*squarecheck2*squarecheck3*squarecheck4;
    
      //Debug for top - added to make logical thinking easier for myself
      //     printf("Sidechecker results- 1: %d 2: %d 3: %d 4: %d || Final verdict: %d || Square status: %d\n", sidecheckresult_1, sidecheckresult_2, sidecheckresult_3, sidecheckresult_4, verdict, squareresult);
    
    
      // Print results
      printf("Polygon status: %d\n", verdict);
      printf("Square status: %d\n", squareresult);
    
      return(0);
    }
    Also, the code was properly colored until I deleted what was down here, then it just turned all grey and icky. Any clue what happened?
    Last edited by tuxdeep; 02-03-2013 at 10:48 AM.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > Also, the code was properly colored until I deleted what was down here, then it just turned all grey and icky. Any clue what happened?
    Yeah, it is when you edit and save.
    But then it's all good when you do a refresh.

    > They specifically ask for squares, and the easiest way to check if a polygon is a square would be to check if side1 = side2 = side3 = side4.
    Yeah, I've never understood why tutors think that "don't use the obvious" is a good thing.

    The thing about these 'magic' tricks is
    - The 'wow' moment doesn't last when you eventually get told what it is (like the xor variable swap - the first time you see it).
    - It has very little (if any) usefulness outside the immediate problem that was constructed around it to make it work. Because it is of limited use, it tends to get forgotten about (you're attending college to learn things, not forget things).
    - Professionally written code is all about maintaining clarity of purpose. Nobody has time to spend days pulling apart some clever magic trick which no longer satisfies the problem at hand. It would just get deleted and replaced with something simple, obvious, and working.

    If your tutor does end up giving you some clever mathematical expression, then just feed it 4 values close to INT_MAX (or INT_MIN) and watch it blow up.
    C data types - Wikipedia, the free encyclopedia
    Whilst it might be an algebraic answer, computationally on a real machine with real overflow and underflow to deal with, expect surprises.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Thanks, I'll keep that in mind whenever I edit my post next time.

    I agree about the 'don't use the obvious' thing, but at the same time I can see where my instructor is coming from. I'm taking an introductory programming class in my college, but I've self taught myself C++ before so I can do these first few assignments with ease. We're not allowed to use 'selection' because it's ahead in the course (like two to four chapters); however, I really don't think they should give us twisted assignments like these if we can't apply whatever we want. All of those expressions and variables wouldn't be needed if I could have used if statements or for loops, for example, cutting out unnecessary code and increasing the efficiency of the code, if you know what I mean.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    You said that this problem has been bugging you for a while now (so I'm assuming that it was a while ago you did this question - i.e. You are no longer getting marked on this, right?)
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    It's due today, and I posted this a week ago. I did all I could to it and I'm quite pleased with the result, haven't turned it in yet but I will get marked on it.

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    There has been a lot of criticism from the programming industry about students graduating without being taught "critical thinking", instead they are being told how to put a program together in Java. I don't have time to dig up the links which I have seen in other forum topics on this site, but they are there...

    These programs you have been asked to put together are by every account stupid, but I don't think that it is bad for you critical thinking. It's important that you try hard to come up with your solution using all the mathematical operators

    Just make sure that when you actually make a program, do as Salem said and don't use any of these "magic tricks".
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-18-2012, 04:20 AM
  2. Replies: 1
    Last Post: 07-03-2010, 01:18 PM
  3. Application A interacting with application B
    By Dante Wingates in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2010, 08:01 AM
  4. Getting Started
    By MadnessRed in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 08-23-2008, 04:52 AM
  5. Convirt my C program to a GUI application/Web Application
    By kapil1089thekin in forum C Programming
    Replies: 6
    Last Post: 07-21-2008, 01:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21