Change the name from fill to fillArray and see what happens.
Nick Evan commented: Good suggestions +12
vidit_X commented: Very informative. +2
Change the name from fill to fillArray and see what happens.
>> the flamethrower works differently then the gun
Does it?
The flamethrower shoots and so does the gun.
The flamethrower has some mass and so does the gun
The flamethrower can run empty and so does the gun.
The point here is that, they do differ internally, but not so much
externally. Thus a solution to your problem is to create a hierarchy
with virtual and pure virtual functions, as pointed out already, while
making the have the same functions like shoot() or destroy(), or whatever.
This
printf("no. rows: ");
int row=GetInteger();
printf("\nno. columns: ");
int column=GetInteger();
Is not going to work. Have Row and Col be a big number.
Then ask the user to input row and col and use that.
For example :
int main()
{
const int MAX_ROW = 200;
const int MAX_COL = 200;
char Array[MAX_ROW][MAX_COL] = {0};
int ROW = 0;
int COL = 0;
cout << "Enter row and column : ";
cin >> ROW >> COL;
//now use Array, thinking that ROW and COL are its limit.
}
Well you can use 1d array to represent 2d array. Or you can create a
2d array at runtime, or you can use 2d array of vectors. Show your code as
of now and let see.
Ok thanks for helping me now I just have to fix the errors.
The errors are from ROW and COL, you have to define them.
Good. Now combine everything together to get this :
void Fill(char Array[ROW][COL], int row, int col )
{
if(row < 0 || col < 0 || row > ROW || col > COL)
return;
else if(Array[row][col] == '#' || Array[row][col] == '*')
return;
Array[row][col] = '*';
Fill(Array,row+1,col);//down
Fill(Array,row-1,col);//up
Fill(Array,row,col-1);//left
Fill(Array,row,col+1);//right
}
Ok so now we know that , Fill(Array,i+1,j); is down. So whats up? Hint the opposite.
No realize that (i+1,j) , (i+2,j) ... was showing you a sequence.
In code the sequence is generated by the code.
Remember that in array, moving down amounts to increasing the y,
while moving up amounts to decreasing the y. Its not like the
Cartesian coordinate system.
In array its like so :
//column
0 1 2
{ a b c } 0 //row
{ d e f } 1 //row
{ h i j } 2 //row
See that row 2 is lower than row 1, thus as row increases, we are
going down the array.
You know we have to now move around the array and fill it.
We can do something like this :
void Fill(char Array[R][C], int i, int j )
{
if(i < 0 || j < 0 || i > R || j > C)
return;
else if(Array[i][j] == '#' || Array[i][j] == '*')
return;
Array[i][j] = '*';
Fill(Array,i+1,j);
//more Fill(...)
}
The code Fill(Array,i+1,j) fills the array down from its starting position.
Now we need to also fill it left, right, and up. What do you think
the definition for LEFT, or RIGHT, or UP should look like.
I'm not sure because the normal r+1, j+1, r-1, j-1, etc wouldn't work since the length and width can change depending on the user.
Well we know that we have to start within the the walls. It would
not make sense that we start at the position of the wall. So for now
say we started at the middle of the hollow array. How would you
make it so that we transverse in all direction, left,right,up and down?
Yes that part of it. You have the part that does the actual filling.
So you have added this to the function.
void Fill(char Array[R][C], int i, int j )
{
if(i < 0 || j < 0 || i > R || j > C)
return;
if(Array[i][j] == '#' || Array[i][j] == '*')
return;
Array[i][j] = '*';
//move position
}
Now as you can see, we need to move the position. Since this is an
array we can only move up , down , left, or right. How would
you move the array while making sure that it will be filled? (Hint : recursion )
>> And I wouldn't use an else?
It does not matter.
So our base case checks if the index is valid, i.e if its positive and within the bounds of the array. Another base case we have is to check
if the value at Array[j] hits the wall or if its already occupied.
In real code This would be our code so far :
void Fill(char Array[R][C], int i, int j )
{
if(i < 0 || j < 0 || i > R || j > C) //if !(positive and within bounds)
return;
//if we hit a wall or if its already filled
if(Array[i][j] == '#' || Array[i][j] == '*')
return;
//logic goes here
In the logic goes here what do you think we need to do? Also for your char shape[][], the column needs to be specified, so you need to do something like this , char shape[][COL], where COL is a const variable declared somewhere in global or something.
Well the base case should be something like this :
if coordinate is invalid return;
else if value at index (i,j) == Wall, return;
else ...
Ok think about it.
1) We have a wall, '#', where if we reach that wall we need not to draw
2) We have a (row,col) inside the rectangle, which will be our starting point
3) What we need to do is fill the whole rectangle.
One way to do this is use a recursive function.
From our starting point, we should move left , right, up, and down,
while filling the array as we move. And if we hit a wall, '#', then we
should return. From this statement can you deduce what the base
case should be?
Ok you know that for every recursive function we need a base case, right?
Otherwise the stack will overflow, memory overloaded?
So before we get to the base case. Do you have any idea on how
the fill function might work. For example we could linearly go through the
whole array and look for a space and replace it with asterisk, but thats
not allowed.
Ok, give me 1 quick example of recursion. And FYI, I am going somewhere with this.
Ok. Did you know learn about recursion?
If I am reading this correctly, then what you have is a blank square
like so :
#####
# #
# #
#####
or something similar. To that. Then the uses specifies the row and the
column or the (x,y) coordinate, per say, that is within the space of
the square. For example it could be this :
#####
# #
# s #
# #
#####
the "s" represents the start coordinate. Now you need to fill the
rectangle with some characters? Before I say something, Are these
assumptions correct?
What you should do is create a 2D array class. Then use a vector to hold it. Here is what I mean :
std::vector< Array2D > vecOfArray2D;
int size = 0;
cin >> size;
vecOfArray2D.resize(size);
The Array2D is a class that you should implement.
Make use of the string functions like find_first_of and so one.
For example you can do something like this :
string vowels = "aeiou";
string sentence = "hello baby!";
if(sentence.find_first_of(vowels) != string::npos){
cout << "There is a vowel in " << sentence << endl;
}
sigh. Its practically the same concept as before, when I gave you
toHex() function. This time try to learn whats going on, please.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
template<typename Type>
string toHex(const Type& value, bool showBase = true){
stringstream strm;
if(showBase)
strm << showbase;
strm << hex << value;
string to_hex;
if(!(strm >> to_hex)) throw std::exception("Conversion to hex failed!\n");
return to_hex;
}
typedef __int64 int64;
int64 hexToInt64(const string hexStr){
stringstream strm;
strm << hex << hexStr;
int64 value = 0;
if(!(strm >> value)) throw std::exception("Conversion to int64 failed!\n");
return value;
}
int main(){
cout << hexToInt64(toHex(3145131833)) << endl;
cout << toHex( hexToInt64("bb76e739")) << endl;
}
Show us how you are detecting your collision. And a picture of the
situation would be nice as well. Depending on the situation, you might be
able to get away with 1d collision.
Just to check, what happens when you comment out line 24?
Which line is the problem pointing to?
No don't make it that complicated. This is what I was suggesting :
<input> 4 2
<output> 4/2
We see that the numerator is divisible by the denominator right?
That is 4 % 2 == 0. That means we can divide both the numerator
and denominator by 2, so we get (4/2) / (2/2) = 2/1 = 2
See? Now we can repeat this process until necessary. Thats the euler
algorithm for finding GCD, greatest common divisor.
Try to implement that.
Lets take test cases an see where that leads us.
<input> 4 2
<output> 4/2
We need the output to show it in the reduced term so 4/2 reduces to
2/1 which reduces to 2.
For the test case above name some things you notice about 4/2 ?
This is wrong :
T min(vector<T> a())
Try to guess which part.
Edit : Hint look at the error
clock() returns the time in mili seconds, CLOCKS_PER_SEC is the conversion
factor from the value returned by clock() into seconds.
while (clock() < stopwait) {}
That code waits until the value returned by clock() is greater than
stopwait, meaning, it waits X amount of milliseconds, where x is stopwait.
Oh when you say pair you mean in an 2d array like structure?
Like so :
Random generate= new Random();
int[][] Array2d = new int[4][4];
for (int row = 0; row < Array2d.length; row++){
for (int col = 0; col < Array2d[row].length; col++){
Array2d[row][col] = generate.nextInt(8) + 1; // [0,8)
}
System.out.print("\n");
}
So the above code might print out :
1 2 5 1
5 1 2 3
4 6 1 7
1 3 1 2
combine your even and odd in one loop :
for(int num = start; num != end; ++num){
if(num % 2 == 0) cout << num << " is even\n";
else cout << num << "is odd\n";
}
>>1. You emphasized on using const wherever I can, I'll but may I know why it is good?
Because making things const not only enables the compiler
to maybe optimize things, it also promises that the variable
wont be altered, and in general, if its not needed to be altered, then make it const.
>>2. If I use parameterless constructor like as u said Matrix(), how'll I initialize dimensions? I am not saying only have Matrix() constructor, have multiple overloaded constructor.
Matrix a ; //default constructor Matrix()
Matrix b = Matrix(3,3); // Matrix(const int& Row, const int& Col);
Matrix c = Matrix(3,3 0 ); //Matrix(const int& Row, const int& Col, const int& initValue);
a = Matrix(2,4); // init Matrix a.
>>3. Can you give me a little more hint on that private function, to be used in constructor?
Make a private function like the following pseudo function:
void _arithmeticHelper(const Matrix& other, const int& Operator){
for i = 0 to Row
for j = 0 to Col
switch( Operator){
case ADD : _myMatrix[i][j] + other._myMatrix[i][j]; break;
case SUBTRACT : ... break;
case MULTIPLY : ... break;
}
}
Where ADD, SUBTRACT, and MULTIPLY is some private static const,
maybe declared like so :
class Matrix{
//private helpers
private:
static const int ADD = '+';
static const int SUBTRACT = '-';
static const int MULTIPLY = '*';
}
>>4. const-corrected means using const parameteres in functions?
There is more to it, look …
I have commented on your code with code comments , see below :
#include<iostream>
using namespace std;
// A TEMPLATE CLASS WILL BE BETTER
class matrix //USUALLY A CLASS NAME SHOULD START WITH A CAPITAL LETTER( from convention)
{
//is the user able to change these ? if not then make it constant
int dim1,dim2; // a better name would be Row and Column
int **mat;
public:
matrix(int x=2,int y=2) //No need for default ctor of that form. Either Matrix() or Matrix(const int
//MaxRow, const int MAXCOL)
{
dim1=x;
dim2=y;
//make a private function that does this, which will make you code neater
mat=new int*[dim1];
for(int i=0;i<dim1;i++)
mat[i]=new int[dim2];
for(int i=0;i<dim1;i++)
for(int j=0;j<dim2;j++)
mat[i][j]=0;
}
int returndim1() //whats dim1 the Row or the column ?
{
return dim1;
}
int returndim2() //goto : returndim1 comment
{
return dim2;
}
//why ?Just let the user handle this
void input()
{
cout<<"Enter the elements of matrix - ";
for(int i=0;i<dim1;i++)
for(int j=0;j<dim2;j++)
cin>>mat[i][j];
}
//out is a bad name, and what if user wan't a specific format ?
void out()
{
for(int i=0;i<dim1;i++)
{
cout<<"\n";
for(int j=0;j<dim2;j++)
cout<<mat[i][j]<<" ";
}
}
//use const matrix x, also make this function const-corrected( look it up)
matrix operator+(matrix x)
{
matrix c(dim1,dim2);
if(dim1==x.returndim1() && dim2==x.returndim2())
{
for(int i=0;i<dim1;i++)
for(int j=0;j<dim2;j++)
c.mat[i][j]=this->mat[i][j]+x.mat[i][j];
return c;
}
else
{
cout<<"Matrix cant be added";
return c;
}
}
//goto comment on operator+
matrix operator-(matrix x)
{
matrix c(dim1,dim2);
if(dim1==x.returndim1() && dim2==x.returndim2())
{
for(int i=0;i<dim1;i++)
for(int j=0;j<dim2;j++)
c.mat[i][j]=this->mat[i][j]-x.mat[i][j];
return …
Why do you need pointers?
Now just do something with the gauss function, like so :
double gauss(fun f, double a, double b, int n){
return ( (*f)(a) + (*f)(b) ) * n;
}
The above is just a random example.
They are the member variables. For example :
class Person{
private:
//attributes
int health;
int power;
public:
Person(){ ... };
}
So a health and strength is an attribute of a person.
Is this not giving you an error :
"stream << hex << number;" ?
Make it stream <<std::hex << number; "
>> outputs a bunch of numbers
What what do you mean? In hex, just numbers could represent
some decimal numbers as well.
Is this what you need :
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
typedef size_t Type;
string toHex(const Type& number){
stringstream stream;
stream << hex << number;
return stream.str();
}
int main(int argc, char *argv[])
{
string num = toHex(15);
cout << num << endl;
return 0;
}
>> I'm trying to copy a value in a vector to a string. The vector is also of string type, I'm trying to copy through the iterator
So you are trying to copy the strings inside a vector into an array ? or into
another vector ? or into another string variable.
Usually my password, is something like this , xxxxxxYZZZZ, where
x is a letter, Y is a punctuation, and Z is a number.
Just curious, are you creating a 2d and 3d vector class.
Fair enough. You have to downcast the derived to base class.
e = dynamic_cast<Base&>(d) * 1.0;
Let me spell it out to you :
main.cpp: In function ‘int main(int, char**)’:
Inside your main function
main.cpp:15: error: no match for ‘operator*’ in ‘d * 1.0e+0’
There is no matching function function in the call :"e = d * 1.0;"
derived.h:15: note: candidates are: double Derived::operator*(const Derived&) const
It even says it here, the only valid function is "double Derived::operator*(const Derived&) const"
Only this time, it wasn't.
My reply to this thread is there in the singular.
But I also got a "This forum requires that you wait 15 seconds between posts. Please try again in 1 seconds."
I rather suspect had it timed out differently, it would have been a double post.
FWIW, I did a "posts since last visit in subscribed forums" search while the post was in progress.
The same happen to me, but I only hit the submit button, and
was not multitasking.
>>u = new double[I*sizeof(double)];
No! This is not C. In C++ its used like this :
u = new double[ I ];
The value inside [ ], is the number of elements. No need for the sizeof.
Put your printing loop out side separately. Like this :
void Foo(const int Size, const double value = 0 )
{
double *Array = new double[ Size];
//initialize it
for( int i = 0; i != Size; ++i){
Array[i] = value;
}
//print it
for(int i = 0; i != Size; ++i){
cout << Array[i] << " ";
}
}
>>u = new double[I*sizeof(double)];
No! This is not C. In C++ its used like this :
u = new double[ I ];
The value inside [ ], is the number of elements. No need for the sizeof.
Put your printing loop out side separately. Like this :
void Foo(const int Size, const double value = 0 )
{
double *Array = new double[ Size];
//initialize it
for( int i = 0; i != Size; ++i){
Array[i] = value;
}
//print it
for(int i = 0; i != Size; ++i){
cout << Array[i] << " ";
}
}
>> Your code does not satisfy the OP's requirements. If the OP searches for the word "Hello" in the string "Hello, world!", your code cannot find it because it only delimits on whitespace (which would get the string "Hello," from the file, not "Hello").
Oh, I guess I should have read the exact specification.
Also in both of your examples you provide a copy constructor, how
come? There is no need for that. And if one provides a copy constructor
one should also provide a destructor and an assignment operator,
but your example does not use dynamic allocation explicitly so there
is no need for copy ctor, unless I missed something :)
I think either one of these will do for me :
That idea was sooo not stupiiiddd
That idea was so not stupid<sarcasm>
That idea was so not stupid, cough* bullSh**cough
compare your program to this( not compiled )
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream file("Ahoo.txt");
if(!file) return -1;
string wordToFind;
cout <<" Enter a word to find in the file Ahoo.txt\n";
cin >> wordToFind;
string word;
bool isFound = false;
while( file >> word ){
if( word == wordToFind ){
isFound = true;
break;
}
}
if(isFound ) cout << "Word is found\n";
else cout <<"Word is not found\n";
return 0;
}
>> expected class-name before
Is wxNoteBook a class ?
Post your code, we have nothing to work with except your unclear
wording.
What do you want to do ?
What did you do ?
What were the errors ?
Just use the stringstream to break a string into tokens :
stringstream tokenBreaker;
string aLine = "Please break this into six tokens";
tokenBreaker << aLine; //insert string
string tokens[7]; //for now lets not use vectors and do it manually
int currentToken = 0;
while( tokenBreaker >> tokens[currentToken] && currentToken != 7)
++currentToken;