How to put this part of my program into a function

  • Thread starter Thread starter leroyjenkens
  • Start date Start date
  • Tags Tags
    Function Program
Click For Summary

Discussion Overview

The discussion revolves around how to refactor a C program that reverses user-inputted numbers into functions. Participants explore methods for isolating code segments, determining function signatures, and maintaining data flow between functions.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant requests assistance in separating a program into functions, specifically focusing on reversing numbers.
  • Another participant outlines a step-by-step approach to refactoring, including isolating code sections and determining required inputs and outputs for the new function.
  • A proposed function signature is presented: void PrintArrayInReverse(int numbers[], int readNum).
  • Further elaboration includes a complete refactored code example demonstrating the use of the new function.
  • Questions arise about whether the code is written in C or C++, with clarification provided that it is C.
  • One participant suggests starting with a logical algorithm before implementing it in C, emphasizing the importance of understanding the program's structure and data flow.
  • Another participant notes that the proposed code is not fully compliant with C syntax, particularly in the for loop structure.
  • Discussion includes the importance of developing programs from a high-level algorithm down to implementation details.

Areas of Agreement / Disagreement

Participants generally agree on the need to refactor the program into functions, but there are differing opinions on the best approach and syntax details. The discussion remains unresolved regarding the exact implementation of the proposed functions.

Contextual Notes

Some participants mention missing assumptions and the need for clarity in function definitions. There is also a focus on the importance of understanding the overall program structure before diving into specific coding details.

leroyjenkens
Messages
621
Reaction score
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
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;
}
 
Just curious, is this C or C++?
 
its C
 
Thank you. That step by step explanation helped.

Yes it's C.
 
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.
 
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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
Replies
7
Views
2K
Replies
14
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
47
Views
5K