arkoenig 340 Practically a Master Poster

Subtract.

arkoenig 340 Practically a Master Poster

In your program, nums is just a pointer to the first element of array1.

If your assignment includes a requirement to allocate a new array, why not start by doing that and then figuring out what to do with it?

arkoenig 340 Practically a Master Poster

Please show the full contents of your input file, as well as exactly what the program prints when you run it. Do not try to interpret or explain what the program does. Just the facts, please.

arkoenig 340 Practically a Master Poster

OK, then I take it you don't need any more help or advice. See you around.

arkoenig 340 Practically a Master Poster

Please show the entire program, and explain exactly what you did in order to run it.

It works fine over here, so I am thinking that something is odd about how you're running it.

arkoenig 340 Practically a Master Poster

So apparently your implementation doesn't do anything to an output file unless you try to write something.

arkoenig 340 Practically a Master Poster

myfile is an ofstream, which means that you are intending to use it for output. Calling myfile.open("exercise.txt") says that you intend to write a file named exercise.txt -- which means that anything that was in that file already is about to vanish.

arkoenig 340 Practically a Master Poster

Your array has 20 columns. When you use puts to read a string, it puts a null character ('\0') at the end of its input. So if your input is 19 characters or longer, the input (including the null character) runs off the end of the row of the array and onto the next row.

So this program fails any time it gets more than 19 characters of input.

More generally, this failure is an unavoidable aspect of using gets: The gets function takes a pointer to memory, and simply overwrites as much memory as necessary to hold its input. Because you don't know how much input you're going to get, you don't know how much memory it is going to overwrite.

As a consequence of this behavior, you should never use gets in your programs for any reason. Because it overwrites memory unpredictably, any program that uses gets has security problems that are impossible to fix. For that reason, I think that if you are using a book that recommends the use of gets, you should consider using a different book.

arkoenig 340 Practically a Master Poster

Read a file that contains text, in the form of words separated by whitespace (spaces, tabs, or newlines).

1) Print the longest and shortest words.
2) Print each word, together with how many times it occurs.
3) Print every word that occurs only once.

arkoenig 340 Practically a Master Poster
for (int i = 0; i < 1; ++i) {
    for (int j = 0; j < 1; ++j) {
        cout<<"_ _ _ 1" << endl;
        cout<<"_ _ 2 2" << endl;
        cout<<"_ 3 3 3" << endl;
        cout<<"4 4 4 4" << endl;
    }
}
Nick Evan commented: Haha :) +16
arkoenig 340 Practically a Master Poster

Complexity is a word used to describe an attribute of the process by which you understand a C++ program. This process is called complex because it has a real part and an imaginary part.

arkoenig 340 Practically a Master Poster

The difference between int main() and void main() is that the first one is allowed in C++ and the second isn't.

You're going to say "But my compiler accepts it!"

And I'm going to reply "So? Compilers often accept code that's not valid. This fact doesn't make the code valid."

Ancient Dragon commented: Yes :) +36
arkoenig 340 Practically a Master Poster

And now that it's in the C++ forum, I can point out that the title of this thread talks about printing the reverse of a number, but the program computes the sum of its (decimal) digits. Which is right?

arkoenig 340 Practically a Master Poster

No; you don't need to implement BigNum. You can use someone else's implementation instead.

arkoenig 340 Practically a Master Poster

The last time you asked for someone else to do your homework for you, I directed your attention here.

I do not think you actually read it, because if you did, you would not be asking again for someone to do your homework for you.

So please, do us all a favor: Read the thread I've just cited, then follow its instructions and come back after you have done so.

jonsca commented: It's more like the third or fourth. +6
arkoenig 340 Practically a Master Poster

The declaration

Complex c[a];

isn't valid C++, though some compilers will accept it.

Those that do will presumably take it to mean a request to define an array with "a" elements. Those elements will have indices ranging from 0 through a-1. When you subsequently execute

c[a].setComplex(temp1, temp2);

you will be trying to access the array element with index "a", which is out of bounds.

If that's not enough, consider that line 4 of your code sets "a" to 0, so when you define c in line 7, it will not have any elements at all. Therefore, any attempt to access an element of c will be out of bounds. So when you say that you're trying to access c[0], c[1], and so on, it doesn't surprise me that it doesn't work--none of those elements exist.

