C++ question

homeyg

Deity
Joined
Jan 12, 2004
Messages
3,631
Code:
//ArrayDemo - demonstrate the use of arrays
//            by reading a sequence of integers
//            and then displaying them in order
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

//prototype declarations
int sumArray(int integerArray[], int sizeOffloatArray);
void displayArray(int integerArray[], int sizeOfloatArray);

int main(int nNumberofArgs, char* pszArgs[])
{
    
    //input the loop count
    [COLOR=Blue]int nAccumulator = 0;[/COLOR]
    cout << "This program sums values entered "
         << "by the user\n";
    cout << "Terminate the loop by entering "
         << "a negative number\n";
    cout << endl;
    
    //store numbers into an array
    int inputValues[128];
    
    int numberOfValues;
    for(numberOfValues = 0;
        numberOfValues < 128;
        numberOfValues++)
    {
        //fetch another number
        int integerValue;
        cout << "Enter next number: ";
        cin  >> integerValue;
        
        //if it's negative...
        if (integerValue < 0)
        {
            //...then exit
            break;
        }
        
        //...otherwise sotre the number
        //into the storage array
        inputValues[numberOfValues] = integerValue;
    }
    
    //now output the values and the sum of the values
    displayArray([B]inputValues, numberOfValues[/B]);
    cout << "The sum is "
         << sumArray([B]inputValues, numberOfValues[/B])
         << endl;
         
    //wait until user is ready before terminating program
    //to allow the user to see the program results
    system("PAUSE");
    return 0;
}

//displayArray - display the members of an
//               array of length sizeOffloatArray
void displayArray([B]int integerArray[], int sizeOfArray[/B])
{
    cout << "The value of the array is:" << endl;  
    for (int i = 0; i < sizeOfArray; i++)
    {
        cout.width(3);
        cout << i << ": " << integerArray[i] << endl;
    }
    cout << endl; 
}

//sumArray - return the sum of the members of an
//           integer array
int sumArray([B]int integerArray[], int sizeOfArray[/B])
{
    int accumulator = 0;
    for (int i = 0; i < sizeOfArray; i++)
    {
        accumulator [COLOR=Red]+=[/COLOR] integerArray[i];
    }
    return accumulator;
}

I had a few questions about a sample program in a book about C++ I was reading (the one in my avatar). I had everything in the program pretty much figured out except for two things (and maybe a third). This is a program trying to explain how to use arrays and my questions aren't answered in the book. The first question: why at the beginning of main() do they define nAccumulator (blue) as 0 and then never again refer to it in the program? Question two: why do the arguments sent to the two functions defined at the bottom change their names(bolded)? And finally, question three: what does that += (red) symbol do in sumArray()? The book makes no mention of any of these things when explaining the program. This book is extremely confusing so much thanks to anyone who can help out.
 
Don't know C++, but I think I can answer the third question, at least if it's the same as in Java.

"x += y" is the same as "x = x + y"

I.e. add the value of the variable after the "+=" to the variable before the "+="
 
After checking the bolded part, I think that one of them is in the declaration of the method/function, and the other is a call for the function with certain values.

The part that begins with int or void is the declaration of the functions. The part in the main function is a call to the functions sending the array "inputValues" and the variable "numberOfValues" to the functions.
 
I agree with funxus -

int sumArray(int integerArray[], int sizeOfArray)
Is a function declaration.

void displayArray(int integerArray[], int sizeOfArray)
Is a method declaration.

These can handle any Integer arrays that you want to pass to them.

displayArray(inputValues, numberOfValues);
Is a call to the displayArray method.

You might have wanted to use more descriptive variable names, eg: you could call the method displayArray, passing it more descriptive arguments:
displayArray(CountOfCars, NumberOfCarCounts)

It then seemlessly 'converts' CountOfCars into integerArray and NumberOfCarCounts into sizeOfArray for use in the displayArray function. :)
 
I was suspecting that, but I wasn't exactly sure. Btw, what is the difference between a method and a function? And also, do you guys know of any good sites that teach C++? (This book isn't doing a good job of teaching something new while building on to previous stuff making it progressively more confusing.)
 
The first question: why at the beginning of main() do they define nAccumulator (blue) as 0 and then never again refer to it in the program?

