n1337 29 Junior Poster in Training

In the operator<< function, obj.A is a pointer to the starting location in memory of the 2D array A. Presumably, we don't want to print this out (its an address). You should instead loop through each element in A... Hint:

osObj << (obj.A)[i][j]

should go inside a loop, assuming that you use the variables i and j to loop over the matrix A.

n1337 29 Junior Poster in Training

No, I mean you should think about what the code osObj<<obj.A is doing. In particular, obj.A is an address...

n1337 29 Junior Poster in Training

Perhaps these questions will help lead you to the answer...

What is obj.A? What is *(obj.A)? How is this different from (obj.A)[0]? What is **(obj.A)? How is this different from (obj.A)[0][0]?

n1337 29 Junior Poster in Training

In general, matrices do not need to be square in order to compute their product... rather in computing A*B, the number of columns in matrix A must be equal to the number of rows in matrix B. Regardless...

We can think of each matrix as being composed of row and column vectors. Then, each entry [i,j] in the product matrix is the result of a dot product of a certain combination of these row/column vectors. We can denote the row vectors in matrix A as row[1] ... row[n] and the column vectors in matrix B as col[1] ... col[n].

Then, element [i,j] in the resultant product matrix would in fact be the result of the dot product <row, col[j]> , for 1<= i,j <= n

So the question is... how do you extract the column and row vectors using your particular representation of a matrix?

n1337 29 Junior Poster in Training
n1337 29 Junior Poster in Training

After lots of use you will tend to remember the syntax, even if when starting out you just copy and paste code. I suppose the most important thing is understanding... That is, understanding all of the components and program flow so that you can build on basic templates (provided for example, in VC++), and being able to read and understand documentation (e.g. msdn) so that you can adapt what you need to. And, depending on what your using to code (i.e. an IDE), code completion will probably allow you to get by quite well. If you're using a text editor like VIM on the other hand, it really saves time to not have to look stuff up all the time.

I'm sort of anal in that I typically force myself to memorize mundane syntax before learning new stuff... And unfortunately, it's one of the shortfalls of C/C++. That is, the heavy(ier) syntactical requirements (it's all relative)... I love having to set up all the structures and pointers to structures and stuff just to get a simple working tree structure... /sarcasm.

My prof made a joke in class once when he was explaining code generation that went something like "... and so all you have to do is return a list that will represent a parse tree. In Scheme, just remove the commas from the pseudocode. In C.. write a few hundred lines of code. Then write a few hundred more to support it." :)

Edit: but basically, …

n1337 29 Junior Poster in Training

What about a taylor series approximation... The theory is rooted in calculus, but you can probably manage by simply applying the formulae and ignoring the theory (FYI series and taylor approximations are typically covered in a Calc 2 course).

Here are the approximations (scroll down to the "series definition" section): http://en.wikipedia.org/wiki/Trigonometric_functions

Edit: You need to know radians as well!

n1337 29 Junior Poster in Training

Oops I made a typo in my previous post. I meant to say "does the function need to return a string that represents the binary integer"? I ask this because the character sequence "10110" is not the same as 10110 (the first has a different bit sequence since each individual character has a corresponding ASCII value, which has its own bit sequence..) For example, the character '1' has ASCII value 0x31..

Anyway, to output the actual bits you can use fprintf( stdout, "%c", __) combined with the bit shift operator >> combined with bitwise & and bitwise |..

But I am guessing that you need the string representation... So you will find repeated mod a % b and division a / b here.. Hint: binary is base 2

n1337 29 Junior Poster in Training

Let me try to understand here... The function needs to return a that represents a binary integer? Or the actual bits of the returned value need to be the correct binary representation of the decimal value?

n1337 29 Junior Poster in Training

But it is not working. I get an error when I run the procedure, which says this: "string-append: expects type <string> as 2nd argument, given: #<void>; other arguments were: """

What am I doing wrong here?

The issue is that your char_toupper function actually returns void... In fact, the display function (display <>) has no return value. That is, (void? (display "a")) returns #t...

But you are using your char_toupper function as though it were returning an upper case character. If you want to use it like this, then you should modify it so that it actually returns the uppercase character to be used in string-append. Alternatively, you could modify your string_toupper function so that it does not require a result from char_toupper. That is, simply output the uppercase characters one character at a time (as your char_toupper currently does), and do not construct an entirely new string of uppercase characters... This would actually be faster since you would not require string-append, which takes O(n) time (although may not be the purpose of the assignment... it depends on whether or not your string_toupper need return a value...)

Hope that makes sense!

n1337 29 Junior Poster in Training

Oh, well in that case, how are you storing the matrix? Probably an array of numbers, I would guess.

If you are just starting, might be easiest to use a 2D array; declare an mxn array of integers (or whatever data type you need to hold, where m = number of rows, n = number of columns). If you are dealing strictly with square matrices, then m = n.

For the upper triangular one:
Simply loop through and print out the first n entries of row 1, (n-1) entries of row 2, (n-2) entries of row 3, ... , (n - (m-1)) = (n - m + 1) entries of row m.

If you are a little heavier on the math side, you could also try to implement it using a 1D array, (after all, a rectangular 2D array is really just a 1D array in disguise).

Ummm, the lower triangular one is very similar, if you can see the pattern then you should be on track to solving it now..

n1337 29 Junior Poster in Training

Do you mean you need to print the LU factorization of the matrix?

if so, perhaps this page might be of some assistance...

n1337 29 Junior Poster in Training

Well, if you want to get technical, it has to do with how you are referencing each object at the memory level (and understanding of this depends on how much you know about memory models). Additionally, I think the pointer information is declared on the heap, vs your second declaration which is declared on the stack (someone to confirm this?).

The first is declaring a pointer to the Node struct, and so acts as a pointer. The second is just a straight declaration of the object. You can read up on classes and objects here.

With the first, you have to dereference and then you can access object members (hence the -> operator). With the second, you need only use the "." operator. Otherwise, the first acts like a pointer, the second is simply the object, so use them accordingly.

n1337 29 Junior Poster in Training

Since Addition is a boolean value, you could also simply code:

if (Addition) { //do whatever }

That is of course unless you are attempting to make the boolean value true...which would defeat the purpose of checking it in the conditional statement in the first place...

Note: You have similar syntax elsewhere in your code, i.e. lines: 47, 76, 105, 134, 163, 192, 221, 250, 279, 378, 385, 392, 399...

n1337 29 Junior Poster in Training

yup yup of course in that case, if you ever computed a sum of 12 (two 6's), numInstances[sum] would actually be out of bounds (no error though, so be careful). So...you would have to store it in array index sum-1 in this case. Also remember that numInstances[0] would actually never have any other value other than 0, since you can never roll a 1 with 2 die...

VernonDozier commented: Good catch! :) +5
n1337 29 Junior Poster in Training

lol dont redefine stuff. I said move the declarations around. To before where you use it. Before the loop. You already have the working code (I assume since that is what you said in a previous post). So move the declaration of end and running time before the loop, then just use the variables within the conditional statement as I provided...

n1337 29 Junior Poster in Training

Yes but you will need to change around some declarations (I.e. end and runningTime need to be declared before the loop). Also, I just copied your code from outside the loop because you said it was working, so if you move it inside the loop, you no longer need it outside...

Note: I haven't tested any of your code or my own...I don't have a compiler on this computer. Should be ok though...gl

n1337 29 Junior Poster in Training

can you do something about it?

Well, you could do something about it... In your for loop, do something like:

if (i%5000 == 0)
{
     end = clock();
     runningTime = (double) (end - start) / (double) CLOCKS_PER_SEC;

     cout << i << endl << "the running time is " << runningTime << endl;
}
n1337 29 Junior Poster in Training

Well...those links have a lot of the differences. I think basically C++ is an extension of C with added features. You can still use C code in C++, and code in a "C-Style", but C++ has features that do away with some C stuff.

In terms of abstraction from the machine, C is very low level. It was designed to be basically a thin layer over the machine, providing the functionality and sort of direct interfacing of machine-level or assembly-code, but being portable (assembly and machine code is not portable, i.e. it is dependent on the architecture). You should read about the history of C...here is one place to start.

So then, C++ is an extension to the C language. It has added functionality. In particular, it has a rather extensive standard library (compared to C), or rather has an extended library. It also supports the idea of objects and classes, and supports large-scale projects and security better than does C. You should read about the history of C++ as well and read the links provided above to get some of the more technical differences.

Please tell me about some functions that we can not use in C.

Uhhhh well I really was only learning C in school, and am just sort of learning C++ because. Everywhere I look (and from what my profs tell me) you should learn C first, and then learn C++ (easy transition). In terms of actual functions, well:

One …

n1337 29 Junior Poster in Training

What have you tried so far? Any thoughts on the subject? Do you know how to declar a multidimensional array, or a single-dimensional array for that matter? Do you know what a conditional (if) statement is (or what a switch statement is)? Do you have any code attempts written (if so post them here)?

n1337 29 Junior Poster in Training

Well not ALL, just every integer up to that numbers square root. :)

Yes, Shaun is correct. However there is more...A related theorem says that every number is either prime itself, or else is a product of primes. I forget who proved this (Euclid?) but here is a proof (I did it quickly, so it might by slightly sloppy :)):

Let n be an integer greater than 1. We must show that n is either prime or a product of primes. If n is prime, then we are done (clearly the result follows). If n is not prime, then n is composite, and thus there exist integers x and y such that x*y=n. Now, if x and y are primes, then we are done and n is a product of primes. If not, then x and/or y can be divided further into a product of smaller integers. Clearly the process iterates until we have broken the integers into primes (i.e. we are bounded below by 1, and since we are dealing with integers, the process repeats finitely many times). Thus the result follows...(I know this is really quick so you can search for a better? proof online...)

From this result you can see that you do not need to test EVERY integer up to a given number n to determine whether n is prime. Rather, since n is either prime or a product of primes (as we just showed), you only need to determine whether n is divisible …

William Hemsworth commented: Great, Interesting post :) +2
n1337 29 Junior Poster in Training

OK that is a really open ended question...Before I (or anyone else) can even begin to help, we need to know a few things:

1) When you say user to user, do you mean like across a network? On the Internet? On the same computer?

2) Do you want to create a windowed application (or in general a GUI if you are not using windows), or did you have a console-based game in mind...?

3) Have you done any background work yourself? You need to post the code you have done so far, as well as any thoughts you have about solving the problem before we can help.

Without more specifics, you are likely to get an open-ended response to your question...

"Use a 'game loop' to check if there is a winner. Create a grid to hold the player choices. Loop and allow each player to enter his/her choice turn by turn."

n1337 29 Junior Poster in Training

I do apologize. Permit me to clarify:

I am a beginner and am learning C++ and QT

I think I went on at length about how this might restrict the types of things you will be able to accomplish...until you get more experience/depending on how experienced you are. If you have never programmed before, I really do not suggest diving face-first into programming GUIs. Nail down the basic language concepts first, learn how to program, then revisit the topic.

