Preliminary

Approach

Not going to pour knowledge into their heads (see slide).

Get to know each other with class sort exercise.

Use of group learning. Talk about why we are using group learning. Have students get together in groups and come up with ideas.

Resources for Review

Pascal to C tapes in the library.

Books, CS1005 book, any book that you can find.

Appendix A in text discusses conversion from C to C++.

Review

Expect you to know (embodied in C version of handout program):

What I expect you have seen, but may not know well:

What I expect you to learn in converting from C to C++ (summarized in C++ program handout and Appendix A):

What I expect you not to have seen:

/*
 * /cs/cs2005/pub/example/poly.c -- read and print a polynomial
 *
 * programmer -- Craig Wills
 *
 * date -- October 28, 1996
 *
 * modification history
 */

#define MAXCOEFF 10       /* maximum number of coefficients */

#include <stdio.h>

#define FALSE 0
#define TRUE 1
typedef short Boolean;

typedef float CoeffList[MAXCOEFF];   /* for storing the list of coefficients */

/* function prototypes */
void ReadPoly(CoeffList, int *);
void PrintPoly(CoeffList, int);


void main()
{
    CoeffList rgCoeff;
    int wDegree;
    
    /* read in and print a polynomial */
    ReadPoly(rgCoeff, &wDegree);
    PrintPoly(rgCoeff, wDegree);
}

/*
 * ReadPoly -- read and store the contents of a polynomial along with its
 *     degree.
 */
void ReadPoly(CoeffList rgCoeff, int *pwDegree)
{
    Boolean bOk;                /* is the degree ok? */
    int i;

    /* obtain the degree of the polynomial */
    bOk = FALSE;
    while (!bOk) {
        printf("What is the degree of your polynomial? \n");
        scanf("%d", pwDegree);
        if ((*pwDegree < 0) || (*pwDegree >= MAXCOEFF))
            printf("%d is not in the allowed degree range of 0-%d\n", 
                   *pwDegree, MAXCOEFF-1);
        else
            bOk = TRUE;
    }

    /* obtain the coefficients of the polynomial */
    for (i = *pwDegree; i >= 0; i--) {
        printf("Enter the coefficient for the degree %d term: ", i);
        scanf("%f", &rgCoeff[i]);
    }
}    

/*
 * PrintPoly -- print the contents of a polynomial given the coefficient
 *    list and the polynomial degree (in a rather ugly manner).
/*
void PrintPoly(CoeffList rgCoeff, int wDegree)
{
    int i;

    printf("f(x) =");
    for (i = wDegree; i >= 0; i--) {
        printf(" + %fx^%d", rgCoeff[i], i);
    }
    printf("\n");
}
What is the degree of your polynomial? 
3
Enter the coefficient for the degree 3 term: 2
Enter the coefficient for the degree 2 term: 1
Enter the coefficient for the degree 1 term: 3
Enter the coefficient for the degree 0 term: 4
f(x) = + 2.000000x^3 + 1.000000x^2 + 3.000000x^1 + 4.000000x^0

Use of C++

Illustrated in poly.C. Note that extensions C, cc and cpp can be used to indicate C++ code.

Can use // for comments. Comment extends from these symbols to the end of the line.

cin and cout are part of the streams library in C++. They are used for input and output. Associated with their use are the extraction operator >>, for input, and insertion operator <<, for output.

These operators are overloaded so they work on different types and extract or insert information based on the type of the operand.

/*
 * /cs/cs2005/pub/example/poly.C -- read and print a polynomial in C++
 *
 * programmer -- Craig Wills
 *
 * date -- October 28, 1996
 *
 * modification history
 */

#define MAXCOEFF 10       // maximum number of coefficients

#include <iostream.h>

#define FALSE 0
#define TRUE 1
typedef short Boolean;

typedef float CoeffList[MAXCOEFF];   // for storing the list of coefficients

// function prototypes
void ReadPoly(CoeffList, int &);
void PrintPoly(CoeffList, int);


void main()
{
    CoeffList rgCoeff;
    int wDegree;
    
    /* read in and print a polynomial */
    ReadPoly(rgCoeff, wDegree);
    PrintPoly(rgCoeff, wDegree);
}

