Thread: Finding the error

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    4

    Finding the error

    So they gave me this code and asked me to find its errors.
    Code:
    #include <stdio.h>
     int main(){
    float a, b, c;
    scanf(“%f %f”, &a, &a); 
    mult(a, b, c);
     printf(“%f\n”, c);
    return 0;
    }
    void mult(float x, float y, float z) {
     z = x * y;
     }
    And my resolution is (full of mistakes):
    Code:
    #include <stdio.h>
    void mult(float x, float y, float z);
    
    
    int main(){
    
    
    float a, b, c;
    
    
    scanf(“%f %f”, &a, &b);
    
    
    mult(a, b, c);
    
    
    printf(“%f\n”, c);
    
    
    return 0;
    
    
    }
    void mult(float x, float y, float z) {
        float z = x * y;
         return z;
         }
    I know its related to the mult function but i don't know what to do with it . Thanks

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    "mult()" is void, so it shouldn't return anything. Think, how can you change a variable from inside another function?
    [hint]Pointers[/hint]
    Devoted my life to programming...

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    You have two options:

    1) pass a pointer to the c variable into mult()

    2) pass a & b to mult() and return the result and capture into c

    In addition, you do not prompt the user what the program does, and what values to enter.

    I would ask for the two values separately.

    main should be defined as:
    Code:
    int main(void)
    {
        / ...
    }

  4. #4
    Registered User
    Join Date
    Jan 2017
    Posts
    16
    Hello,

    I tend to ramble a bit but I promise to give you at least one solid bit of help before the end of this if you wade through the rest patiently.

    The phrase "find its errors" is one of those wide open to interpretation type of things that educators (at least in the USA) seem to be very fond of and it annoys me greatly.

    Errors in syntax?

    Errors in logic?

    Errors in style or failure to follow "best practices"?

    Ack, the list could be very long.

    Let's assume a mere three options of what you should be trying to do:

    1. Just get the code to the point that it: Compiles without error; Allows you to run it without obvious error; Outputs a mathematically accurate result if two numbers are entered.

    2. Same as above but also conforms to some of the practices that your teacher(s) advise.

    3. Same as both above plus actually deals with unexpected input in some reasonable way and feels to you like it is user friendly to some extent.

    4. Trying to send a message to your teachers that you can think beyond the minimum.

    If option one is enough for you, begin a mental loop of actions:
    Code:
    while( compile fails ) {
        Read errors;
        Change code;
    }
    That may seem like lame advice but errors from the compiler are very instructive. Or, possibly it might be more accurate for me to say errors from gcc on Linux are very instructive.

    Since the errors come very close to telling you exactly what you need to do, you shouldn't have much trouble getting past that hump. (Or so I hope)

    ***Solid bit of help: You will need to change things about the call to the function, the definition and declaration of the function, and the contents of the function.

    The original code had one error that would screw up the math, but wouldn't result in a compiler error, but you already caught it when you changed the third argument to scanf(). Yay you!

    Okay, assuming you complete option 1 there is also option 2 to consider.

    I have no real idea what your teacher(s) have told you regarding best practices, but you should be able to look at your notes or textbook or whatever information you have from them and think for a bit and come up with something.

    It's hard to tell in the boards interface what the code really looks like but from what I can see you probably want to at least make sure that all code blocks are indented to make the code easily readable.

    Now for option 3.

    If you think you should care about "improper use" of the program you should try to use the program improperly. Try entering too many numbers, or enter a mixture of numbers and non numbers, and whatever else you can think of, but limit yourself in terms of how long you will do that, because this is after all just school work.

    As for user interface, try to pretend like you were using the program and knew nothing about how it was supposed to work. Pretend that someone opened a terminal typed something like "cd /home/myuser/dev/c/mathstuff/" and said, "I wrote a new program, run it by typing ./multiply", and you did so. Then actually run the program on your system and start imagining what you would have expected from the program somebody else wrote.

    ***Solid bit of help: The declaration for scanf() looks like this:
    Code:
    int scanf(const char *format, ...)
    If successful, the total number of characters written is returned, otherwise a negative number is returned.

    Hmmmn. Why would it ever return a negative number, what use is that?

    Okay, that is all, I hope it helps some.

    P.S. I know that I wrote "assume a mere three options" and then listed four but only talked about three.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by TechnoGourmet View Post
    If [scanf is] successful, the total number of characters written is returned, otherwise a negative number is returned.
    Actually, it returns the number of values successfully scanned, not the number of characters (your thinking of printf).

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Not that this is entirely related, but it's always a good idea to check the return value of scanf when it's used.
    Double Helix STL

  7. #7
    Registered User
    Join Date
    Jan 2017
    Posts
    16
    Quote Originally Posted by algorism View Post
    Actually, it returns the number of values successfully scanned, not the number of characters (your thinking of printf).
    Actually, I wasn't thinking of printf, I was just not thinking. I did a copy and paste of that line from the online docs

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help finding error
    By aka7plus in forum C Programming
    Replies: 1
    Last Post: 05-22-2016, 05:26 AM
  2. I need help finding an error
    By ghozo in forum C Programming
    Replies: 3
    Last Post: 04-09-2016, 08:49 AM
  3. Please help me finding the error... :(
    By fredsilvester93 in forum C Programming
    Replies: 4
    Last Post: 01-04-2012, 01:15 AM
  4. Help finding the error
    By C++Noob316 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2009, 01:16 PM
  5. Need help finding error
    By chorney in forum C++ Programming
    Replies: 13
    Last Post: 01-15-2007, 12:16 AM

Tags for this Thread