How to put this part of my program into a function

  • #1
leroyjenkens
616
49
Hello, here is my program. It let's 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:
#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.
 
Technology news on Phys.org
  • #2
Step 1: Isolate the section of code you want to factor out
Code:
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:
#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;
}
 
  • #3
Just curious, is this C or C++?
 
  • #4
its C
 
  • #5
Thank you. That step by step explanation helped.

Yes it's C.
 
  • #6
leroyjenkens said:
I'm still struggling figuring out how to separate programs I make into functions.

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:
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:
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:
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.
 
  • #7
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.
 
Back
Top