Thread: Trying to understand how to call 2 functions (unrelated) into main

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    6

    Trying to understand how to call 2 functions (unrelated) into main

    I have two separate mini-assignments that I need to call to main, and I don't quite understand how to do it. I know that there needs to be a function prototype for each, above main, and then each is included in main before they are eventually called. I just don't know what to include as their parameters. Sample code follows:
    Code:
    #include <stdbool.h> 
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define NUM_SUITS 4
    #define NUM_RANKS 15
    
    /* prototypes */
    //some prototype for temperature code
    int deal();  //no idea
    
    int main(void)
    {
        //no idea what to put here
    }
    
      void deal(void);  //dont really know what to put here
        {
        
      bool in_hand[NUM_SUITS][NUM_RANKS] = {false};
      int num_cards, rank, suit;
      const char *rank_code[] = {"Zero", "One", "Two", "Three", "Four",
        "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen",
        "King", "Ace"};
      const char *suit_code[] = {"clubs", "diamonds", "hearts", "spades"};
      
      srand((unsigned) time(NULL));
      
      printf("Enter number of cards in hand: ");
      scanf("%d", &num_cards);
      
      printf("Your hand:\n");
      while (num_cards > 0)  {
        suit = rand() % NUM_SUITS;    /*picks a random suit */
        rank = rand() % NUM_RANKS;    /*picks a random rank */
        if (!in_hand[suit][rank])  {
          in_hand[suit][rank] = true;
          num_cards--;
          printf(" %s of %s\n", rank_code[rank], suit_code[suit]);
        }
       }
       printf("\n");
       
       return 0;
    }
    Last edited by Bibble; 05-01-2012 at 12:21 AM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Well, I've always been fond of floating point numbers. I think, and this is entirely a person favor situation, that you should include no less than fifty `double' parameters.

    If that doesn't work for you, you'll have to take the time to tell use what you are supposed to be doing for the assignments instead of just that you have assignments.

    Soma

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    I'm afraid my ignorance of the situation has made me unaware of what information would be necessary to answer the question.

    Basically, I am tasked to modify a program to read out random cards from a deck by their full names (e.g. Ace of Spades). I was able to do that, and attached the code above for that "stand-alone" code. I was also tasked with a completely unrelated assignment that said "Write a loop that prints the highest temperature in the temperature array for each day of the week. The loop body should call the find_largest function, passing it one row of the array at a time." The code that I have put together for that loop is below.
    Code:
     
    /* function find the largest among array elements */
    int find_largest(int a[], int n)
    {
        int temperatures[7][24];    
        int *p, max;
        /*initialize max with first element of array */
        max = *a;
        
        int (*p)[24];
        printf("Highest temperatures:");
        for (p = temperatures; p < temperatures + 7; p++)
        printf(" %d", find_largest(*p, 24));
        printf("\n");
    
    
        /*loop until the end of the array by incrementing pointer*/
        for (p = a + 1; p<a+n; p++)
              /* replace current element in max if current element
                  is greater than max */
              if (*p > max)
                    max = *p;
        return max;
     }
    The teacher has requested the we insert both assignments into one program such that they are both called by the main() function. I don't really understand how to do that. I hope that is a relatively clear explanation.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Okay.

    Well, first thing is to get both parts of the assignment working by themselves. You can glue them together later.

    The `find_largest' is certainly broken. If you ever called `find_largest', it would run forever. (In reality, it would run until stack exhaustion.)

    Start with the simpler case first. Because of the names and numbers I assume the target data has a temperature record for each hour of the day. Can you code a `find_larget' function that takes a pointer parameter (pointing to 24 `int' values) and find the largest of that set?

    Soma

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    I don't really know how to do that, but I'm trying to read about it in my book.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Bibble View Post
    I don't really know how to do that, but I'm trying to read about it in my book.
    Move the int temperature[][] array out, into main(), and in main, give it the values it needs.

    Then you'll take one row of that array, (temperature[i]) at a time, and pass it to the find_largest() function:

    Code:
    int maxTemp;
    for(i=0;i<7;i++) {
       find_largest(temperature[i], &maxTemp);
    }
    Since maxTemp will be changed by find_largest(), the call the the find_largest() function should include the address of that variable.

    Inside find_largest, you will have no maxTemp type variable - using *maxTemp, instead now. Similarly, you will remove the temperature array from find_largest(), since it is now being brought in from main.

    Inside a function, you don't want variables with the same names as the parameters you're bringing into the function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. I don't understand what it main whitespace separated words
    By Stefke33 in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2009, 06:39 AM
  3. Malloc() call outside main()
    By drb43 in forum C Programming
    Replies: 8
    Last Post: 01-10-2006, 08:50 AM
  4. How to call main of one program in other
    By rinkle_lalwani in forum C Programming
    Replies: 4
    Last Post: 11-28-2005, 12:47 PM
  5. Can I call the main () function within itself ?
    By pritesh in forum C Programming
    Replies: 9
    Last Post: 11-25-2001, 06:11 PM