Section 19.11 Practice Passing Arrays to Functions
Time to practice! Please pick and solve at least one of the following problems. If you get done early please pick another problem!
Subsection 19.11.1 Option 1
Activity 19.5.
Write a program that does the following: Declare an array of 10 integer values and pass the array to a function in which you have the user enter 10 integers which you save in the array (you should use a loop here). Next, use a second function to print out all 10 values the user has entered.
For example, if the user entered 45, -34, 5, 0, 1, 100, -55,... then the output should be:
1. 45
2. -34
3. 5
4. 0
5. 1
6. 100
7. -55
...
Does this problem look familiar? You have worked on this before, just not using functions. Below is the code we wrote previously for this problem.
admin.....open in new window
When you are done, please paste your code into the code submission box below:
Subsection 19.11.2 Option 2
Activity 19.6.
Write a program that declares an array of 100 floats. Then call a function, to which you pass the array, which fills the array with the square roots of the numbers 1 - 100. Next, call another function to print the numbers stored in the array in the following form:
The square root of 1 equals 1.00000.
The square root of 2 equals 1.41421.
The square root of 3 equal 1.732051.
...
Note: In order to be able to use the square root function (sqrt()
), you need to include the header file for the mathematics library using the command
#include <math.h>
at the top of your program where the other #include
directives are located.
Does this problem look familiar? You have worked on this before, just not using functions. Below is the code we wrote previously for this problem.
admin.....open in new window
When you are done, please paste your code into the code submission box below:
Subsection 19.11.3 Option 3
Activity 19.7.
Write a program that prompts the user to type in a sentence, and that reads the sentence, character by character, and stores it in an array of characters. Stop reading, when the array is full or the character read is \n
. Next, print out the sentence in reverse. Your program should use two functions: one function to read from the user input the relevant sentence, and another function to print the sentence in reverse. You’ll also need to think about passing along the length of the sentence entered by the user.
Here is a sample output:
Enter a sentence: I love programming.
Reverse: .gnimmargorp evol I
Does this problem look familiar? You have worked on this before, just not using functions. Below is the code we wrote previously for this problem.
admin.....open in new window
When you are done, please paste your code into the code submission box below: