What language is this? Is it "managed" C++?
nucleon 114 Posting Pro in Training
JAGgededgeOB172 commented: Thank you! +1
What language is this? Is it "managed" C++?
I could be wrong ;), but I don't think it's possible. I believe you have to explicitly give the return type, like so:
template <typename U, typename T>
U foo(T& param) {
return *param;
}
// ...
int n = foo<int>(i);
If you simply want the numbers 1 to 9 uniquely randomized in a row, you could load the row with the numbers 1 to 9 and then shuffle it. No need to pick random numbers and check if they've already been picked. That's always something to avoid, if possible.
However, shuffled rows would still have column repetitions. Getting rid of those is the interesting part.
And to time the routine, you might consider the clock() function, as it has better resolution than time().
You only need to loop through less-than-half of the matrix since in each execution of the inner loop you're swapping two elements (and the main diagonal stays put). If you iterated through the whole matrix, you'd just put it back the way it started. Starting j at i+1 loops through the upper triangle of the square (or lower, depending on how you look at it), swapping elements with the opposite triangle.
Your program makes very little sense. Either you are using an object or you are not. You should clarify that with your instructor. This does not look like a case where you would use an object (although I'm learning that school exercises can be weird).
AD: If the (originally) larger number is even you simply don't add the (originally) smaller one to the accumulator. Since "odd" just means that the one's bit is set, it's essentially binary mutiplication using shifts (division by 2) and adds.
kuru: Have you written a program before? Do you have an attempt for this one? Anything?
SeeTheLite: I still can't make sense of your subtle concept. Can you give a (runnable) example of using a virtual function to access a base class's private data from a derived class?
To be complete, there is one other possibility. You can specifically call the non-unicode function by appending A to the function name. The other methods are probably better, though. ShellExecuteA(...);
Your gcd is not quite right. The recursive call should be gcd(x, i % x)
.
Sorry for getting kind of mean there. I had just read another post by a guy complaining because I had asked for more information, so I was kind of pissed off. I'll be sure to count to 10 next time.
I stand by my original post, every word of it. You're on your own. Good luck. (You'll need it!)
SeeTheLite: You are simply wrong here. Virtual functions are not used "for access" like friend functions; there is no similarity at all. Virtual functions are the basis of true object-oriented (as opposed to object-based) programming, so if you're confused here, you really need to study up.
Okay, I get it now. He uses list.h simply because it compiles with that (but why?) and is shorter than saying iostream.h. He declares variables in the main parameter list to avoid semicolons. He uses 42 instead of '*', etc., because it is one character shorter.
Here's something a little shorter:
#include <list.h>
int main(char c, int a, int b, int n) {
if(cin>>n)
while(cin>>a>>c>>b,
cout<<(c==42?a*b:c==43?a+b:c==45?a-b:0)<<endl,
--n) {}
}
Your tobase10 function does not work properly, and it is overly complicated. You do not need log10 or pow. You can loop until result becomes 0 (instead of using log10). A special case here is when result starts out as zero. Instead of pow you can start a number at 1 and multiply it by the base every time through the loop.
And you really don't want to convert to base 10 anyway, so the last part of the function should go (and the name should be changed to, say, getValue). You really just want the numerical value, not a base 10 representation.
Also there's no reason to use "displace". You can deal with the digits one at a time in a loop.
> the 15 on line 12 doesn't count either.
There are two 15's on line 12.
You should probably post this in the Perl section.
I'd try (in Perl): /\..*\s(15|53|92)\s.*=>/
SeeTheLite: You're either thinking of protected or friend, but not virtual.
It's ridiculous. And why do you include list (and the deprecated list at that)? You need to include iostream. And your main signature is wrong. Presumably you're supposed to be reading the input on stdin.
I don't know anything about .txx files.
Your example is not quite correct. You mean to do this:
base* pb = &d;
cout << pb->get_tag() << endl;
Simply declaring a base object and calling a method on it will never invoke a method of a derived object since the base object knows nothing about the derived object. It doesn't even know if there are any derived objects!
> Then if I include Point in Tools is that not a problem?
I don't think it's a problem. Have you tried it? If so, and if it doesn't work, then post the entire code.
I'm not sure I understand your question. Why can't you just #include "Point.h"
? I do not see anything circular here.
I'm not sure what the keyword "class" is doing in your map declaration, but using "int" would work like this:
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef map<int, int> t_map_inner;
typedef t_map_inner::iterator it_inner;
typedef map<string, t_map_inner > t_map_outer;
typedef t_map_outer::iterator it_outer;
int main() {
t_map_outer m;
m["abc"][3] = 5;
m["abc"][7] = 2;
m["def"][4] = 7;
for (it_outer i = m.begin(); i != m.end(); ++i) {
cout << i->first << '\n';
for (it_inner j = i->second.begin(); j != i->second.end(); ++j)
cout << " " << j->first << ", " << j->second << '\n';
}
}
sizeof p gives the size of an int**.
sizeof *p gives the size of an int*.
sizeof **p gives the size of an int.
sizeof *p[n] also gives the size of an int.
Those wishes are natural.
And you should use iostream, not iostream.h.
#include <iostream>
using namespace std;
Your declarations should be more like this. Change the definitions to match.
char getStuNames(char [][21], int);
double getStuGrades(char [][21], double [][4], int, int);
void displayGrade(char [][21], double [][4], int, int);
You can only put types in the angle brackets. You're trying to use a variable. Try this: map <int, list<CommandOp> > m_DynamicListMap;
I'm not exactly sure! I've learned to try typename in situations like that.
Obviously it's disambiguating something. It isn't needed if you use a concrete type like int instead of T, so it has something to do with that. Apparently the compiler cannot be sure exactly what a vector<T>::const_iterator
is and needs the hint that it is indeed a typename.
Perhaps a C++ guru can tell us more.
People generally like a more specific question. What exactly is the problem?
Declare your iterator like this: typename vector<T>::const_iterator pos;
You need to instantiate the OutputObject template: for_each (V.begin(), V.end(), OutputObject<T>);
You should use a const_iterator: vector<double>::const_iterator pos;
I'm assuming you have a using namespace std;
in your actual code. (Please post runnable code.)
Remember what NicAx64 said, that the plugboard etc belong as data members within the Enigma class. So something like this:
class Enigma {
PlugBoard plugBoard;
Reflector reflector;
Rotor rotor[3];
public:
Enigma();
// ...
string encrypt(string plainText);
string encrypt(char* plainText);
string decrypt(string cipherText);
string decrypt(char* cipherText);
};
int main() {
Enigma machine;
string msg("Hello world");
cout << msg;
string cipher = machine.encrypt(msg);
cout << cipher;
string plain = machine.decrypt(cipher);
cout << plain;
}
Try system().
I've been on a wild goose chase! Sorry about that.
This just looks like pointer arithmetic. When you add a number to a pointer it increments the pointer by that number times the size of the thing pointed to. So you probably just want to add 1 to lastseen.
What does sizemal hold?
Is 18h the size of a struct memory
?
See this for code tags.
If you feel you must use sbrk, then this line looks fishy to me: newnode=(struct memory *)sbrk(sizemal+sizeof(struct memory));
Should that possibly be: newnode=(struct memory *)sbrk(sizeof(struct memory));
It depends on what sizemal is. If it's the current size of the data segment, then you certainly don't want to pass that into sbrk since sbrk increments the size of the data segment by its parameter (whereas brk sets it to its parameter).
USE CODE TAGS!!!
Having said that, is it possible you mean to use brk and not sbrk?
People don't generally like "help me" or other generalized titles. A better title for your post would be "Problem in text-based RPG". That sounds much more intriguing!
I haven't seen this kind of thing before: if (Energy >= 5, Energy -= 5)
Do you realize that the Energy -= 5
part will be executed whether or not the Energy >= 5
part is true? Put Energy -= 5
in the body of the if.
Use a loop in Berkshire, not a recursive call, and you will probably be able to sort out why it goes right back to the menu.
Your basic problem is that you are assuming row is a char when it is actually an int. Additionally, you are using the parameters row and col of the player functions like local variables. Remove them from the function declaration and declare them as locals, making row a char.
Don't compare letters to there ascii codes. Say if (row == 'A')
not if (row == 65)
. It's both more readable and more portable.
Also, note that your Player1 and Player2 routines are almost identical. Combine them into one and pass in the player number (or possibly the player letter X or O).
The design depends on how it will be used. Write a driver (a main) that uses the object's interface AS IF it was already written. Then create a class for an object that fits that interface. It also depends on how much detail you want in the simulation. If you wished you could model it right down to the wires.
(Assuming you're not allowed to use std::list...)
It can be tricky swapping elements in a singly-liked list. Make diagrams so you can see what's needed. You'll probably need extra pointers to remember the previous elements of the two pointers with which you're traversing the list. Also, after you swap two elements, you will have to swap the contents of variables temp1 and temp2 to keep your loop on track.
That's a lot of code with very little explanation of the problem. Also, too many tabs and blank lines, IMHO.
You need to declare showBoard like this: void showBoard(char[][3], int, int);
Note that this hardcodes the size of the second dimension, but that will be okay if the board is always 3 by 3 (or indeed anything by 3).
You could try signal():
#include <stdio.h>
#include <signal.h>
void cleanup(int unused) {
printf("Cleaning up\n");
exit(-1);
}
int main() {
signal(SIGINT, cleanup);
for (;;) /* Ctrl-C to exit */
;
}
The usual approach here is to "shuffle" the numbers and then pick them off one by one. (Code untested.)
// Load array
for (int i = 0; i < 40; ++i)
n[i] = i + 1;
// Shuffle
for (int i = 39; i > 0; --i) {
// Pick rnd position and switch that element with i
int r = rand() % (i + 1);
int t = n[r]; n[r] = n[i]; n[i] = t;
}
// Use them in array order
for (int i = 0; i < 40; ++i)
doWhatever (n[i]);
AD: Our analyses seem to gell nicely. It explains why all the extra stuff is at the end. Icebone is basically reading and writing 8 times too many bytes. It could have caused a segfault but didn't in this case.
Simply adding / 8
after width * height
in the two loops involved (the reading and writing loops) writes the file perfectly.
But a better solution (assuming padding to the nearest byte) might be:
int size = width / 8;
if (width % 8 != 0) ++size;
size *= height;
And use size in the loop conditions.
The reason your file is 8 time bigger is because you are forgetting that each bit (in this format) represents a pixel. You are reading (and writing) 1152 * 813 bytes, but that is actually how many bits you should be reading! There will probably be some padding at the end of the picture rows, so there will be more than 1152 * 813 / 8 bytes. You will have to look up the info for the padding. You could try assuming that it's simply to the nearest byte and see if that works, but it could be rounded off to 4 bytes. At any rate, that's your basic problem.
You mean 3D array. In that case:
GLubyte ***bufImg;
bufImg = new GLubyte** [height];
for (int i = 0; i < height; ++i) {
bufImg[i] = new GLubyte* [width];
for (int j = 0; j < 3; ++j)
bufImg[i][j] = new GLubyte[3];
}