Some help with my assignment

Hi guys
I would like some feedback on my progress with one of my assignment questions about void functions and if I am actually doing it right. the assignment question is as follows. Thanks in advance.

Question 4
A function named printTabs(with no parameters) produces a table of the numbers 1 to 10, their squares and their cubes.
Question 4a
Write a function printTabs. Include the function in a working program named question4.cpp and call function printTabs from main().

below is my answer for 4a

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
#include <iostream>
#include <cmath>
using namespace std;

void printTabs()
{
    int answer;
    //loop to calculate table
    for(int i = 1; i <= 10; i++)
    {
        for(int j = 1; j <=3; j++)
        {
            answer = pow(i, j);
            cout << answer << '\t';
        }//end of inner loop

        cout << endl;

    }//end of outer loop 

}//end of void printTabs


int main()//main to display heading and call void functin
{
    cout << "NUMBER" << '\t' << "SQUARE" << '\t' << "CUBE" << endl; //heading
    printTabs();//call for void printTabs to display table
}


Question 4b
Modify the program by adding a function selTabs to accept the starting values of the table, the number of values to be displayed, and the number to increment between the values. If the increment is not explicitly sent, the function should use a default value of 1. example: A call selTabs(6, 5, 2) should produce a table of five lines, the first line starting with number 6 and each succeeding number increasing buy 2.

Below is my answer for 4b

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
54
55
56
57
#include <iostream>
#include <cmath>
using namespace std;

//printTabs function to calculate and output table
void printTabs(int startValO, int rowO, int incrementO)
{
    int answer;

    for(int i = 1; i <= rowO ; i++)//start loop to control rows
    {

        for(int j = 1; j <= 3; j++)//loop to control columns
        {
            answer = pow(startValO, j);//expression to calculate square and cube of starting value/incremented value with column j
            cout << answer << '\t';//out put of integer answer
        }//end of column loop

        startValO += incrementO; // to increment the initial value 
        cout << endl;

    }//end of row loop
}//*********************End of printTabs********************//


//selTabs to 
void selTabs(int startValP, int rowP, int incrementP)
{
    //if statement to set increment to 1 if a value of 0 or less us input
    if(incrementP < 1)
        incrementP = 1;
        
    printTabs(startValP, rowP, incrementP);
}

//main function to call table from function printTabs
int main()
{
    int startVal, rows, increment;

    //user instructions and input
    cout << "Enter the starting value of the table: ";
    cin >> startVal;
    cout << "Enter the number of values to be displayed: ";
    cin >> rows;
    cout << "Enter the increment between the values: ";
    cin >> increment;


    //display headings
    cout << "NUMBER" << '\t' << "SQUARE" << '\t' << "CUBE" << endl;
    //call and produce table from void function printTabs
    selTabs(startVal, rows, increment);

    return 0;

}//end of main 
Last edited on
I've just looked at the first one right now, but you don't absolutely need a second for loop and <cmath> to output the table. You can just do something like this...

1
2
for(int i = 1; i <= 10; i++)
        cout << i << "\t" << i*i << "\t" << i*i*i << endl;

to get

NUMBER	SQUARE	CUBE
1	1	1
2	4	8
3	9	27
4	16	64
5	25	125
6	36	216
7	49	343
8	64	512
9	81	729
10	100	1000
Last edited on
Thanks for looking wild blue.

the 1st function setTabs ultimately calculates a table with values that are input by the user, thats why i used cmath and the extra loop.
Any feedback on question 4b?

Last edited on
4b works for me.

You can still get the output in one for loop even if the user enters the data.

1
2
3
4
5
6
7
int startVal=5, rows=10, increment=2;
	
for(int i = 1; i <= rows; i++)
{
     cout << startVal << "\t" << startVal*startVal << "\t" << startVal*startVal*startVal << endl;
     startVal+=increment;
} 


5	25	125
7	49	343
9	81	729
11	121	1331
13	169	2197
15	225	3375
17	289	4913
19	361	6859
21	441	9261
23	529	12167
Topic archived. No new replies allowed.