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
|