Perhaps it is a placeholder - books like to reuse example programs by adding code to them. This way the readers are more familar with the growing amount of code and would feel more comfortable.

Or perhaps it is simply a careless mistake.

Question two: why do the arguments sent to the two functions defined at the bottom change their names(bolded)?

Once compiled, the variable names no longer exist. The change of variable names is for readability - since a method/function could be generic and used by many.

e.g. For a string append function, it is better to call the parameters "stringToAppendTo" and "stringToAppend":

char* stringAppend(char* stringToAppendTo, char* stringToAppend)

as it can be called in many ways,

e.g. char* fullName = stringAppend(firstName, lastName);
char* phoneNumber = stringAppend(areaCodeWithHyphen, phone);

And finally, question three: what does that += (red) symbol do in sumArray()?

A shorthand for increment, as answered by others. It makes no difference to the compiled code... so use the one you think is more readable.

The book makes no mention of any of these things when explaining the program. This book is extremely confusing so much thanks to anyone who can help out.

I've never read those "for dummies" books... I'm past that stage before any of them is published ;)
Since it is for dummies, it can't expect the readers to have experience in any other programming languages... either the book is really bad, or have you been skipping chapters?
 
the only resource i usually use for C++ is www.cppreference.com but that isn't exactly a tutorial. very useful though.

with the variable name change - the functions are not related. this is how it is in a main class, any functions other than main are going to be used in a static manner (don't know if that word has the same connotations in C++ as it does in java - but what kcwong said about generic use - we don't want to make it seem exclusive to the example we're currently using.). I'd say nAccumulator is a remnant left over from before they split the summation functionality into the sumArray() function.

as far as i know, the word "method" is the java equivalent of the C++ "function". i think java only uses "method" in any case. i wish i could be more certain of C++.

and i would have stayed away from a "for dummies" book. i have "C++ for java programmers" (since java is what i learned first), but didn't use it that much.
 
kcwong said:
Once compiled, the variable names no longer exist. The change of variable names is for readability - since a method/function could be generic and used by many.

e.g. For a string append function, it is better to call the parameters "stringToAppendTo" and "stringToAppend":

char* stringAppend(char* stringToAppendTo, char* stringToAppend)

as it can be called in many ways,

e.g. char* fullName = stringAppend(firstName, lastName);
char* phoneNumber = stringAppend(areaCodeWithHyphen, phone);

As I can see, those functions and variables above are using pointers (am I right?). I read the two chapters dealing with pointers in my book (that's where I was at anyway) and did not understand one bit of what they were trying to say.

kcwong said:
I've never read those "for dummies" books... I'm past that stage before any of them is published
Since it is for dummies, it can't expect the readers to have experience in any other programming languages... either the book is really bad, or have you been skipping chapters?

I haven't been skipping chapters. I've picked up a few things, so it can't be that bad of a book. I don't think I'm at the point where I could write any working programs (except for very simple ones). I haven't really had any experience in programming before, either (a little BlitzBasic, but that's not what I would call experience).

bobgote said:
and i would have stayed away from a "for dummies" book.

Yeah, I wish I had thought the same way. Although, The Complete Idiot's Guide to C++ got outstanding ratings on Amazon...
 
Yeah, a 'method' is a function that is the member of a class. However this terminology is used less often in the C++ community than in the Java community. 'Member function' is a common alternative, and it is common to simply call it a 'function'.

Functions that are not members of classes are sometimes called 'free functions' -- i.e. 'free' of binding to any class, although this can lead to confusion with the standard library function, free().

In some languages, there is a distinction made between a sub routine that doesn't return a value -- often called a 'procedure', and one that does -- called a 'function', but this terminology isn't common in the C++ community.

Sadly, there really aren't very many good "beginner's" books for C++. Accelerated C++ is decent, but only if you know at least one other programming language.

-Sirp.
 
homeyg said:
As I can see, those functions and variables above are using pointers (am I right?). I read the two chapters dealing with pointers in my book (that's where I was at anyway) and did not understand one bit of what they were trying to say.
you'll have to work out pointers as it is fairly fundamental.
 
Well, it's been a day, I *think I got the pointers down.
 
Back
Top Bottom