C++ Intro

hw 5: pointers


Description

The purpose of this assignment is to experiment with C++ pointers.

Instructions

Think about a C++ program that might use pointers. For example, the following utility (download) changes a string of characters and prints the result:


// Utility to reverse a string of characters provided by
// the user on the command-line and print the result.
#include <iostream>

void swap( char* front, char* back )
{
    char temp = *front;
    *front = *back;
    *back = temp;
}

int main( int argc, char* argv[] )
{
    if ( argc != 2 ) {
        // Incorrect number of command-line arguments:
        std::cout << "\tUsage:\n\t\t" << argv[ 0 ] << " TEXT\n";
        return 1;
    }

    // Create a placeholder for the text to process:
    char buffer[ 1024 ] = {0};
    strcpy( buffer, argv[ 1 ] ); // copy source text into the buffer

    // A couple of pointers to point to the beginning and ending
    // of the string that is about to be reversed:
    char* pfront = buffer;
    char* pback = buffer + strlen( buffer ) - 1;

    // Do the required work: swap pairs of characters pointed
    // by pfront and pback pointers:
    while ( pfront < pback ) {
        swap( pfront, pback );
        ++pfront; // Move front pointer forward
        --pback;  // Move back pointer backward
    }

    // Display the result and exit:
    std::cout << buffer;
    return 0;
}

Your assignment is to write your own program that uses at least one C++ pointer in its implementation. While writing your code, feel free to dig up ideas from your textbook or the internet.

For example, a good number of Windows API functions is available to a console application. One of the APIs is a Beep function, which generates simple tone on the system speaker (not using the sound card.) The following C++ program demonstrates a loop of 50-millisecond Beeps with rising tone frequency:


#include <iostream>
#include <windows.h>

int main()
{
    int ms = 50; // sound duration in milliseconds
    
    for ( int frequency = 100; frequency < 1000; frequency += 10 ) {
        std::cout << frequency << " "; // display sound frequency in hertz before each tone.
        Beep( frequency, ms );
    }
    return 0;
}

Write a program that takes text entered as command-line argument, and converts it to a series of sounds generated by Beep calls with alternating tones and frequencies. You may also insert pauses between the tones by using the Sleep API function:

        Sleep( milliseconds );

Your task is to create a function that takes a pointer to a character and converts it to a corresponding sound. The program should invoke this function for each word or phrase specified by the user on the command line. For example,

        C:\>hw5.exe "This program can talk"

One way to make this exercise interesting is to try to distinguish between vowel and consonant phonetic sounds. For example, consonants could be emulated by lower tone, shorter sounds; vowels could be producing a higher tone, longer sounds. It will take some experimentation to make the program generate entertaining output. Have fun!