Or you could learn VB.Net instead (I'm being facetious...) :)

I would like to know how to make an interface like the one in windows media player 11 - one that is completely custom including the minimise and close icons.

By interface I am assuming you mean a GUI (graphical user interface) application. I mentioned above to investigate Win32 if you are using a windows platform. This will be a starting point for windowed applications (btw the minimize and close icons are standard). I also gave you a link to a discussion where there have been other posts on a similar topic. While your at it, if you actually do learn Win32, I might suggest investigating DirectX or OpenGL...(thats another story for another time). Again, I just want to stress that these topics may seem a little bit foreign if you are a complete beginner, so my humble suggestion would be to build a solid foundation in the language before trying to tackle them...But that's just my opinion …

n1337 29 Junior Poster in Training

You know, it seems that introducing GUIs to the beginner programmer is a little bit like opening pandora's box. I mean, I'm not saying that in doing so we release all the evils of programming... It just seems to me that when you are new to a language or programming in general, trying to tackle advanced topics will only bring about pain and suffering (or hours of torture trying to decipher what seems like cryptic code to anyone not familiar with the language syntax and semantics, libraries, etc). My fear is that, in doing so, the new programmer loses motivation and gets turned off learning...

That said, GUIs are probably the most exciting thing to most new programmers. I don't really know what I'm accomplishing in this post, I just guess I want to stress that if your new, it might not be easy, and it might be a leap to accomplish some of the things you might be dreaming of, so just stay motivated (even if progress is slow) and you'll get there eventually...Thats my inspirational/motivational tip for the day. aha :)

Anyway, if your using windows, investigate Win32.

Uhhh, this link is a recent discussion that gets into some books and things about GUIs (scroll through the posts, I think niek and William leave some links). There are also TONS of other discussions on this forumn and the net.

Good luck

n1337 29 Junior Poster in Training

If anyone has a good book suggestion, or site where I can get some practice/advice on memory management I would really be grateful. I'd hate to be one of those problem-programmers that cause the dreaded memory leaks in team-projects.

I literally googled "C++ memory management" and here is what I found:

one, two, three, and many more relavent links.

Google "C++ avoiding memory leaks":

one, two, three, and many more relavent links.

As for books, go to a public library or a bookstore and search for C++ coding techniques, memory management, pointers, memory allocation, memory stack and heap, etc etc. There is also a sticky thread in this forumn about C++ books...lots of good references in there...just sift through them to see what you see...

Of course, best way to learn is to do... You can read all you want about memory management (or any programming technique in general), or find sites that tell you what to do and list rules to follow, but without practice, you won't understand how to apply your knowledge and problem solve.

Cheers!

n1337 29 Junior Poster in Training

You could also use a HashMap which is built for indexing, but I cannot explain that well enough to give a valid example.

A Hashmap is basically a combination of an array and a list:

Suppose you had a whole bunch of keys. The way a hashmap works is basically, you reduce the keys mod some fairly large prime number (say n). The prime number is essentially the "verticle" size of the hashmap (this is the way I picture it). What I mean by verticle is that, you create an array of size n, and this array has n indices.

So now you have this n element array, where the indices hold elements placed according to their keys mod n. As a (simple) example, let element A have key = 1, and element B have key = 2. Let n = 3. Reducing 1 and 2 (mod 3), we obtain 1 and 2, respectively. So element A will be stored at array index 1, and element B at inex 2. Fairly straight forward.

But what happens when two keys mod n have the same result? For example, suppose element A has key = 5 and element B has key = 8, and n = 3 (just as an example, the keys/n will generally be larger). Then 5 ~ 2 (mod 3) and 8 ~ 2 (mod 3) ("~" means congruent, I don't know how to do a triple-bar equal sign). So we have a collision. That is, …

n1337 29 Junior Poster in Training

ummm did you see the date? and it was already solved...

n1337 29 Junior Poster in Training

Right, I was compiling on VC++ 08...I think it looks cleaner this way too.

Anyway, it didn't even cross my mind that it would be parsed as a binary shift...

And I actually meant that, in the original posting,

vector< <vector<string> > 2dVectorOfStrings;

there is an extra opening character. Thats more what I was getting at :S

n1337 29 Junior Poster in Training

vector< <vector<string> > 2dVectorOfStrings;

I do believe it is just:

vector <vector<string>> myvec;

How you would size the inner vectors to 10 strings each and the vector of vectors to 10 inner vectors of 10 each would be pure conjecture

You could do something like this:

vector <vector<string>> myvec(10, vector<string>(10));

This would initialize the vector to have 10 elements, where each element (being a vector itself) has 10 elements. Of course, like you mentioned, a vector will resize itself anyway, so not too big of a deal. Alternatively, you could set an optional "starting value" for each of the 10 inner elements:

vector <vector<string>> myvec(10, vector<string(10,"hello!"));

...this sets each inner vector element to "hello!". And you can access elements like any multi-D array...

myvec[outer_accessor][inner accessor];

though I see no reason why you would even want to do so, other than as an educational experience.

Well, perhaps you want to create a directed graph-like structure, except instead of each node pointing to only a few other nodes, each node in the structure is reachable from any other node (all interconnected).

So for example, suppose you had a vector of vectors, where each element (outer vector and inner ones) had 3 elements.

Call the outer vectors country nodes. Like Canada, U.S.A, and Mexico. Call the inner nodes city nodes. So within the Canada vector, you have 3 city vectors, Toronto, Vancouver, and Montreal. These represent a network of some kind, each having the ability to interact …

n1337 29 Junior Poster in Training

I think you generate 100 random numbers in the range 1 to 200, and store them in an array.

Then you do 100 searches, each time generating a new random number, and using this newly generated random number to check if it is in the array (of random numbers created previously). You need to check :

a) that the (new) random number is in the array that you created previously
b) how many tests were required to find the random number in the array. I.e., you had to test array element 0, array element 1, array element 2, etc...
c) how many times out of the 100 newly generated random numbers did you find a match. I.e., if you only generate 50 new numbers that match those previously generated in the array, then you have a 50% success rate...
d) the number of searches completed...well you are only searching 100 times regardless, so I think this means the number of tests or comparisons used. That is, suppose that each time you search you had to go through the entire array of 100 elements, then you would have searched for a match 100*100, or 10,000 times.

