Functions

This is a practice assignment for one of my classes, I don't understand functions that well. Can anyone code this for me and explain it? I just dont know how to do this at all.




Write a function with the following interface:

void multiplyTable(int num)

This function should display the multiplication table for values from 1...num. For example, if the function is passed 10 when it is called, it should display the following:

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100


As another example, if the function is passed 5, it would print the following:

1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

Write a main() to test your function. Main should ask the user for an input value (in the range 1..15), then call multiplyTable with that value.

Note:
• The maximum parameter value that your function will need to handle is 15.
• Make sure that your numbers are aligned properly as illustrated in the examples above.
The purpose of practice problems is to learn the skills you need to succeed. Having someone else do the work and explain robs you of the benefit and satisfaction of completing it yourself.

That said, a function is essentially a named piece of code. Consider the basic hello world code
1
2
3
4
5
6
#include <iostream>
int main()
{
    std::cout << "Hello World" << std::endl;
    return 0;
}


We can create a function to do the same thing. as the following code.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
void sayHello()
{
    std::cout << "Hello World" << std::endl;
}

int main()
{
    sayHello();
   return 0;
}


Typically functions are used to represent operations that will be used multiple times. They increase the maintainability and the understanding of code.

For the problem you have, you have the function signature
void multiplyTable(int num)

This means that you do not return anything and you take an integer value as a parameter.
With the integer value you receive you need to generate the structure shown by your examples. The generic pattern can be described as

x *1 x*2 x*3 ... X*num
(x+1) (x+1)*2 (x+1)*3 ... (x+1)*num
.
.
.
num*1 num*2 num*3 .... num*num

I suggest thinking about what control structures and variables will be needed coming up with some code and then posting problems that come up as you implement. The purpose here is to aid others, not to inhibit their ability to become programmers by solving their problems for them.

Good luck
Topic archived. No new replies allowed.