arkoenig 340 Practically a Master Poster

I'm sorry, but I do not see any connection between my question and your answer.

You wrote the following statement:

file_read >> country_name[20] >> happiness_values;

This statement is equivalent to the following two statements:

file_read >> country_name[20];
file_read >> happiness_values;

I am asking about the first of these two statements.

What were you trying to do when you wrote that statement?

arkoenig 340 Practically a Master Poster

What do you think the expression country_name[20] means?

arkoenig 340 Practically a Master Poster

Line 27 of your original code sets the variable sizeA to 0. Line 30 calls the function getarrays, passing a reference to sizeA. Nowhere in the getarrays function do I see any code that ever changes the value of sizeA.

So on return from getarrays, sizeA is still 0. I doubt that's what you have in mind.

The same reasoning applies to sizeB.

I suggest you break this program into smaller pieces and test each piece before you try to combine them.

arkoenig 340 Practically a Master Poster

One observation:

string* sp;
sp = new string[n];     // every element of the array is initialized to a null string
int* ip;
ip = new int[n];        // the elements of the array are not initialized

This behavior might explain your problem.

If it doesn't, there isn't anything more I can say. You posted code that doesn't work, and say "My real code is just like this, except that it's perfect, and I won't show it to you because it's too long; but I want you to tell me what's wrong with it."

Sorry, but that kind of magic is above my pay grade.

arkoenig 340 Practically a Master Poster

I don't think you read either of the last two messages in this thread. Because if you did, you would understand why the very first statement in your example:

char* nstring="C:\Program Files\C++\myfile.txt";

simply does not work.

The rest of your code does not work either. The statement

nstring << nstring + "\\";

is just plain nonsense. If you're writing code like this, it suggests that you don't understand anything about how C++ expressions work.

I don't mean to sound unkind, but I think you should spend some time studying a good C++ textbook before asking other people for advice. It is very inefficient to try to learn by asking questions about material you don't understand in the first place.

arkoenig 340 Practically a Master Poster

The code you posted cannot possibly compile for several reasons, including:

1) You never defined the variables n or price2.

2) The variables price and price2 do not have definitions in scope in the functions apple and orange.

If you post code that does not compile, and you complain that the code does not do what you want when you run it, you are essentially telling everyone that the code you posted is not the code you ran--because you could not have run it as it did not compile.

Why should we bother trying to debug code that we know is different from the code with which you claim to be having problems?

arkoenig 340 Practically a Master Poster

In order to get a \ character into a string, or to write one as a char literal, you need to put two consecutive \ characters as part of your program between quotes. Examples:

char c;
c = '\';                 // Wrong.
c = '\\';                // Right -- c now contains a (single) backslash.

char* s;
s = "a\b\c";             // Wrong.
s = "a\\b\\c";           // Right -- s now points to the initial char of a
                         // 6-char array.  The six chars are a, backslash, b,
                         // backslash, c, null.
arkoenig 340 Practically a Master Poster

When you execute

int *intersection = new int[inter];

and then you change the value of inter, that does not change the number of elements in the block of memory to which intersection points.

That is exactly the assumption you are making in lines 5 and 14, respectively, of the third segment of code you cited.

I hate to be the bearer of bad news, but if you're writing code like this, it shows that you really don't understand how memory allocation works, and you're unlikely to acquire that understanding between now and when your homework is due.

arkoenig 340 Practically a Master Poster

You can't have an array of size 0, in the sense that every array type includes the number of elements as part of the type, and for the type to be well defined, the number of elements must be strictly positive. Part of the reason for this requirement is to be able to guarantee that sizeof(x) is nonzero regardless of the type of x.

So your second example is not well-formed, although I imagine there might be a compiler out there that accepts it.

Your first example, on the other hand, is fine. There is nothing wrong with using "new" to create a pointer to "the first element" of a 0-element dynamically allocated array. Of course, that first element does not exist, so the pointer is an off-the-end pointer for that array. It's a valid pointer; it's guaranteed to be distinct from other pointers; and you should delete it at some point if you wish to avoid memory leaks. What you can't do is dereference it, because it doesn't point to an element. That's true of any off-the-end pointer, not just this one.

arkoenig 340 Practically a Master Poster

This is for my Data Structures class. This is all we were given:

