Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to put this part of my program into a function

  1. Jun 14, 2012 #1
    Hello, here is my program. It lets a user put in numbers and then gives them back to the user reversed. Can someone help me separate this program into functions, with the part that reverses the numbers being in its own function?

    Code (Text):
    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
        int readNum;
        int numbers[50];

        printf("How many numbers would you like to enter? ");
        scanf("%d", &readNum);

        printf("\nEnter your numbers: ");

        for (int i = 0; i < readNum; i++)
            scanf("%d", &numbers[i]);

        printf("\nYour numbers reversed are: ");
        for (int i = readNum - 1, numPrinted = 0; i >= 0; i--)
        {
            printf("%d", numbers[i]);
            if (numPrinted < 9)
                numPrinted++;
                else{printf("\n");
                    numPrinted = 0;}
        }
        return 0;
    }
    I'm still struggling figuring out how to separate programs I make into functions.
     
  2. jcsd
  3. Jun 14, 2012 #2

    DavidSnider

    User Avatar
    Gold Member

    Step 1: Isolate the section of code you want to factor out
    Code (Text):

    printf("\nYour numbers reversed are: ");
        for (int i = readNum - 1, numPrinted = 0; i >= 0; i--)
        {
            printf("%d", numbers[i]);
            if (numPrinted < 9)
                numPrinted++;
                else{printf("\n");
                    numPrinted = 0;}
        }
     
    Step 2: Determine what variables are requried as input
    You will need the numbers[] array and readNum

    Step 3: Determine what information you need to be returned once the process is complete
    None.

    Step 4: Construct the signature of your function
    void PrintArrayInReverse(int numbers[], int readNum) {

    }

    Step 5: Refactor
    Code (Text):

    #include <stdio.h>
    #include <stdlib.h>

    void PrintArrayInReverse(int numbers[], int readNum) {
    int numPrinted = 0;
    printf("\nYour numbers reversed are: ");
        for (int i = readNum - 1, numPrinted = 0; i >= 0; i--)
        {
            printf("%d", numbers[i]);
            if (numPrinted < 9)
                numPrinted++;
                else{printf("\n");
                    numPrinted = 0;}
        }
    }

    int main()
    {
        int readNum;
        int numbers[50];

        printf("How many numbers would you like to enter? ");
        scanf("%d", &readNum);

        printf("\nEnter your numbers: ");

        for (int i = 0; i < readNum; i++)
            scanf("%d", &numbers[i]);

        PrintArrayInReverse(numbers,readNum);
        return 0;
    }
     
     
  4. Jun 14, 2012 #3
    Just curious, is this C or C++?
     
  5. Jun 14, 2012 #4
  6. Jun 15, 2012 #5
    Thank you. That step by step explanation helped.

    Yes it's C.
     
  7. Jun 15, 2012 #6

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    One way is to start with a "program" that isn't in C, but solves the problem in a simple logical way. (Or to give it a better name, an "algorithm"). For example
    Code (Text):

    int main()
    {
        read_some_numbers_from_the_terminal();
        print_the_numbers_in_reverse_order();
    }
     
    That is OK, but it's missing something rather important. The "print the numbers" routine needs to know what numbers to print. So you need to store the numbers somehow. Suppose you store them in an array. You also need to know how many numbers you read in.

    The "read" routine needs to output the data so you can pass it to the "print" routine.

    So you can add a that to the program:
    Code (Text):

    int readnumbers{int* numbers);
    void reverseprint(int* numbers, int howmany);

    int main()
    {
        int numbers[50];
        int howmany;

        howmany = readnumbers(numbers);
        reverseprint(numbers, howmany);
    }
     
    That is now real C, except you need to write the two functions.

    For "read numbers" you could start with something like this. (I've jumped a step ahead, and decided you need a variable "howmany" - note, this is a local variable, it's not the same as the "howmany" in the main program!
    Code (Text):

    int readnumbers{int* numbers)
    {
       int howmany;
       prompt_for_how_many_numbers();
       read_how_many_numbers();
       prompt_to_enter_the_numbers();
       for i = 1 to howmany
       {
          read_the_next_number();
       }
       return howmany;
    }
     
    Most of the steps in "readnumbers" are also done by function calls, except they are things like printf() and scanf() which you don't have to write for yourself.

    Then, do the same for "reverseprint"...

    This might seem longwinded, but the important thing is the thought process, not the relative trivia of how much detail you actually write down.
     
  8. Jun 16, 2012 #7

    Mark44

    Staff: Mentor

    Note that AlephZero's code is not quite C just yet, and in particular the top line of the for loop.

    Leroy, I hope that you are starting to get the idea, from AlephZero's and DavidSnider's comments in this thread, and jtbell's comments in the other thread you have going, of how important it is to develop your program from the top down, starting with an algorithm. It is crucial to have a clear understanding, in broad strokes, of what your program needs to do. The details of how the functions accomplish the details can and should come later.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: How to put this part of my program into a function
  1. How to program? (Replies: 4)

  2. How To Program? (Replies: 7)

Loading...