arkoenig 340 Practically a Master Poster
transform(object.begin(), object.begin(), //Only one parameter within that range
           tolower);

This example doesn't work, for two reasons:

1) If you pass object.begin() twice as a range, what you get is the empty string immediately before the first character (if any) in object.

2) You can't actually pass tolower as an argument to a library function because tolower itself is overloaded.

One way to accomplish the same end that does work, and (in my opinion) is easier to understand:

if (!object.empty())
    object[0] = tolower(object[0]);
Red Goose commented: Thanks for pointing that out. +1
arkoenig 340 Practically a Master Poster

You only look at the first character of each word.

arkoenig 340 Practically a Master Poster

There isn't a set of published answers for Accelerated C++ because Barbara's and my teaching experience is that if students have the answers already available, they don't try to solve the problems for themselves.

Typically, if you don't know for sure whether your answer is right, it probably means you don't understand the subject matter well enough; in which case, it doesn't matter much whether the answer is right or wrong. Still, I can understand why you might have nagging doubts; so I'll underscore what firstPerson said.

arkoenig 340 Practically a Master Poster

Your array "choices" has 6 elements. That means that the first element is choices[0] and the last element is choices[5]. So lines 30 and 31 are invalid, because they attempt to access an element of choices that does not exist.

arkoenig 340 Practically a Master Poster

It seemingly crashes when it tries to get the value at getTile(0, 1).
getTile(0, 1) would mean tile[(16*1*320)+(0*16)] = tile[5120], with the image I'm working on, tile should be an array of 51200 cells, it should certainly be in range, I still have no idea what makes it crash.

When you say "it should certainly be in range," that says to me that you do not actually know whether it is in range. That's why I'm suggesting you modify getTile so that it does a range check.

arkoenig 340 Practically a Master Poster

I'm sorry, but I don't understand the question. You can't pass an array to a function; you can only pass a pointer to one of its elements (usually the first one). So for example, if you execute the following:

int* p = new int[n];
fun(p);
delete[] p;

you are allocating an array with n elements and setting p to point at its first element. Then you call fun with that pointer as its argument. Finally, after fun returns, you deallocate the array and return its memory to the system.

If this doesn't point you in the direction of doing what you want, then I'm afraid you'll have to explain it more clearly.

arkoenig 340 Practically a Master Poster

When you say "Both lines work on their own if the other is removed from the code, and both functions used are not making any of the problems themselves," I am skeptical.

Usually, if a program crashes when doing A and B, and it seems to work when doing A alone or B alone, what it really means is that there is a problem in A, B, or both, but the problem isn't visible until you happen to run both of them together.

I think I would start looking for the problem by putting extra code everywhere there is a computed array reference (for example, in getTile) to verify that the index you compute is actually within the bounds of the array. Who knows? You might get a surprise.

arkoenig 340 Practically a Master Poster

Let me elaborate on my excessively terse answer before.

When you create an array, you have to say how many elements it has, and those elements are all constructed. That means that the type needs a constructor.

When you create a vector, you have two additional options. First, you can create a vector without any elements. If it has no elements, there is no need to construct them. Second, you can create a vector with a given number of elements, all of which are copies of a value that you supply. In neither case do you have to create an element out of nothingness.

So if you can get by with using a vector instead of an array, it is apt to be the easiest way to solve your problem.

arkoenig 340 Practically a Master Poster

You know what? When you say that you did not ask anyone to do it for me, you are technically correct--but I don't believe you.

Look at what you posted. It was a paragraph that is expressed in a way that looks like it was literally copied from a homework assignment, and it said nothing about what kind of assistance you were seeking. So there are two possibilities:

1) You wanted someone to finish the incomplete solution that you posted, or

2) You wanted someone to read your mind and figure out what question(s) you wanted to ask.

But now that you've asked a question, I'll answer it. You asked "which array function will I need to use to store the original inputs from the user."

Well, C++ doesn't have anything called an array function, so far as I know, so I'm going to guess that you mean which of the functions you listed at the beginning of your example. Let's take a look at those functions' names and signatures, and see if we can figure out what they ought to do.

First, there's initArrays. It takes two pointers and an int (when you declare a function parameter with [], it's really just an alternative way of declaring a pointer). I think it is a reasonable guess that the int is the size of the two arrays, that both arrays are the same size, and, from its name, the function is supposed to initialize …

