Week 11 Lab
Q&A on Material
- Functions: array passing (by reference)
- Functions: return types
- Functions: Prototypes (function declaration)
- Bonus only: arrays as character strings
Sign in to lab!
Exercises
Complete the Lab 11 - Using Functions
assignment on Vocareum. The goal of this exercise is to help prepare you for HW 11 and 12. In this assignment you are given several functions that process arrays. Your job is to complete main()
to make calls to the provided functions and add a small amount of “glue” code to output the results, etc.
Spend the majority of your time on this exercise.
Other Exercise(s)
Walk through these exercises and complete them as a group (if time allows):
Tracing Exercise
Assume the user types abcdef
when prompted.
#include <iostream>
using namespace std;
void f1(char str[], int i, int j);
int main()
{
// 6 locations + 1 for the null character
char input_str[7];
cout << "Enter a 6 character string: " << endl;
cin >> input_str;
for(int i = 0; i < 6; i++){
f1( input_str, i, (i+1)%6 )
}
for(int i = 0; i < 6; i++)
{
cout << input_str[i];
}
cout << endl;
return 0;
}
void f1(char str[], int i, int j)
{
char temp;
temp = str[j];
str[j] = str[i] + 1;
str[i] = temp;
return;
}
Challenge exercise (2D arrays)
Solutions
Solution to the Vocareum coding exercise are available here. ONLY look at the solutions after the lab period and after struggling to figure it out on your own. By looking at the solutions before struggling with how to do it yourself, you lose a majority of its benefit.
Open-ended Help
Any students with questions may stay after to get help. Anyone else may leave at this stage.