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().
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.
#include <iostream>
#include <cmath>
usingnamespace 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
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;
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?