But again, check with the prof to make sure!

n1337 29 Junior Poster in Training

Just so you know, if you write *crafty* recursive code, a.k.a. tail recursive functions, your compiler should optimize for recursion and use less stack space (i.e. reuse stack space)...the equivalent of using a for or while loop. (At least, GCC optimizes for tail recursion...I would hope that *some* c++ compilers do so as well)

That is, if the function call is the last operation (and the calculations are done as parameters), then memory can be reused --> no intermediate build up of operations becuase in order to call the function, the function parameters will be evaluated first.

EDIT: I took out the examples because after posting this I got curious and started playing around in VC++...turns out my example was kind of useless (hard to see the distinction), and seems like VC++ doesn't optimize for recursion at all...moved over to GCC and was able to pump out way more recursive calls, but again I didn't see the optimization between the tail recursion and structurual recursion...Now I'll have to think about this...

Maybe worrying about optimized recursion is best left in Scheme. haha

n1337 29 Junior Poster in Training

edit: how to delete a multiple post...

n1337 29 Junior Poster in Training

Please look at the date before reviving old threads...you are late by almost 2 years...

Salem commented: Quite so. +17
n1337 29 Junior Poster in Training

LIFO.

Stack using arrays...? It might be a better exercise to implement a stack struct (that can be easily resized and whatnot). But if you really want...then I guess we could work through one with an array...

But first, read about stacks here:
http://en.wikipedia.org/wiki/Stack_(data_structure)

Once you understand the data structure, come back with your attempts and we can go from there.

n1337 29 Junior Poster in Training

i tried to think it through but cant seem to figure it out..i was using the if to test some parts of it..sorry

Sorry I didn't realize this hadn't been solved. I don't have time atm (I'll be out and about all day...), but I will sit down later tonight and help you out, unless someone else has fixed up your problem by then.

n1337 29 Junior Poster in Training

Essentially you are extending the triangular number sequence: 1, 3, 6, 10, 15, ... (i.e. each number is the number of asterisks printed so far). Here is the pattern:
http://www.mathematische-basteleien.de/triangularnumber.htm

Using a switch statement would not be very effective if you needed, say, 100 lines (heck even having to make 10 separate cases scares me...if a problem is not fun, then there is a better way to solve it, and writing out 10 switch statements does not sound like fun :)).

In any case, notice that on each successive line, there is 1 more asterisk than on the previous line (this pattern will become more evident if you follow the link I gave you above). If you use a nested loop as Narue suggested (i.e. another for-loop within your existing for-loop), you can print out a number of asterisks equal to the current value of "digit" plus 1.

That is, if digit = 0, your inner for loop will run from 0 to 0, and output 1 asterisk.

If digit = 1, the inner loop would run from 0 to 1, and output 2 asterisks.

If digit = 5, the inner loop would run from 0, 1 , ... , 5, and would output 6 asterisks...

EDIT: and Narue posted again before me...so this kinda becomes redundant

n1337 29 Junior Poster in Training

Right, well, I'm not sure I understand what you are asking...

Which shapes do you need to learn how to draw? Just a square/triangle? That would be where I would start...

Assuming your exam is a written exam, I suspect your instructor wants to examine your ability to trace through the iterations of a loop...try to understand how the variables change after each iteration and how that affects your output (when outputting to the console).

As for math calculations...that could really mean anything. For example, if you were given the side length of a square, you could classify a calculation as determining the dimensions of the square, and consequently how to construct an appropriate loop to render it onto the screen/console.

Math calculations could mean extending a sequence of numbers (for example, the triangular numbers 1, 3, 6, 10, 15, ...) to construct a triangle.

Moreover, math calculations could also pertain to rendering shapes on a (cartesian) coordinate system.

So what do you mean by math calculations?

n1337 29 Junior Poster in Training

C# and C++ have very similar syntax (C# came after, and is based on Java and by extention C++). C# was designed around the .NET framework (which I actually really like, from what I remember)...

This might be helpful...
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=542964&SiteID=1

n1337 29 Junior Poster in Training

I am having a problem because what i am doing is not related to the class.

What do you mean? What specifically are you having difficulty with...Also, does your code compile? If not, where are the issues...

Take a look at the code below please and give me some suggestions as to how to make an efficient menu