arkoenig 340 Practically a Master Poster

Not that I can think of. Use a vector instead.

arkoenig 340 Practically a Master Poster

@jkoske: You know what the question is. It's "I've done a little bit of my homework; will you do the rest for me?" The answer is (or should be) no.

arkoenig 340 Practically a Master Poster

You have (at least) two problems.

The first one is that the expression test % p in line 27 should be p & test.

The second is more subtle. Look at the loop in lines 23-32. You will see that the only way that your prime function can ever return anything but 0 is if the variable "test" reaches 0.

However, before it reaches 0, it has to reach 1--and when it does, line 27 will compute p % test (after you've fixed it) with test equal to 1, which will always be zero. So the function will always return zero.

I'm not going to tell you how to solve this problem, because I've already done enough of your homework for you.

arkoenig 340 Practically a Master Poster

That's because you have the same problem in your prime function: You never change the value of the variable test, so the program never terminates.

arkoenig 340 Practically a Master Poster

You never change the value of your variable "current," so your program can never terminate.

arkoenig 340 Practically a Master Poster

Your statement

int Size = sizeof Array / sizeof Array[0];

will not work if Array is a formal parameter, because in that case, Array is simply a pointer and sizeof Array is the size of a pointer. If you want to use an array parameter to represent an array, you have to pass the size separately.

arkoenig 340 Practically a Master Poster

It looks to me like you have an extra "if" and an extra pair of { } in line 30.

That is, lines 29-30 should read

else if (discriminant==0)

arkoenig 340 Practically a Master Poster

Every time the compiler prints an error message during compilation, that message includes the number of the line with the problem.

arkoenig 340 Practically a Master Poster

If by "best" you mean the closest to 12 without being greater than 12, then what you have just described is a version of the knapsack problem, which is known to be hard to solve efficiently.

arkoenig 340 Practically a Master Poster

Use code tags. Without them, all your indentation goes away, which makes your code very hard to read.

If your book really contained the line

delete [] x,a,b,c,beta,gamma,r;

then I suggest you stop using that book, because the author doesn't know C++ very well. Ditto for the overall design of the program.

That said, the first place I'd look for your error is in the line

x(n-1)=gamma(n-1);

which surely should be

x[n-1]=gamma[n-1];

if that's not the only problem, please repost the code using code tags, and please tell us which line or lines the compiler said was in error.

arkoenig 340 Practically a Master Poster

Who knows? You certainly have not pasted all the relevant code, because you haven't told us what the value of the variable "i" is or what the noBucket function does.

I will say that your use of "i" makes me suspicious, because it's not a variable in your class. By implication, it's probably a global variable--what else could it be--and any program with a global variable named "i" is cause for suspicion all by itself.

arkoenig 340 Practically a Master Poster

If you are going to use the accumulate function to add floating-point numbers together, the third argument has to be 0.0, not 0

arkoenig 340 Practically a Master Poster

If you know how to do most of it, how about doing the part you know how to do, posting the code, and asking for advice on how to do the rest?

arkoenig 340 Practically a Master Poster

And if i understand you correctly then this additional exit condition would serve as an alternative break point from the inner loop after my current check?

No, instead of your current check. Once 2 is in the prime vector, the next number to be tested is 3. 2 * 2 is 4, which is strictly greater than 3; so you won't run off the end of the vector even when you're testing 3. Once you've gotten 3 safely onto the prime vector, 5 and subsequent primes are a piece of cake.

So here's one way you might write it:

// This assert should succeed because we pushed 2 onto primes
assert (!primes.empty());

primeList_t::iterator iPrime = primes.begin();
while (*iPrime <= limit && (x % *iprime) != 0) {
    ++iPrime;

    // This assert should work because the square of the last element
    // of primes is strictly greater than x, so the test *iPrime <= limit
    // should ensure that iPrime never referred to the last element of primes
    assert (iPrime != primes.end());

}

if (*iPrime > limit)
    primes.push_back(x);
arkoenig 340 Practically a Master Poster

Since when is 4 prime? Isn't that what you're claiming in line 20 of your code?

Also, if you care about execution time, one easy way to speed things up is to observe that you don't have to divide a candidate by every prime; just by the ones that are <= the square root of the candidate.

You have to be a little bit careful about this to ensure you don't inadvertently treat a perfect square as a prime because of rounding error in the square root, but an easy way to do that is compute the square root, round it, and then increment the rounded square root until its square is greater than the candidate. In other words:

unsigned long limit = sqrt(x);
while (limit * limit < x)
    ++limit;

Now you know that limit is >= sqrt(x) even if there are rounding problems, and you can use iPrime<=limit as the ending condition for your inner loop.

arkoenig 340 Practically a Master Poster

I'm going to turn the question around: You can have a pointer to int, or a pointer to double, or a pointer to a class object, or a pointer to a function, or a pointer to an object of any of a bunch of other types I've left out. What do you think is so special about pointers that you wouldn't want to allow a pointer to point to one?

arkoenig 340 Practically a Master Poster

It's probably better to read and write the entire object.
f.read( ( char* )aClassObject, sizeof( aClass ) );
f.write( ( char* )aClassObject, sizeof( aClass ) );

Don't even think about it. This technique will not work if the object in question contains any members of type string, or vector, or any other type that manages any auxiliary memory.

arkoenig 340 Practically a Master Poster

When you call getTile, you get a newly created value of type Tile. Your statement

project._p->it->getTile(x, y).CPosX = panel2->Location.X;project._p->it->getTile(x, y).CPosX = panel2->Location.X;

calls getTile to obtain such an object, then changes the CPosX member of that object, and finally throws the object away. Fortunately, C++ doesn't let you do that; because if it did, you'd never find your problem :-)