/*
 * ReadPoly -- read and store the contents of a polynomial along with its
 *     degree.
 */
void ReadPoly(CoeffList rgCoeff, int &wDegree)
{
    Boolean bOk;                // is the degree ok?
    int i;

    // obtain the degree of the polynomial
    bOk = FALSE;
    while (!bOk) {
        cout << "What is the degree of your polynomial? \n";
        cin >> wDegree;
        if ((wDegree < 0) || (wDegree >= MAXCOEFF))
            cout << wDegree << "is not in the allowed degree range of 0-"
                << MAXCOEFF-1 << "\n";
        else
            bOk = TRUE;
    }

    // obtain the coefficients of the polynomial
    for (i = wDegree; i >= 0; i--) {
        cout << "Enter the coefficient for the degree " << i << " term: ";
        cin >> rgCoeff[i];
    }
}    

//
// PrintPoly -- print the contents of a polynomial given the coefficient
//    list and the polynomial degree (in a rather ugly manner).
//
void PrintPoly(CoeffList rgCoeff, int wDegree)
{
    int i;

    cout << "f(x) =";
    for (i = wDegree; i >= 0; i--) {
        cout << " + " << rgCoeff[i] << "x^" << i;
    }
    cout << "\n";
}
What is the degree of your polynomial? 
3
Enter the coefficient for the degree 3 term: 2
Enter the coefficient for the degree 2 term: 1
Enter the coefficient for the degree 1 term: 3
Enter the coefficient for the degree 0 term: 4
f(x) = + 2x^3 + 1x^2 + 3x^1 + 4x^0

Parameter Passing and Pointers

C

In C all parameters are passed by value meaning that changes to the parameter within the procedure are not reflected in the caller. To return values through the parameters in C, you must pass the address of a variable.

Meaning of * and &.

int *pa; /* the contents of pa is an integer, hence pa is a ptr to an int */
int b;     /* b is an int */

*pa not valid  (segmentation violation)
pa = &b;
b = 5;
*pa is now valid and is 5

Looking at pwDegree in ReadPoly() in poly.c we see that what is passed as an address of a variable looks to the procedure like a pointer.

Why does scanf() need the address of its variables?

reference to an array passes its address.

C++

In addition to call-by-value, C++ allows a programmer to define parameters as call-by-reference. See the parameter wDegree in ReadPoly() in poly.C.

Strings

By default in C/C++ a string is not a separate type, but rather it is an array of characters that is terminated by a NULL character. An array of characters that is not terminated by a NULL character is just an array of characters.

C/C++ provides functions for handling strings. Can print them using %s as a formatter. Can also manipulate them with functions.

/*
 * /cs/cs2005/pub/example/string.C -- string examples in C++
 *
 * programmer -- Craig Wills
 *
 * date -- October 30, 1996
 *
 * modification history
 */

#include <iostream.h>
#include <string.h>

void main()
{
    char rgch[20];
    char sb[20];      /* same declaration */
    char sb2[20] = "hello world";  /* can initialize the string */
    char *pch;                 /* character pointer */

    /* standard string operations */

    /* strlen -- return number of characters in string */
    cout << "The length of " << sb2 << " is " << strlen(sb2) << "\n";

    /* strcpy -- copy second string into the first */
    strcpy(sb, "hello again");   /* string copy */

    /* strcat -- concatenate second string onto the first */
    strcat(sb, " again");

    /* strcmp -- compare contents of two strings, return 0 is the same */
    if (strcmp(sb, "hello") == 0)   /* string compare */
        cout << "strings are the same with strcmp\n";

    /* strncmp -- compare contents of two strings for some number of chars */
    if (strncmp(sb, "hello", 5) == 0)
        cout << "strings are the same with strncmp\n";

    /* string manipulation */
    pch = sb;            /* sb is the address of the array */
    while (*pch != ' ')  /* single quotes for chars */
        pch++;
    *pch = '\0';
    pch++;
    cout << pch << ", " << sb << "\n";
}

> string
The length of hello world is 11
strings are the same with strncmp
again again, hello

Referring to an array (specifically a string) always refers to the address of that array.

What happens when comparing string pointers directly? (compares addresses not contents).