If you are bent on making console menus, you may be interested in looking up escape sequences to help you format...
http://msdn.microsoft.com/en-ca/library/h21280bw.aspx

As for making an efficient menu, I would suggest moving all your menu initialization into another method (that stuff where you have "loading" and what not), and then the actual main menu to another method (called menu or whatever). This will just make things cleaner and easier to work with (in my opinion).

Then, based on whatever choice, you can move into separate sub-menus and whatnot (more functions)...I would also suggest you always have an option to return to the main menu (from a sub-menu if you implement this), and possibly might want to look into some error checking.

But if you really want my opinion on how to improve it, I would suggest making a program GUI...would make things way cleaner (from an end-user perspective), and would be a good way for you to start moving away from console applications....If you are interested (and I assume you are on windows), and have time on your hands, then look into Win32 programming.

n1337 29 Junior Poster in Training

Or else you do a few math calculations, then set up a function to draw individual shapes....

For example, you could create a "square" function:

void draw_square(int sideLength);

which basically loops and outputs a square (of the specified size, using a loop) to the screen...

Advantage: you can create reusable shape-builders...to make more shapes...haha

EDIT: niek_e is right, what specifically do you need help with, rather than us just posting code...

n1337 29 Junior Poster in Training

Perhaps you need to update the label text as you update the progress bar value...in the same iteration...

n1337 29 Junior Poster in Training

I'm afraid that, even if you found some function in some library somewhere, it's implementation would still be O(n) (where n is the number of characters)....After all, a string is just an array of characters, and so you would have to see each character to determine if it is a digit or a number (or has an ASCII value within a certain range...)

EDIT: the only way I could think of improving this is to preprocess the data, which I don't think is practical for you purpose...Of course, if you are reading in a filename (from a user say), then you could check the characters as they are read, thereby avoiding an extra glance at the entire array...

n1337 29 Junior Poster in Training

I tend to think with pointers because I started with C...and pointers are commonplace in C...and thats how I learned...lol. But my thoughts were this: the elements in a stack may in fact be objects themselves (i.e. their own stack structures), so I thought it would be easier to manipulate if you were pointing to the element instead.

I'm sure it can be done without pointers, but that I shall leave as an exercise instead for you.

Besides, pointers are a wonderful and powerful (or scary..) part of the language...if you aren't going to use them, switch to Java (or C# if you are a Windows-only programmer ;) ). lol (just kidding) :)

n1337 29 Junior Poster in Training

I have a potential solution...though you may not like it (and I haven't fully implemented it, so there could be some leaks):

1) In your stack class, in the private field, in the node struct, change T element to be a pointer (i.e. T *element)

2) In the public field of the stack class, change your prototype for push to take T* as a parameter, and change the pop prototype to return T*.

3) In the push implementation, change the function header to accept T *ele as a parameter.

4) In the pop implementation, return type should be T*, change T popped to T* popped.

5) In the view implementation, dereference temp->element (i.e. *temp->element).

6) In main, you can set up your stacks as follows:

stack <int> *subs = new stack<int>;
stack <stack<int>> *mains = new stack<stack<int>>;

To push elements to the subs stack:

int a = 5;
int b = 10;

subs->push(&a);
subs->push(&b);

To push elements to the main stack:

mains->push(subs);

However, I didn't know how you wanted view to behave for main...To view the top stack in main you can do this:

mains->pop()->view();

Anyway, I didn't check all the implementations, so some things might need to be changed...and this may not be what you were looking for...and you might have to write a few extra things...dunno

n1337 29 Junior Poster in Training

But what's the main diff between them?

The difference is just in the way you are storing them, at the memory level. For example, suppose you had the following memory model:

Address             Contents
               ...                    ...
            |  200   |         |             |
            |  204   |         |    15     |        
            |  208   |       | 'a' 'b' 'c' 'd'|       
                ...                    ...
            | 1000  |         |             |
            | 1004  |         |             |
                ...                    ...

And suppose that memory address 200 is empty, and 1000-1004+ is enough space to store the Pen object that you are declaring.

Now, when you declare Pen myPen1, you are essentially binding the name myPen to a memory location (the location of the pen object), which for the sake of this example, we shall say is memory address 1000 (enough space to store the Pen object). On the other hand, *myPen2 would be bound to memory address 200, where instead of directly pointing to the Pen object, you instead store the location of the actual object, location 1000. So we get something like this:

