select_function.c

Code:

#include <stdio.h>

#include <cs50.h>

// Prints my name several times.

void ShowMyName(void) {

for (int i = 0; i < 20; i++) {

printf("Anita Borg\n");

}

}

// Prints the numbers from 1 to 10, each on its own line.

void CountToTen(void) {

for (int n = 1; n <= 10; n++) {

printf("%d\n", n);

}

}

// Prints a compact multipliation table from 1x1 to 9x9

void PrintMultiplicationTable(void) {

for (int a = 1; a < 10; a++) {

// Print the several multiplication facts for this row.

for (int b = 1; b < 10; b++) {

printf("%3d", a*b);

}

printf("\n"); // end the line

}

}

// Prompts the user for their choice of task, then does that task.

int main(void) {

printf("Select something for me to do:\n");

printf("\n");

printf("(1) Print 20 copies of the coder's name.\n");

printf("(2) Print the numbers from 1 to 10.\n");

printf("(3) Print a multiplication table.\n");

printf("\n");

printf("Your selection? ");

int selection = GetInt();

if (selection == 1) {

ShowMyName();

} else if (selection == 2) {

CountToTen();

} else if (selection == 3) {

PrintMultiplicationTable();

} else {

printf("Not a valid selection.\n");

}

}

Terminal Window:

$ make select_function

...

$ ./select_function

Select something for me to do:

(1) Print 20 copies of the coder's name.

(2) Print the numbers from 1 to 10.

(3) Print a multiplication table.

Your selection? 1

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

Anita Borg

$ ./select_function

Select something for me to do:

(1) Print 20 copies of the coder's name.

(2) Print the numbers from 1 to 10.

(3) Print a multiplication table.

Your selection? 2

1

2

3

4

5

6

7

8

9

10

$ ./select_function

Select something for me to do:

(1) Print 20 copies of the coder's name.

(2) Print the numbers from 1 to 10.

(3) Print a multiplication table.

Your selection? 3

1 2 3 4 5 6 7 8 9

2 4 6 8 10 12 14 16 18

3 6 9 12 15 18 21 24 27

4 8 12 16 20 24 28 32 36

5 10 15 20 25 30 35 40 45

6 12 18 24 30 36 42 48 54

7 14 21 28 35 42 49 56 63

8 16 24 32 40 48 56 64 72

9 18 27 36 45 54 63 72 81

$ ./select_function

Select something for me to do:

(1) Print 20 copies of the coder's name.

(2) Print the numbers from 1 to 10.

(3) Print a multiplication table.

Your selection? 4

Not a valid selection.