I am guessing that you want to make your getTile function return a Tile& rather than a plain Tile.

arkoenig 340 Practically a Master Poster

What I mean is that no one is going to do your work for you. So if you show us something you've done and ask "How do I go about adding feature X to it," someone might help you; but if--as you did--you say "I want feature X in my program; pleae do it for me," you're not going to get very far.

arkoenig 340 Practically a Master Poster

What have you done so far?

arkoenig 340 Practically a Master Poster

I'm sorry, but saying "this 7 and 5 allocation must also be random" is not a specification. In general, you have to say much more than just using the word "random" in order to make it clear what problem you're trying to solve.

For example, suppose I tell you that I want a variable to have a random floating-point value between 0 and 1. Is it acceptable to toss a coin and set that variable to 0.1 for heads and 0.9 for tails? Why or why not?

What if I pick two random integers and use them as the value for the fraction and exponent, subject to the restriction that the exponent will never be so large that it causes the number to be greater than 1. Is that approach acceptable? Even though it will cause the distribution of the values to be heavily skewed toward very small numbers?

I ask these questions because in your most recent message, you say two things that cannot both be true at once: (1) You want a 3x4 random matrix; (2) The elements of the matrix must be mostly zero.

If most of the elements are zero, then it's not random!!! So it won't do to say that it is random; you have to say just what characteristics you want it to have. And until you can explain what those characteristics are, there is nothing that anyone else can do to help you.

arkoenig 340 Practically a Master Poster

Where's the definition for SListIterator?

arkoenig 340 Practically a Master Poster

You can also define a map<string,int> that you can use to look up a string, obtain a corresponding integer, and use that integer in a switch. Depending on how you write your code, that strategy may well run faster, and be easier to maintain, than using a series of if-then-else statements.

arkoenig 340 Practically a Master Poster

You have not specified the problem fully enough to make it possible to think about how to solve it.

For example, first you say that the program should randomly generate 25 numbers, and then you say that "almost 15 or 16 numbers" (whatever that means) should be zero.

It is hard to understand how to meet these requirements at the same time. For example, it is acceptable to begin by deciding which elements will be zero and which ones nonzero? Are there any requirements on the distribution of the nonzero elements aside from the requirement that no row or column be entirely zero? Is the following algorithm acceptable?

For each row, pick one column at random that has not already been picked and remember what it is. If you make those elements nonzero, it will satisfy the aforementioned requirement, but the choice of elements will hardly be random, because exactly one element will be nonzero in each row or column.

Until you are certain that you understand the problem, there is little to gain in looking for solutions.

arkoenig 340 Practically a Master Poster

How about getting your program to compile and execute, then posting the version you executed along with its output, and explaining what it's doing that you don't understand?

If you have never written a C++ program that has actually compiled successfully, then you shouldn't be trying to write one this big for your first effort. If, on the other hand, you have written a program that has compiled successfully, then you should be capable of getting this one to compile too.