I understand what your assignment is.

What I am asking you to do is to explain the program that you wrote.

You say that it is producing results that you do not expect.

I am asking you what results you expect the program to produce, and why you expect the program to produce those results. An answer to that question would be something like:

"Well, the first statement sets this variable to this value, and then the next statement prints that variable, so I would expect it to print the value of the variable" and so on.

jonsca commented: Yep +5
arkoenig 340 Practically a Master Poster

What do you expect it to do and why?

arkoenig 340 Practically a Master Poster

What do lines 20-22 do? They don't seem to correspond to any part of the problem specification.

In particular, they seem to say that A(0,1) is equal to A(1,1). I see no justification for that claim.

arkoenig 340 Practically a Master Poster

By the way, many people who use vectors also use the push_back function. For example:

vector<int> x;
int n;
while (cin >> n)
    x.push_back(n);

Calling x.push_back(n) pushes a copy of n onto the back of x. That is, it increases the size of x by 1 and uses n to initialize the newly created element.

This technique makes it unnecessary to know in advance how many elements the vector will have.

arkoenig 340 Practically a Master Poster

It's defining a variable named x of type vector<int> and using the value of corners to initialize it. When you use an integer to initialize a vector, that integer determines the number of elements the vector has.

arkoenig 340 Practically a Master Poster

Don't do it -- it's a local extension and not part of C++. Do this instead:

int corners;
cout << "Please enter the number of corners: ";	cin >> corners;
vector<int> x(corners);    // Note: parentheses, not square brackets

You have to say

#include <vector>

for this to work.

arkoenig 340 Practically a Master Poster

What mike_2000_17 said.

Also: The moment you allocate a resource in a constructor and use a pointer to keep track of it, you have just obligated yourself to do something about a copy constructor and copy-assignment operator. Because if you don't, then copying an object of your class will copy the pointer, and you will have two pointers pointing at the same resource.

Now when you destroy one of the copies of your object, its destructor will either free the resource, in which case destroying the other copy will try to free a resource that has already been freed, or it won't free the resource, which will result in a resource leak.

arkoenig 340 Practically a Master Poster
arkoenig 340 Practically a Master Poster

You don't need any of the code you've shown.

arkoenig 340 Practically a Master Poster

Your vector 'quantity' is uninitialized, which means you are trying to print garbage.

Not true. If you create a vector of integers and do not initislize it, the integers are initialized to 0.

More generally, if you create a vector of objects of type T, the initialization process creates an object initialized with the value T(), then copies that object into each element of the vector.

arkoenig 340 Practically a Master Poster

Make a random integer between 50 and 99999, then divide it by 100.0

arkoenig 340 Practically a Master Poster

Show us what you have so far and explain what kind of difficulty you're having, and maybe someone will help.

arkoenig 340 Practically a Master Poster

Two suggestions:

1) Learn how to use code tags; without them, your code is very hard to read.

2) Pick one function from the collection of functions described in your assignment and implement that. Post your implementation.

arkoenig 340 Practically a Master Poster

First you need to explain what you expect the code to do.

Line 13 attempts to assign a value to a[count], but neither a nor count is defined anywhere. Without a hint as to what you expect that statement to do, it is hard to figure out how to get it to do it.

arkoenig 340 Practically a Master Poster

I have no idea what you're talking about.

However, if you're trying to convince me that the only only way to divide two integers is by repeated subtraction, then you should learn something about shifting.

Using shifting and subtraction, it is very easy to come up with an algorithm that divides two integers in a reasonable amount of time. In practice, hardware designers are more clever than that.

If you are worried about how long it takes to divide one integer by another, I suggest you write a program to measure how long it actually takes, and then stop worrying.

If, on the other hand, you mean something else entirely, then I'm sorry to say that you're going to have to explain it more clearly if you want me to understand it.

arkoenig 340 Practically a Master Poster

but RAND_MAX is a very big number, and computer does dividing in minusing divisor timees a number.. so it it is like this
10/2 = 10 - 2(1) - 2 (2) - 2(3) - 2(4)- 2 (5)
how effective is that for RAND_MAX/2 :)?

1) RAND_MAX is not always a very big number; I've seen C++ implementations on which it was 32767.

