@OP, switch your language to java. There you don't have to worry anything about pointers.
Red Goose commented: Good adivce. +1
@OP, switch your language to java. There you don't have to worry anything about pointers.
This link talks about NPDA. In particular it states the following :
Theorem. A language is context-free iff some NPDA accepts it
That means, any language accepted by CFG can also be accepted by NPDA and any language accepted by NPDA can be accepted by CFG. Hence L(CFG) = L(NPDA)
>> I want to know more about these PDAs and the class of functions they compute.
The compute the same languages as Context Free Grammers.
Sure :
std::string greeting = "hello";
string::size_type pos = greeting.find('e');
std::string fuzz = greeting.erase( pos , 1)
- First you should be using std::vectors if possible.
- Second returning the array is kind of bad OOP, since your encapsulation decreases.
Instead consider providing mechanism to accesses the coefficients. For example.
class Polynomial{
private:
float coefficients[10];
float exponents[10];
public:
//...
const float coefficientAt(int i )const;
const float exponentsAt(int i)const;
};
or something of that nature. But you would be probably better off using std::vector if possible.
Or consider providing iterator-like interface. For example :
class Polynomial{
private:
float coefficients[10];
float exponent[10];
public:
//...
const float* coefficientsBegin()const { return coefficients; }
const float* coefficientsEnd()const{ return coefficients + 10; }
//...
}
if(y) //verify that y is not NULL before deleting it.
delete y; //this should be safe.
just note, deleting NULL is defined and does nothing.
If its worth anything, OP, I was in a similar train of throught when I first started learning programming also. For example, I didn't want to use std::vector because I didn't want to use other people's code. But I realized that, that train of thought will leave you learning for the rest of you life, and make no use of the learning. Eventually, you need to let go of this, "if I didn't create it, I don't want to use it"" ego. For example, because you don't know C++, you don't want to use C++. And to create C++ you need to know a hell of a lot of crap. And then say you for some reason you started to create another language, well that hell lot of crap uses other hell lot of crap, and it will be a never ending cycle to you, until you finally, start creating your own hardware in your basement, possibly using tools that you might know how it works and then there goes the cycle again.
As for your pointer question in previous thread( with opengl context ). The only reason why there is pointer in that code, is so that it when passed in the function, it can get modified, as mike explained it already.
What part of it confuses you? Instead of trying to avoid it, why not try to understand it, even if its just a little more. And remember we're here to help.
This is just a logic problem. What part of this is troubling you?
A simple way to do this could be something like so :
for i = 0 to coefficients.size
print coefficients[i] + "x" + "^" + i + " "
So in the simple example, say coefficients contains C = [2 0 1] which means "2x^2 + 0x^1 + 1x^0"
The above algorithm does exactly that.
So I should have been clear. It is a function that you need to create to validate the user's input. Here is an example :
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
void validate(const std::string& input){
for(int i = 0; i < input.size(); ++i){
if( ! isdigit(input[i] || input[i] != '.' ) throw std::exception("Error invalid input");
}
}
int main(){
string input;
getline( cin , input );
validate( input );
double d = atof( input.c_str() );
cout << d * d << endl;
}
The above is not tested nor compiled, its to give you an example and to be more clearer.
Quick way as suggested:
string userInput;
getline(cin, userInput);
validate(userInput);
double d = atof(userInput.c_str());
doStuffWith(d);
Sorry, didn't mean to offend you, just everyone else lol.
If you are fine with prepending it with a dash then you can for example :
int _1 = 0;
int var = _1 + 3;
It doesn't work because for an element n to be less than m, both x and y fields of n has to be lower than of m's( by your construct). You should test x first then y. Like so :
if(p1.x < p2.x) return true;
else return p1.y < p2.y
Sure. List the input, and what the output should be.
In your findpath function where you do this findpath(x, y - 1) == 1 )
, you need to pass it the maze and other variables as well. Change it to findpath(maze,high,wid,x, y - 1) == 1 )
make the return type of findpath int
Integer range : –2,147,483,648 to 2,147,483,647
unsigned integer range : 0 to 4,294,967,295
unsigned long long range : 0 to 18,446,744,073,709,551,615
so a integer cannot go up to 600851475143, so use long long type.
Also I would suggest for you to start from the number 600851475143 - 2 and go on from there. It will be faster.
Did you not have many female siblings?
no just a brother. now actually, lets drop this thread and forget about it. its pointless.
To enable polymorphism
Take your pick :
Complexity is :
- The quality of being intricate and compounded
- Characterization of something with many parts in intricate arrangement
- The state of being complex
- The degree to which a program is difficult to understand by human developers in order to, for example, inspect the program, or modify it
- something that relates to the co-presence of attributes in a coffee
- An ill-defined term that means many things to many people
- The term used when a wine has multiple flavour and aroma characteristics
You can't rely on people to help you all the time. The point of this project is not only for you to be able to do large complex project but also research and do stuff on your own. This is the internet. There are plenty of sources. First you need to learn a little about speech recognition. You need to get out there and look for sources. Sources that helps you. So go and work hard.
If I were you, I'll leave her and go find someone new. You don't deserve her..
Don't be desperate about her. If you want to forget everything about her, erase her cellphone number, email, and everything that reminds you of her.If she keeps contacting you, say this "It's over. Please stop keeping in touch with me". I assure you won't regret that decision :)
I don't want to forget her. She's a good friend. Plus right now, we're not going out, but we're just friends with benefits. And on real talk, I don't have feelings for her no more. Actually, I've been messed over one too many times. From now on, I'm treating all girls the same. They are full of bull. There is no such things as mushy gushy stuff. All girls are full of bullshit. They are all crazy. I'm sorry, there isn't no need for me to rant about my personal problems, especially in the internet.
If all he needs to do is output it then he can simply use string concatenation.
I think (from another thread with the OP) that the get() (and perhaps unget())is the only thing allowed. I agree that there are many other easier ways to do this.
In that case, if it hasn't been suggested, you can get the character if it is digit, else he can discard it. Even easier.
It shouldn't be hard. Read the input as a string. And move everything thats not a number to the right, and everything that is a number to the left. and output.
You have to assign a static const variable in order to be accepted as an array size.Try using a template function and pass it a constant integral when calling it.
template <int size> int* func () { //example int array[size]; return array; }
Don't do that. That code is undefined since you are returning a pointer to a variable that
will be destroyed at the end of its scope.
@OP you would want to use either a container or use dynamic memory allocation, for the variable modeCounter. In fact, just changed this line int modeCounter[S];
to this std::vector<int> modeCounter(S,0)
and it should all work fine.
You never intialized sec. Anyways, you should be doing something like this :
class Vector2F{
private:
float pos[2];
public:
Vector2F() : pos(){}
Vector2F(float x, float y) { pos[0] = x; pos[1] = y; }
float getX(){ return pos[0]; }
float getY(){ return pox[1]; }
void setX(float x){ pos[0] = x; }
void setY(float y){ pos[1] = y; }
void translate(float dx, float dy){ pos[0] += dx; pos[1] += dy;}
};
class Circle{
private:
Vector2F position;
Vector2F velocity;
public:
explicit Circle(float x = 0, float y = 0) : position(x,y){};
void draw(BITMAP *screen){
//default color is black, you can encapsulate color if you want also,
circlefill(screen, position.getX(), position.getY(), makecol(0,0,0) );
}
void move(float dx, float dy ){
position.translate(dx,dy);
draw();
}
}circle;
void update(Circle& circle, float dt){
//use some sort of numerical update method
}
void display(){
//..stuff
circle.draw( screen );
//calculate dt, i.e time difference
const int UPDATE_INTERVAL = 100; //milliseconds
if( timer.getElapsed() > UPDATE_INTERVAL){
update(circle, 1.0f ); //
}
}
I just thought I should say paper gel with apple pie and black window.
I don't know if there is a solution manual for it( google it), but you can post the problem and we'll be happy to solve it for you, provided that you attempted and solved the problem already, if not then we can guide you to the solutions.
So why aren't you using std::vector<DNA> again ?
from what I can see your code is prefect. Bugs free, fast, and takes no memory space.and its portable. Congrats.
>>What I think is a much more pertinent problem with C is the lack of certain paramount features of C++:
- Resource Allocation Is Initialization (RAII)
- Enforcing interface usage through strong type constraints and access rights
- Generic algorithms with compile-time enforceable policies
- Avoiding name clashes via namespaces, nameless functors and lambdas.
- Modularity in general
... things of that nature (and I only wanted to mention those that are essentially impossible to reproduce in C, i.e. they need templates).
Well the discussion was about mixing C with C++, not specifically the difference between C and C++. If C had a counterpart to those listed above then it would be a valid point, but since C doesn't have those features, you can't argue with this point.
Its probably because of the casting and such. Unless your working in a 'tight' environment, generally, forget about lookup tables.
What constitutes C vs. C++ and how much C style is too much will be highly subjective. But there's really no point I would call it harmful.
"IMO" being the key point here.
Well on average, the code you see, do you see C++ code constructing the program more safely or do you see C code constructing the program more safely. Now I'm not talking about the real 20yrs experienced coder, but just averaged coders. C is a lot more easier to f-up than C++, although its easy in C++ as well.
Let's not forget that std::string is carefully written to play nicely with C-style strings.
Well yes, but I was saying when possible not always.
You're thinking about this too hard. Let's use an example of "C is teh 3vil!" that I've personally been called on before:
std::srand((unsigned)std::time(0));
That ugly cast is ugly and should be removed because it's ugly. I was told that "proper" C++ uses static_cast:
std::srand(static_cast<unsigned>(std::time(0));
This is a matter of mixing C and C++ to those people. There's an alternative in C++ that many will delude themselves into believing is better. But it's not necessarily better, it just is. In this case I'll pick one or the other based on little more than my mood.
Ok thats the minor details. Again "mixing C++ with C", I took it as :
- using raw pointers versus std containers
- using pointers versus references
- using pointers versus smart pointers
- using char* versus …
averaged[i];
if(i>highest)
You probably want
if( averaged[i] > averaged[highest] ){ highest = i; }
No, it's not bad. Only C++ purists will rag on you about it, but they're in a world of their own anyway.
Its not bad but its harmful isn't it? If one had an option, why would he think about mixing C style with C++? Usually, its more safe to use C++ than C, IMO. Or maybe I'm interpreting his questions wrong. When he says mixing C with C++, I think of stuff like using char pointers and std::string. Plus its not consistent with one's program to mix up styles. It might confuse readers, or at least frustrate them.
Since they are raw pointers you would have to deallocate each pointer manually. If you want to use something like m_vPopulation2 = m_vParentPop2 then you should instead use smart pointers instead of raw pointers.
If both of the vectors are the same size then you can do this :
for(int i = 0; m_vParentPop2.size(); ++i){
DNA* elem = m_vPopulation2[i];
m_vPopulation2[i] = new DNA(*(m_vParentPop2[i]); //make a copy of DNA
delete elem; //delete the old element
}
You need to define what exactly you mean by limit. For example what should happen in the sixth character? Should the program just go with the first five character or should the program output an error message if more that 5 character is entered or should the program enable the user to input only 5 letters and any extra characters entered is ignored and not read as input? Not all of these choices can be done with pure C++.
>>There's an implicit trade off with any data structure where the cost of maintaining it (to a certain size threshold) is greater than the benefit of using a smarter data structure
Good point.
That access the first element. If you want to access the nth element you would do something like these :
Car car[25];
int n = 5; //for example
car[n].gas; //option # 1
(car + n)->gas; // option # 2
( *(car + n) ).gas; //option # 3
If possible go in that order. And if also possible go with std::vector instead of raw arrays/pointers.
struct car cobalt
. car is not a pointer. I believe you want something like so, struct car cobalt[23]
. In that case you could use the same notation as for regular arrays. For class object there is a special pointer operator cobalt->gas
. Notice the "->" operator. For class objects you can use the arrow instead of the tedious (*cobalt).gas
. In general though, avoid raw pointers.
Just note( I may be wrong but its worth thinking about ):
They will teach you theoretically that linked list are fast for insertion in the front, middle, and end of the list. Fast in terms of theoretical comparison of big-omega. While theoretically this is true, in practice, you will find that front and end insertion are potentially faster in arrays implementation.
So I'm not trying to pollute your brain or anything, but just hoping you realize the practical sense in data structures. And question everything you can, because from experience, teacher are likely to make mistakes every so often and be aware of them trying to endow their beliefs onto you.
Just thought I'd give you a little food for thought. Enjoy.
A suggestion is to make a get() function that checks for errors, for example :
template<typename InputType>
InputType getType(){
InputType var = InputType();
while( ! (cin >> var) ){
cout << "\nInput failed...try again : "; //print error message if failed
cin.clear(); //clear the error bits
while(cin.get() != '\n') continue; // throw away the junk left in the stream
//then try again
}
return var;
}
int main(){
int i = getType<int>();
}
The first thing I see is this :
int main()
{
//called functions
void userData(string studentName, int numOfClasses, char letterGrade);
double calData(int totalPoints, int numOfClasses);
void displayData(string studentName,char letterGrade, double gpa);
getch();
return 0;
}
which is wrong and should be this :
int main()
{
//called functions
userData(studentName, numOfClasses, letterGrade);
calData(totalPoints, numOfClasses);
displayData(studentName,letterGrade, gpa);
getch();
return 0;
}
There are definitely other problems but that should get you started.
>>Note: Im planning on using this with a Message box
If so then you will need to convert the variable to a string if its not already. A handy tool is to use something like the following :
template<typename ReturnType, typename InputType>
ReturnType convertTo(const InputType& input){
std::stringstream stream; //#include <sstream> for this
ReturnType var = ReturnType();
if(! (stream << input && stream >> var ) ) throw std::exception("Conversion failed");
return var;
}
//...
//use like this
float f = 3.14f;
string str = "PI = " + convertTo<string>(f);
Or you can get the input as a string and just access the first digit from there