Lab 7 - Pointers and Functions

Due: Friday by 5:00pm
NOTE: This lab will take place on Wednesday October 27, 2010 with a guest instructor. On Tuesday October 26th, we will have a lecture on this material.

The purpose of this lab is to write functions that compare pointer arithmetic to using array subscripts. Name your file lab7.cpp.

You are allowed to work in groups of 2 for this assignment. Remember to put the names of both group members in the comment section of the code and in the submission email.

For this lab, you will use the following code snippet and code the function bodies for the functions convert_index and convert_pointer. You can also view this code seperately in lab7_handout.cpp. You can copy this code to your Sleipnir account using vi set paste command for copy/paste, using wget to download from the website or using cp to copy from my directory. See the notes in Lab 6 for how to use the cp command.

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

// Input: A character array that terminates with '\0'
//        An integer indicating the first index to convert
//        An integer indicating the index one after the last index to convert
// Output: Converts the lowercase characters between the indices to uppercase
void convert_index(char s[], int start, int end);

// Input: A pointer to the first character to convert
//        A pointer to the character one after the last character to convert
// Output: Converts the lowercase characters between the pointers to uppercase
void convert_pointer(char *start, char *end);

int main()
{
  char str[51];
 
  strncpy(str, "This is a test.", 50);
  cout << "Original string: " << str << endl << endl;

  convert_index(str, 0, 5);
  cout << "After index convert: " << str << endl << endl;

  convert_pointer((str+5), (str+10));
  cout << "After pointer convert: " << str << endl << endl;

  return 0;
}
Both functions will use a for() loop to check if the character is lowercase and, if so, convert it to upper case. The convert_index function will use the subscript operator (e.g. s[i]) to access each element. The convert_pointer function will use pointers to access each element. Note that your for loops should treat the ending point as non-inclusive, in other words, exit the loop when you reach the ending index or ending address.

There should be no cin or cout statements inside your function bodies. The main function should also remain unchanged. All you will add is the function bodies below main.

Email the completed lab7.cpp to me on Sleipnir.