2) I think you are mistaken about how computer arithmetic works.

arkoenig 340 Practically a Master Poster

Indeed, Narue's tutorial is detailed and useful.

However, it does not cover the point that I was trying to make in my earlier note, which is that unless n is a perfect divisor of (RAND_MAX+1), I don't know of any way of determining a uniformly distributed random number k such that 0<=k<n with a single call to the rand function. In fact, I'm not sure it is even possible to put an absolute upper bound on the number of calls to rand that will be needed in a particular case.

Of course, if numbers that are almost, but not quite, uniformly distributed are good enough, then it's easy.

arkoenig 340 Practically a Master Poster

It definitely can be done in linear time, just because of its special nature, that is because of the properties, 1,2,3,4 listed above.

Indeed. I had figured that the reason that no one bothered to answer it was that it was so easy.

However, since no one has made any progress so far, I'll give a few hints:

1) The O(n) space requirement means that it is possible to make temporary copies of the arrays. Therefore, we can assume without loss of generality that it is OK for the algorithm to change the contents of the arrays.

2) The constraints on the values of the array elements means that it is possible to use a radix sort to sort them, which is O(n).

3) Once the arrays are sorted, there is a trivial O(n) algorithm to determine whether they are disjoint.

I'll leave the code to someone else.

arkoenig 340 Practically a Master Poster

If this is the biggest problem of your life, you are truly blessed.

Fbody commented: LOL! :) +4
arkoenig 340 Practically a Master Poster

Your program starts with a do...while loop that looks at the value of a variable named "choice" as its terminating condition. After that, you have a collection of if statements that also look at the value of this variable.

However, I can find no place in your program where you ever give that variable a value. What value do you expect to have, and how do you expect the value to get there?

arkoenig 340 Practically a Master Poster

The technique that jonsca describes is widely used, and I'll bet that most of the people who use it fail to realize that it doesn't quite work: The numbers yielded by rand()%n are not uniformly distributed unless n is an exact divisor of RAND_MAX+1.

I believe that if n is not a divisor of RAND_MAX+1, there is no way to solve the problem correctly without using a loop (or an equivalent recursive function call). With that understanding, here is a function (slightly modified from page 135 of Accelerated C++) that takes an argument n and returns a uniformly distributed random number that is >= 0 and < n:

int nrand(int n)
{
    assert (n >= 0 && n <= RAND_MAX);
    const int bucket_size = RAND_MAX / n;
    int r;

    do r = rand() / bucket_size;
    while (r >= n);

    return r;
}
arkoenig 340 Practically a Master Poster

Your a teacher? A professor?

I'm a coauthor of this book.

jonsca commented: The master is finally revealed! +5
arkoenig 340 Practically a Master Poster

>>I would generally agree, but if OP is trying to learn the semantics of arrays or even C++, then he needs to get a good grasp of the basics before using something like vectors. So that he knows exactly what each function will do for him.

It's easier to learn vectors first. I know because I've taught it both ways, and I could tell where my students had trouble from the questions they asked.

arkoenig 340 Practically a Master Poster

If you're just getting started with C++, then the following advice may seem heretical; but long experience has convinced me that it is sound:

Don't use arrays. Use vectors (i.e. std::vector<T>) instead.

The reason is that arrays aren't really objects, which means that there are various restrictions on how you can use them: You can't assign them; if you try to pass one to a function, it magically transmutes into a pointer, the size of an array has to be a constant (unless you take charge of memory allocation yourself by using new and delete), and so on.

If you use std::vector and std::list instead, you will make your life much easier.

arkoenig 340 Practically a Master Poster

When you say that it doesn't work, what do you mean? What did you try? What happened when you tried it?

The answer to your question "How do we compre the data..." is found in the definition of lexicographical_compare. I suggest you look it up.

arkoenig 340 Practically a Master Poster

Are you saying that you don't have the time to do your own homework but you want someone else to find the time to do it for you?

arkoenig 340 Practically a Master Poster

New programming language like python and ruby, do they have space to grow?

New? Python? It's about 20 years old! Heck, Python 2.0 is more than 10 years old...

arkoenig 340 Practically a Master Poster

By the way:

1) The IEEE floating-point standard does say that there is a "sticky bit" that the programmer can turn off or test, and that the hardware turns on whenever a floating-point operation does not give an exact result.

