Is it possible to merge those functions into one?

The program run completely fine, the thing is I would like void write and void display function inside one function with least amount of code and line used. I tried it many time no luck, what should I do; This program I'm creating has too many function like 10 and 500 line of code which we student should only have 5 function and no more then 200 line of code. I been lucky enough to get it down to 7 function and 389 lines. I have no clue what this should teach us what matter if one has 500 or more line of code I thought programming is about getting it runing. Any thoughts.

Thank,

John

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 #include <iostream>
#include <iomanip>

using namespace std;

const int COL = 30;
const int ROW = 15;

void write(char[][COL], int);
void display(char[][COL], int);


int main()
{
    char grid [ROW][COL];

    write (grid, ROW);
    display(grid, ROW);

    return 0;
}

void write(char grid [][COL], int rows)
{
    for(int x = 0; x<rows; x++)
    {
        for (int y = 0 ; y < COL; y++)
        {
            grid[x][y] = '#';
        }
    }

}

void display(char grid[][COL], int rows)
{
    int row = 1;
    cout <<"                   Seat              " << endl;
    cout <<"       123456789012345678901234567890" << endl;

    for(int x = 0; x<rows; x++)
    {
        cout  << "Row " << left << setw(3)<< row++;

        for (int y = 0 ; y < COL; y++)
        {
            cout << grid[x][y];
        }
        cout << endl;

    }

}
Last edited on
Well, I'm not sure that those two functions should be combined into one. write() seems to be an initialisation which would be performed only at the start of the program, while display() could be called several different times.
One thing you could to is with care remove some of the unnecessary lines. Personally I like a to use blank lines to separate blocks of code as it aids legibility, but as an example, this could be made shorter:
1
2
3
4
5
6
7
8
9
10
11
void write(char grid [][COL], int rows)
{
    for(int x = 0; x<rows; x++)
    {
        for (int y = 0 ; y < COL; y++)
        {
            grid[x][y] = '#';
        }
    }

}

like this:
1
2
3
4
5
6
void write(char grid [][COL], int rows)
{
    for (int x = 0; x<rows; x++)
        for (int y = 0; y < COL; y++)
            grid[x][y] = '#';
}


The other function could be shortened slightly - but I don't think all whitespace should be removed, that would be sacrificing legibility.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void display(char grid[][COL], int rows)
{
    cout << "                   Seat              \n" 
            "       123456789012345678901234567890" << endl;

    for (int x = 0; x<rows; x++)
    {
        cout  << "Row " << left << setw(3) << (x+1);

        for (int y = 0; y < COL; y++)
            cout << grid[x][y];
   
        cout << endl;
    }
}


I have no clue what this should teach us what matter if one has 500 or more line of code I thought programming is about getting it runing

Well, that's an interesting point. In my opinion "getting it running" is the bare minimum requirement. Beyond that, the program should have a clear, well-planned design with a clean, easy to read structure and layout.

Generally the use of separate functions will tend to make the code shorter, as it reduces unnecessary repetition of the same or similar code over and over again.

If your code is significantly longer than expected, perhaps there are some other design issues to be reconsidered. Without seeing more of the code I don't think it's really possible to give more specific advice.
Okay, thank you that was very detailed and helpful.
Topic archived. No new replies allowed.