TIP: Click on subject to list as thread! ANSI
echo: nthelp
to: Geo
from: John Beckett
date: 2004-11-20 11:22:28
subject: Re: C++ for beginners?

From: John Beckett 

"Geo"  wrote in message
news::
> I think that's what really bothers me about it, the examples we do in class
> are like the most simple clear examples you could come up with, then
> sometimes for homework or sometimes for a test she comes up with this whacko
> stuff.

That problem really was whacko. I suspect that Paul and Gregg have totally
forgotten how baffling templates are when you first encounter them. Years
ago I did some C teaching (in the days before C++), and I know how the
simplest concept can be really hard for someone new.

> ...write a void class called sort and using a reference variable pass it an
array

Sounds a bit weird to me. The following is part of C lore:

void swap(int x, int y)
{
    int temp = y;
    y = x;
    x = temp;
}

Then you have a sort routine that calls swap when needed to put integers in
the correct order. The swap function of course is totally useless because
it swaps a COPY of the variables it is given. You can fix this by changing
swap to take references to x and y.

However, many people don't like using references in this way because
looking at a call like
    swap(var1, var2);
provides no clue that the values of var1 and var2 are being changed
(although taking this rather simple example, it is obvious that swapping
the variables is what is wanted).

Anyway, I am NOT a C++ theory expert, but I would sort an array by passing
the array "by value" - the way Ritchie intended! I don't see a
need to use a reference.

void sort(int array[], int size)
{
    // Code to sort array (insertion sort - please not bubble sort!).
    // In practice, call the library qsort function.
}

To use the above:
    int a[] = { 11, 5, 7, 17, 3 };
    int n = sizeof(a) / sizeof(a[0]);   // a clever way to say n = 5
    sort(a, n);

This passes the value of a which is the address of where the array is
stored (by definition, a = &a[0]). This point is a bit subtle when new,
but quite simple. No references are used.

John

--- BBBS/NT v4.01 Flag-5
* Origin: Barktopia BBS Site http://HarborWebs.com:8081 (1:379/45)
SEEN-BY: 633/267 270 5030/786
@PATH: 379/45 1 396/45 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.