Thread: Help with starting Problem 1

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    4

    Help with starting Problem 1

    insert
    Code:
    Lab 04
    Preparation
    1. Make a folder for lab 4.
    2. Make a source file for the lab with a function that prompts the user for an int and returns the
    value of that int. This is not main. Use this function and your main function for testing.
    All your programs should be in the General Form. Remember this includes function prototype
    declarations. This will always be part of your grade. ALWAYS.
    Call your functions problem_i where “i” is the number of the problem that you wrote the function
    to solve. That is, if you wrote the function to solve problem 1, call your function problem_1.
    Problem 1
    Write a function that takes two ints as input (this function DOES NOT PROMPT the user; use the one
    you wrote earlier for testing) which will represent conditions A and B. Write the function so that it
    preforms the actions as specified in the following truth table. The output for this function is void.
    A is true A is not true
    B is true printf(“Y \n.”) printf(“Y \n.”)
    B is not true printf(“X \n.”) printf(“Y \n.”)
    You should check a single condition. That is, you should only have a simple if - else statement.
    Multiple ifs are not allowed and neither is nesting.
    Problem 2
    Write a function that takes two ints as input. As in the problem before, use one of the ints to represent
    condition A and the next int to represent condition B. Write a function that does the actions specified by
    the truth table below.
    A is true A is not true
    B is true printf(“X \n.”) printf(“X \n.”)
    B is not true printf(“X \n.”) printf(“Y \n.”)
    Your function should have a single check. That is, it should be a simple if-else statement. Do not use
    multiple ifs or nested ifs.
    Problem 3
    Use De Morgan's laws to write a function that is equivalent to the one you wrote to solve problem 2
    (i.e. your new function correctly does what the truth table indicates). The new function should also
    have a single check, but if you used a && operator to solve problem 2, you must now use a || operator,
    and vice versa.
    Problem 4
    Write a void function that takes an int as input. Write a switch statement that prints out “too low” if the
    number given to the function as input is between 0 and 3, inclusive. By inclusive it is meant that it will
    print out “too low” if the number happens to be 0 or 3 as well. The switch statement should print out
    “too high” if the number given is between 6 and 9, inclusive. If the number given as input is either 4 or
    5, then the string “just right” gets printed out. Any other number given as input causes the line “Sorry, 0
    – 9 is all I can do.” to get printed out. The string “too low” should only appear once in your function.
    The string “just right” should only appear once in your function. The string “too high” should only
    appear once in your function.
    Problem 5
    Write a void function that takes two ints as input and correctly executes the following truth table:
    A is true A is not true
    B is true printf(“X. \n”) printf(“Y. \n”)
    B is not true printf(“Y. \n”) printf(“X. \n”)
    While the function may be solved correctly with two checks (i.e. if – else if – else) you are to solve it
    using only one check (i.e. if – else).
    Hint: Use the || operator
    Problem 6
    In the following code,
    void f1() {
    float x = 1.1 ;
    if ( x == 1.1 ) {printf(“this should print.\n”);} /* does not get executed */
    }
    The line “this should print.” never gets printed. This is because constants that are not integers have
    double precision. That is, any number that is not an integer is assumed to be a double, unless specified
    otherwise. Thus, when we compare the float version of the number 1.1 (stored in x) with the double
    version of 1.1 they are not quite equal.
    Rewrite f1 so that printf gets executed when x has a value of 1.1 . Call the new function problem_6.
    Extra Credit
    Solve problem 6, but the only modifications you can make are type casts. That is, you may not change
    any of the code unless it is to add a casting operator.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please do your own homework. Refer to the homework guidelines.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    // your code for declaring the function here
    int main(void)
    {
        // your code to call the function here
        return 0;
    }
    That's about all the help I can give unless you are more specific. What exactly are you having trouble with? Have you tried anything? If so post your code, properly formatted and indented, in [code][/code] tags.

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    4

    Furthur help

    I need help transitioning from the first problem to implement the second, im not sure how to code the truth tables right

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    4
    this is what i have so far insert
    Code:
    #include <stdio.h>
    
    
    int
    main()
    {
    int number
    printf("Enter a number: ");
    scamf("%s", number);
    printf("You entered : %s\n", number);
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    scamf sounds like a dubious function. I wouldn't trust it.

    It seems like you're here more for a handout, than anything else. A little more effort on your part would be nice, particularly
    1. Formatting and indenting your code, as per the comment and link in my post #3.
    2. Bothering to try compiling your code and eliminating silly mistakes that you know how to fix.
    Now, I could be totally wrong there -- and I hope I am -- but you definitely seem to fit the "do my homework for me" pattern.

    Also, you're not terribly clear. It appears, based on your post #5, that you are trying to transition to "Problem 1" (truth table), but having trouble moving past the preparation stage, where you are to create a function (and I quote "This is not main.") that gets an int from the user and returns it.

    The code you posted does not read an integer from the user and return it. It tries to read a string, but incorrectly tries to store that string in an int variable. It does not return anything. Rather it tries to print it, but again, you tell printf it's getting a string to print, and pass it an int. And none of this is in it's own function, as per the instructions. It's all just in main.

    Again, what exactly don't you understand? If you're completely lost, even on what a function is, what is and is not inside main(), and the printf/scanf basics (which your posted code suggests), then you should really get out your textbook and class notes, and start reading. Find some good online tutorials too. Do practice problems until you at least get the hang of printf and scanf.

    Once you've brushed up on your basics, feel free to post back with your updated code (properly formatted), and with specific questions.

  7. #7
    Registered User
    Join Date
    Feb 2014
    Posts
    4
    insert
    Code:
    #include<stdio.h> 
    int main(void) 
    printY(printf("Y \n."); 
    printN(printf("N \n."); { 
    if(a=1||b=1) {printY();} 
    else if(a=1||b=0) {printN();} 
    else if(a=0||b=1) {printY();} 
    else(a=0||b=0) {printY();} 
    return 0; }
    This is what i have an am pretty sure im on the right track its just not executing properly

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Mickey1990 View Post
    insert
    Code:
    #include<stdio.h> 
    int main(void) 
    printY(printf("Y \n."); 
    printN(printf("N \n."); { 
    if(a=1||b=1) {printY();} 
    else if(a=1||b=0) {printN();} 
    else if(a=0||b=1) {printY();} 
    else(a=0||b=0) {printY();} 
    return 0; }
    This is what i have an am pretty sure im on the right track its just not executing properly
    You're not even on the right railroad, let alone on the right track. For one, you seem to be thinking that printY and printN are functions. You didn't declare them to be functions, and the assignment specs seem to me to require they not be. Second, you are required to use a single if, not a chain of three of them. Third, there is a difference between assigning one to a variable (which you are doing), and determining whether a condition is true (which you should be doing). Fourth, this code "solves" problem 1, so should not be in main, but in a separate function. Fifth, even fixing all of those, your truth table would be incorrect (Y would always be printed given your code; it is never possible to get to the part where N is printed). There's undoubtedly a sixth, a seventh, and many more, but that might get you started.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Starting with Dev C ; a problem
    By mabauti in forum C++ Programming
    Replies: 18
    Last Post: 12-05-2007, 11:16 AM
  3. Problem starting a thread from a system service
    By abachler in forum Windows Programming
    Replies: 2
    Last Post: 08-23-2007, 12:27 PM
  4. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  5. Starting out
    By Chris2k in forum Game Programming
    Replies: 5
    Last Post: 11-23-2002, 06:20 PM

Tags for this Thread