Can someone please help me with a short segment of code I am having trouble with?

This is a discussion on Can someone please help me with a short segment of code I am having trouble with? within the C Programming forums, part of the General Programming Boards category; Hello, Can someone please help me understand why my function is not working? I tried a few different things but ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Can someone please help me with a short segment of code I am having trouble with?

    Hello,
    Can someone please help me understand why my function is not working?
    I tried a few different things but below is my best attempt which is still not working like it is suppose to:/



    One of my functions in my header file and how I attempted to code it:
    Code:
    // Pre-conditions: board and answer are of length NUM_PEGS and store the player's
    //                 guess and the correct board, respectively.
    // Post-conditions: returns the number of pegs that match at the correct spots.
    int numPerfectMatches(int board[NUM_PEGS], int answer[NUM_PEGS]) {
    
    for (i=0; i<NUM_PEGS; i++) {
    if (answer==board)
    num_perfect+=1
       
    }
    part of my program that includes the variables from the header file:

    Code:
            // Figure out the number of matches.
            int num_perfect = numPerfectMatches(board, answer);
            int num_imperfect = numWrongPlaceMatches(board, answer);
            
            // Update for this turn.
            printf("You have %d perfect matches and %d imperfect matches.\n", num_perfect, num_imperfect);
            num_turns++;
    NOTE: I am only focusing on "num_perfect" as of right now.

    Thank You

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by matthayzon89 View Post
    Hello,
    Can someone please help me understand why my function is not working?
    I tried a few different things but below is my best attempt which is still not working like it is suppose to:/
    Can u be more specific?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    I am working on creating a guessing game, the function above is suppose to be a loop that adds 1 to the count of each correct guess and then outputs the amount of "correct guesses" (num_perfect) in the segment of code that I posted below the function.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your function *prototypes* and defines should be in your header file. You function code, should be in your .c file.

    You want to compare board[i] with perfect[i], imo. Use your index!


    Trivially, if you know num_perfect, then you don't need to call a function for num_imperfect. That is simply num_pegs - num_perfect.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    oh I see...
    So this is what a header file should look like? and after I have a header file then every function in the header file should be coded in my .c file?

    Code:
    #define NUM_PEGS 4
    #define NUM_COLORS 6
    #define MAX_TURNS 10
    #define MARKED_BOARD -1
    #define MARKED_ANSWER -2
    
    
    void fillBoard(int board[]);
    int numPerfectMatches(int board[], int answer[]);
    void getUserGuess(int board[]);
    void copyArray(int dest[], int source[], int length);
    void markOutCorrect(int board[], int answer[]);
    int numWrongPlaceMatches(int board[], int answer[]);
    void greeting();
    void printArray(int array[], int length);
    void printWinner(int num_turns, int num_seconds);

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Can someone please help me understand how the function code for the functions listed in the header file above can be coded into such a program such as the one below:

    I am not looking for solutions, just an explanation... The reason why I am confused is because the program below was given to us with the program assignment instructions...


    Do you guys think I am just suppose to use the program below as a structure suggestion and simply replace each line below with a line of code that I created that completes the same objectives as the lines below suggest, or am i suppose to run the code below at the end, just as it is, and have it work successfully?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <time.h>
    #include "frameworknew.h"
    
    
    int main () {
    
    int guess1, guess2, guess3, guess4;
    
    
    if ((guess1 == 0 > 5) && (guess2 == 0 > 5) && (guess3 == 0 > 5) && (guess4 == 0 > 5)); 
    {
    
        // Stores the real answer and the user's board, respectively.
        int answer[NUM_PEGS];
        int board[NUM_PEGS];
        
        // Set everything up.
        int num_turns = 0;    
        fillBoard(answer);
        greeting();
        int start = time(0);
        
        // Loop through each turn, ending at 10 moves or a win.
        do {
            
            // Read in the user's guess.
            getUserGuess(board);
            
            // Figure out the number of matches.
            int num_perfect = numPerfectMatches(board, answer);
            int num_imperfect = numWrongPlaceMatches(board, answer);
            
            // Update for this turn.
            printf("You have %d perfect matches and %d imperfect matches.\n", num_perfect, num_imperfect);
            num_turns++;
            
        } while (num_turns < MAX_TURNS && numPerfectMatches(board, answer) < NUM_PEGS);
        
        // Winning case.
        if (numPerfectMatches(board, answer) == NUM_PEGS) {
            int end = time(0);
            printWinner(num_turns, end-start);
        }
        
        // Losing case.
        else {
            printf("Sorry, you have exceeded the maximum number of turns. You lose.\n");
            printf("Here is the winning board: ");
            printArray(answer, NUM_PEGS);
            printf("\n");
            }
    }
    
    
    system ("PAUSE");
    return 0;
    
    }
    Last edited by matthayzon89; 03-11-2010 at 04:22 PM.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    First, I haven't read all of this thread, just your last post. My guess is that you are given the file above (obviously), that you've posted, and are also given "frameworknew.h", and what you have to do is write a file "frameworknew.c" that implements all of the functions listed in "frameworknew.h". So, say you are given "foo.h" that looks like
    Code:
    int someVariable = 1234;
    
    int getRandomNumber();
    
    void foo(char*);
    
    // and more function prototypes
    then you have to write the file "foo.c", which implements all of those functions, like
    Code:
    #include "foo.h"
    
    int getRandomNumber()
    {
       return 42;
    }
    
    void foo(char * str)
    {
       if ( str != NULL )
       {
          printf("%s\n", str);
       }
    }
    So, if my assumption above is correct, then you have to create the corresponding ".c" file, implementing each function prototype in the ".h" file.

    However, if this is an assignment, I imagine you are told exactly what to do, and you shouldn't really have to "guess" what the goal is. Try and clarify with the appropriate person, if this is the case.
    Last edited by nadroj; 03-11-2010 at 04:37 PM.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Hey, Thank you for the response

    ...so if I need to write a .c file that implements each function prototype in the .h file why do you think the code one post above yours was given?


    so If i am understanding correctly I would end up with 3 files total, 1) framework.h 2)framework.c (implementing each function) and 3) the .c code that is located one post above yours?
    Last edited by matthayzon89; 03-11-2010 at 04:51 PM.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    why do you think this was included in the assignment?
    Are you asking why the ".h" file was included? If so, the header files is basically the "interface". It says "I have these global variables (if any), and these functions named X, Y, Z, which take these types of arguments, and return this type of variable." Then the program that uses this (the program you posted above, with "main") just needs to refer to the header file, or "interface". It just needs to know that "Oh, there's a function named "getRandomVariable" that I can use." This prevents the caller from having to know or care about the implementation of this function. It just needs the functions signature (return type, name, type of parameters). It doesn't need to know how it does whatever it does. It just needs to know that it will "somehow" be done. You can kinda think of the header file as a "blackbox" to the ".c" code.

    Your instructor, or whoever, gave you the ".h" file and the "main" program so that each student (I assume) has to implement the same functions, without modifying any of the given .h or .c code. You may implement the functions differently than some other person. However, all that matters is that the function does what it should do. Again, how it does it is transparent to the caller/main program.

    In short, the ".h" file specifies the requirements, what you must implement. If you don't implement something, or something correctly, then the program will either not compile at all, or not run correctly.

    For example, when you "#include <stdio.h>" to use, say, "printf", you don't have to go look at the code or know anything about the implementation of "printf" function. You just need to know how to use it, and expect it will do what it says it will do (i.e. print something).

    EDIT:
    so If i am understanding correctly I would end up with 3 files total, 1) framework.h 2)framework.c (implementing each function) and 3) the .c code that is located one post above yours?
    Yes. You, I believe, are given the two files, and you must write "framework.c", for a total of 3 files.
    Last edited by nadroj; 03-11-2010 at 05:00 PM.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    So once I am done writing the code for each function in the framework.c file... will using a function call such as "#include "framework.h" be good enough for my guessinggame.c program to understand what each function in my header file means?


    Thank you so much by the way, I am finally beginning to understand what needs to be done THANK YOU
    Last edited by matthayzon89; 03-11-2010 at 05:10 PM.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    The "#include "framework.h"" line just tells the compiler to "look" into that file, so it knows what variables it declares, as well as what the prototypes of the functions in it. These prototypes are basically just "promises" that somewhere these functions will actually be implemented. To the compiler, all it needs to know is its prototype, like
    Code:
    int foo(char* bar);
    is a function prototype, and gives the compiler all the information it needs about this function.

    When you compile the program given to you, the compiler will "look" at the ".h" file you include, so it knows what functions you promised to implement. When the compiler later "link"s the code, it is actually expecting you to come through on your promise that you will implement these functions. So, it is looking for the actual ".c" code of these functions. If it isn't given, you will see compiler errors like "undefined reference", because you promised to implement a function, then never did (according to the compiler).

    In short, in addition to the "#include" line, when you compile you have to also tell the compiler where the code is, so you need to pass two files, each of your ".c" files. If you use gcc, you might do something like
    Code:
    gcc mainProgram.c framework.c -o myProg
    If you use a different compiler, then look around in it's documentation for compiling multiple files. I think it will "automatically" figure out what files to compile, and compile them, but I'm not sure.

    EDIT: Also, the "#include" isnt a "function call". Its a "preprocessor directive", that you can read about if you want.
    Last edited by nadroj; 03-11-2010 at 05:18 PM.

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    nadroj your awesome, thank you!

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Your welcome for the help.

  14. #14
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Can someone please help me understand what I need to do with this function?

    I tried looking for 'int length' in the code but failed to find it, the instructions for post and preconditions are not very clear, can anyone hint me what I need to do maybe?

    Some information that might help is:
    *Each player has upto 10 tries to get the guess correctly (the game ends if they successfully get it right before 10 tries
    *each guess contains 4 numbers, and the answer consists of 4 random numbers



    Code:
    // Pre-conditions: dest and source are both have the given length.
    // Post-conditions: All values from source are copied into the corresponding
    //                  locations in dest.
    void copyArray(int dest[], int source[], int length) {
        
    }

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Matt, not sure about the overall program but the frunction you are talking about looks like a vector copy. Basically you need to iterate through vector source and copy every element in vector dest.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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