Address          Contents            Name          Location/Bind
               ...              ...
            |  200   |       |  1000    |         myPen1             1000
            |  204   |       |    15     |         myPen2             200
            |  208   |      | 'a' 'b' 'c' 'd'|
                ...                    ...
            | 1000  |       | (Pen object) |
            | 1004  |       |             |
                ...                    ...

I think that serves the purpose to illustrate the point. However, I think more accurately, myPen would not point directly to the Pen object, but would also actually have …

n1337 29 Junior Poster in Training

Well, you could use the idea of integer division, i.e. a|b ("a divides b"), and remainders...

So for example, we say "3 divides 10" 3 times, with a remainder of 1. So you could take the floor of your standard division, floor(10/3) = 3, i.e. just cast the answer as an integer, like (int) (10/3), and then use the idea of modulus to find the remainder (and thus the numerator of your fraction, where the denominator is simply the divisor...)

EDIT: Note that this thread was actually over 4 months old, so perhaps next time it is better to start a new thread...

n1337 29 Junior Poster in Training

what if you have to have the two input values as intigers, not allowed to classify them as floating or double? i can change the result classification thu.

..we have to divid two intigers (int) but still get an accurate answer.
ex: 10/3, is 3 with int
but there should be ways to make it =3.3333.
or 8/3 to be 2.666

absolutely you can do this. As was stated previously, you can explicitly tell C++ to hold onto the decimal portion. For example:

int n1 = 10;
int n2 = 3;

cout << (double)n1/n2 << endl;

//Note:  (double)n1/n2 is the same as ((double)n1)/n2

We have declared two integer variables. Then, before we calculate n1/n2, we explicitly "cast" n1 to be of type double (i.e we temporarily convert it to a double data type). So now n1 is of type double (temporarily) and n2 is an integer. We now have "double"/"integer", and C++ automatically keeps the most degree of accuracy it needs. I.e., now we will display information to the accuracy of a "double" data type.

However, if we had done this:

int n1 = 10;
int n2 = 3;

//note the parentheses around n1/n2 this time
cout << (double) (n1/n2) << endl;

The calculation n1/n2 would have been done first, i.e. "integer/integer", so C++ will throw away the decimal portion and only keep integer accuracy. Thus we would be left with 3, and this number 3 would then be converted to a double data …

n1337 29 Junior Poster in Training

Pass by reference works much the same as passing an array in Java. In fact, when you supply an array as an argument in Java, you don't actually copy the array, you just pass the address (believe it or not). It's just you don't explicitly manage the pointer aspect in Java; Java does all that for you behind the scenes (an array is just a pointer to a block of memory).

Having started with C, I don't quite like/use the C++ pass-by-reference style...I much prefer passing an address and dereferencing (I also tend to do a lot of things in an "archaic", C-fashion...haha).

Either way, I found a few articles that you might find helpful/interesting:

http://www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references
http://www.horstmann.com/ccj2/ccjapp3.html
http://www-ali.cs.umass.edu/~mckinley/377/labs/c++_for_java_programmers.html

n1337 29 Junior Poster in Training

This is exactly what you should expect. C++ implicitly converts (or rather truncates) the decimal portion to the integer. It is equivalent to using a floor() function..

So if you try to stick 2.999999 into an integer variable, the variable will instead hold 2 -- all the decimal information is simply thrown away and lost.

In fact, you can explicitly tell C++ to do this via "casting". For example, if you had a decimal number, for example 3.14, and you wanted to store it into an integer variable i, you could do this:

i = (int)3.14

In fact, you can explicity tell C++ to convert any type using this technique.

In any case, your program won't throw an error if you enter a decimal number into an integer variable...you will just get unexpected results (if you don't realize what you are doing).

So instead, you will have to change the data type (from integer) to one that will store decimals (like I said above). And again, be wary of modulo...

n1337 29 Junior Poster in Training

the data type is integer but it is not catching decimal numbers.

Unless I'm missing something, I think you answered your own question here...after all, integers are not real numbers or rational numbers (i.e. no decimals allowed). They are simply all the numbers in the set: {...-3, -2, -1, 0, 1, 2, 3...}. I.e. positive and negative whole numbers, including zero...

So you could change the data type from integer to float or double or what have you. But be careful....in your code you have "integer1 %1"...modulus works fine for integers, but is not defined for decimal numbers.

Also, please use code tags when posting code...