2) The current C++ standard does not provide a uniform way of accessing this bit.

3) The current C standard does provide a ay of accessing this bit, which will be picked up by the next version of the C++ standard.

4) Some C++ implementations surely make it possible to do this now.

So if you really care about being able to determine whether a floating-point operation is inexact, I suggest you look at your compiler documentation to see whether it is one of those that already supports this forthcoming feature.

arkoenig 340 Practically a Master Poster

Your problem is probably that your program is trying to remove blanks in place, but on line 50 you're calling it with a string literal as its arguments. Compilers are permitted to write-protect the contents of string literals, and many of them do these days.

As a quick check of what your implementation does, try the following:

int main()
{
    char* p = "foo";
    *p = ' ';
}

and see what it does.

arkoenig 340 Practically a Master Poster

In my problem, the main factor that affects the accuracy is not the physical error it is really the rounding error.

Please show us an example.

In particular, the IEEE floating-point standard was designed with a lot of input from some very good numerical analysts. What you're saying is that you think the problem you're working on is one for which their experience is irrelevant.

Well, this should be an easy claim to prove: Describe the problem and let us make up our own minds about it.

arkoenig 340 Practically a Master Poster

Line 17 says that the add_fractions function has a parameter of type double. Line 31 says that add_fractions has no parameters. These two lines are not consistent with each other, so the compiler is complaining.

arkoenig 340 Practically a Master Poster

In order to solve a problem successfully, you must first understand what it is.

One way to test your understanding is to solve a simple version of it by hand, without using a computer.

So... m = 13, n = 241. In other words, what is the base-13 representation of 241?
Your turn.

I'm going to guess that your answer will be "I don't know." If that's true, no code that anyone writes will help you, because the best you can hope for is a solution to a problem that you still don't understand, and from which you will therefore learn nothing.

arkoenig 340 Practically a Master Poster

I don't understand -- what aspect of the program's behavior are you proposing to change, and in what way?

arkoenig 340 Practically a Master Poster

If what you are trying to do is determine whether a particular computation has been rounded, there is no standard way of doing that in C++. An individual implementation may offer a way.

arkoenig 340 Practically a Master Poster

If you have an IEEE-compliant implementation, then rounding is according to the current rounding mode, which by default is to round to the nearest value and to round to even in case of ties.

If you set the rounding mode differently (and standard C++ does not offer a standardized way of doing so, so you would have to consult your implementation's documentation to learn how), then rounding occurs as specified by the current rounding mode.

If you do not have an IEEE-conforming implementation, then you get whatever your implementation does.

arkoenig 340 Practically a Master Poster

If your computer supports IEEE floating-point, as most processors do, the five fundamental operations + - * / and sqrt will yield the correctly rounded result.

arkoenig 340 Practically a Master Poster

C++ does not require that, but the IEEE floating-point standard does. So if you're using C++ on an IEEE-conforming machine, it will behave as you want; otherwise you get what you get.

arkoenig 340 Practically a Master Poster

Your remarks, such as "user input" and "ends loop", do nothing to convince me of anything, for several reasons.

The most important reason is that the program you originally posted does not compile, because it uses a variable named "word" that is not declared anywhere.

But even if it did compile, it could not possibly work, because the loop in the program terminates based on the condition search != "" and the variable "search" is never changed in the program after its initialization near the beginning.

So either the loop always terminates after running once, or it never terminates.

That is enough to convince me that the program cannot be correct.

Now it's your turn. Fix the program, and understand it well enough that you can explain why you are sure that every line in it is correct.

arkoenig 340 Practically a Master Poster

If you don't see why the program doesn't work, I suggest that you go through the program a line at a time and explain to us what each line does and why you are confident that it is correct.

arkoenig 340 Practically a Master Poster

You're missing << in line 27 and () in lines 32 and 40.

arkoenig 340 Practically a Master Poster

In order for Map to work correctly, you have to give it a comparison function that takes two arguments and yields true if the first is conceptually "less than" the second.

All of the comparisons of vector values that appear in your code are equality comparisons.

You probably want to do something like this:

bool operator() (const vector<Literal>& v1,const vector<Literal>& v2)
{
    return std::lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end());
}