if Array is of type string, then the correct declaration would be :
ifstream readFile(Array[5].c_str()];
if Array is of type string, then the correct declaration would be :
ifstream readFile(Array[5].c_str()];
Did you define them? Also why does ThreeDimensionalShape only store 2
values. Also there is some serious problems with your inheritance diagram.
Take another look at the shape class. Look at what it stores. Look at
what you need for each class.
Is there a reason on why the overloaded operators are not
part of the Rational class? I think the GCD and simplify function need
not to be there. Why shouldn't the class automatically simplify the numerator and the denominator?
I can't think of any of the top of my head.
If there is no reason that the rational should not simplify the fraction in
the constructor, then there is no need for gcd. And thus no need for
simplify() function either. Other than thats, your on the right track.
Also what isValid() doing? And make sure the denominator is not 0 at all times.
I assume the problem you are having is to install the library into your IDE.
What compiler are you using ?
>>And the best part is, you can call it like this from anywhere, including inside of a class function, because it is global
That's not the best part, in fact, that's the worst part!
Take for example the call result(3). Trace the recursion out and get:
result(3) = result(2)*2
= result(1)*2 * 2
= 2*2*2
= 8
So you see calling result(3) calls result(2) which calls result(1).
Thus there are 3 calls to result n. If you try to generalize it,
you will see that result(n) will call result n times.
I was just in class, learning about STL and class templates. My teacher said you cannot split the class declaration and the function definitions into the normal header/cpp file like you can with non template classes.
Is this true, and if so why? I assume it would have somthing to do with the linking process because you can choose any datatype to work with in templates?
Thanks
Yes for now its true. Although if your compiler supports the
keyword export, then you can separate the definition and the header file. Did you ask the teacher about this ? She
would have probably gave you a through answer.
I kind of know how to do that but i'm not supposed to use the STL library. I'm supposed to write the stack implementation myself and i'm confused about how to embed the stack in the list class. i have taken the array stuff out of the code now.
using templates is not part of the stl. Its part of C++. Anyways,
first create a linked list implementation. Have a Node class.
And use that Node class inside a linkedList class. Use the notion
of head and tail pointers. Do you know how to implement a linked list? After you get the linked list, creating a stack class will take you less than 5 minutes.
>> if((number!=3) || (number!=5) || (number!=7) || (number!=9))
Change the OR(||) to AND(&&).
>>
while((number==3) || (number==5) || (number==7) || (number==9))
Change the "==" to != and change the || to &&.
And most of all, inside your do while loop, GET THE INPUT from the console!
EDIT: I just got ninja'd
looks staged.
Nope it was real. They banned it in later years for obvious reasons.
>>
bool Empty()
{
if(Head->GetFront()==NULL)
return true;
else
return false;
}
condense that down to :
bool isEmpty(){ return Head->GetFront == NULL; }
>> code in DigiNode, "DigiNode* Front". That name , Front is misleading. Consider renaming it nextNode or something similar.
This part of the code from DigiNum function :
Head->SetFront(CurNode);
Head=CurNode
Is wrong in a sense. After it gets executed, the Head is no longer the head, that is, it no longer points to the first element in the list. Rather
it acts like a broke down tail. You should change your function name
to something better. DigiNum is basically, getting a number and extracting each digit and inputting it into the list. So in you code :
void DigiNum(int x)
{
int r, q;
q=x;
while(q>0)
{
r=q%10;
q/=10;
CurNode=new DigiNode();
CurNode->SetDigit(r);
if(Head==NULL)
{
Head=CurNode;
return;
}
else
{
Head->SetFront(CurNode);
Head=CurNode;
}
}
}
Thats what it essentially does. But there are minor mistakes. You
got the part correct, where you extract the last digit of a number
in each iteration. The only part incorrect is when you add the number
to the list.
I would suggest doing something like this :
void extractDigitsIntoList(int num)
{
int lastDigit = -1;
while(num > 0)
{
lastDigit = num % 10;//get the last digit
num /= 10; //remove the old last digit
DigiNode *newNode = new DigiNode(lastDigit);
if(Head == NULL){
Head = newNode;
}
else
{
DigiNode …
>> tamano = length(v);
That line does not do what you think it does. The array passed in
as a parameter gets converted to a pointer, essentially. When that
happens, the size of the array is lost. You can either pass in the size( as suggested by WaltP) or you can use template deduction to let
the compiler deduce the size.
>>static bool not_isOdd(const int &i)
A better name would be , isEven(...);
Work with 2 copies, of the vector and transform then into even and odd
vectors like so :
int A[9] = {1,2,3,4,5,6,7,8,9};
std::vector<int> numbers = std::vector<int>(A,A+9);
std::vector<int> evenNumbers = numbers;
std::vector<int> oddNumbers = numbers;
std::vector<int>::iterator vecItr;
vecItr = std::remove_if(evenNumbers.begin(),evenNumbers.end(),isOdd);
evenNumbers.resize(vecItr - evenNumbers.begin());
vecItr = std::remove_if(oddNumbers.begin(),oddNumbers.end(),isEven);
oddNumbers.resize(vecItr - oddNumbers.begin());
Can you give a specific example of the equation you have in mind.
>>Line 1 of your code creates an object called "clubSuitIcon" giving it a parameter range value of 10 - 2. Does the order of this parameter matter? I'm curious as to why it would not be 2-10.
I wrote " ImageIcon[] clubSuitIcon = new ImageIcon[10-2] "
because I though it would show you that there are 10 - 2 = 8 elements.
I guess it would have been better if I did :
" ImageIcon[] clubSuitIcon = new ImageIcon[8] ".
Since your cards start from 2( of clubs) and end with the number(10 of clubs), the "10 - 2" was supposed to show you
>>that there are 10 - 2 = 8
cards to represent. The rest are J,Q,K, A.
>>Line 2 initializes the counter at zero and says if the clubSuitIcon length is less than i, increase the counter by one.
Its the other way around. If "i" is less than "the array length"
then increment "i" by one.
>>Not sure if i get why we are using the .length method?
.length is not a method. Its a constant variable. We use it
so that we don't access an element pass the array.
>>Line 3 says if the returned string value is "clubs" then increase the counter by 2 and concatenate the specific file. ??? I can tell that here we're telling the code what jpg file to pull but I don't know how to …
I guess you have never heard of templates. You can't use that code because it is an array-based stack implementation. You need to implement
an linked list based stack implementation. Do you know the basics
of a single linked list?
Why would they ever ban this sport. Its so easy to play.
You can play with your families. You can play with friends. Hell,
you can even play with a random stranger walking down the street.
Think of it this way,
a = 19;
b = 5;
result = a < b; // result = 0
So its essentially the same thing. You compare each bits. Check if
there is a mis-match, and go from there.
For example the equation for A < B, where A is a 2 bit number and
B is a 2 bit number would be this :
Let A = a1 a2;
Let B = b1 b2;
Where a1 and b1 are the left most bit. We are using the complementary
system, so a1 represents '-' if a1 = 1, and b1 represents '-' if b1 = 1.
Below the "!" is the negation operator. Then "+" is the or operator
and "*" is the and operator.
So the boolean equation is [A < B] = ( a1 * !b1) + (!a2 && b2)
Note that the above equation does not handle negative zero.
Thanks for the help. Why should I make the Fraction constant?
Because you are not changing the parameter. You are just
using its values for the computation.
But won't a friend give more versatility?
Obviously you did not get my post. In your case friend does not
do anything different to offer than the same function without it being friend.
So don't use friends for that. There is no need. Just implement
a class member function of type :
Fraction operator*(const Fraction& rational){
/* logic goes here * /
}
>>I need some help writing code for a stack using link list implementation without using the STL
So your first task is to create a linked list implementation. Do you know
how to start ? Try it and come back.
I want them to have access to the private section of the class.
The reason you might want friends is so you can do something like this:
Fraction frac(2,3);
Fraction f = 1/2.0 * frac;
But in your friend function is doing this :
Fraction f(3,4);
Fraction g(4,5);
Fraction r = f * g; //or g * f;
Since you only provide fraction as an argument to the friend function.
For your function a friend is not needed because implementing a simple Fraction operator*(double); will do the job.
Now if you prototype were this :
Fraction operator*(const double frac)const;
Then you should also provide a friend function with this prototype:
friend Fraction operator*(const double frac1, const Fraction& frac2);
That way you can do this :
Fraction f = 1/2 * aFractionObject; //uses the friend function
Fraction g = aFractionObject * 1/2; //uses aFractionObject.operator*(double);
Is there any way to reduce the number of times you have to write:
template<typename T>
for a bunch of functions all using the same template parameters?
i.e., something like this:
template<typename T>
namespace my_functions_using_T {T function_do_something(const T & t);
void function_do_something_else(T & t);
}
(besides making them all methods of a class... which seems heavy handed...?)
Nope. Don't be that lazy.
>>
friend Fraction& operator +(Fraction& c);
friend Fraction& operator -(Fraction& c);
friend Fraction& operator *(Fraction& a, Fraction& b);
Why do these need to be friends anyway?
You apparently have never used *nix. mv is a common *nix program that moves a file (or directory) from one place to another. Lots of *nix program have very cryptic names and arguments.
>>@vidit_x & @firstPerson & WaltP - .
As for closing files -- it depends. Like
return 0;
in main() you can elect to close it or not. I only close files if there is no other code to be executed, that is, if the function just returns after the file processing is finished. There are obvious times when the file(s) must be explicitly closed, such as when you want to delete the file. But I don't think that is what firstPerson is talking about. I doubt he meant "never" close files, only don't close them unless its otherwise necessary.
No I never used *nix.mv, but he has a chance to rename it
so people like me ( a young one, with only college experience )
can understand it better. Thats why I suggested to rename it
something clearer. Clearly renaming it moveFile rather than mv
sounds better.
As ancient dragon pointed out, I was not saying that to never close
the file explicitly. All I was saying is that when you can let the
destructor close the file then let it.
Why don't you use arrays ? For example his code :
ImageIcon c1icon = new ImageIcon ("clubs-2-75.jpg");
ImageIcon c2icon = new ImageIcon ("clubs-3-75.jpg");
ImageIcon c3icon = new ImageIcon ("clubs-4-75.jpg");
ImageIcon c4icon = new ImageIcon ("clubs-5-75.jpg");
ImageIcon c5icon = new ImageIcon ("clubs-6-75.jpg");
ImageIcon c6icon = new ImageIcon ("clubs-7-75.jpg");
ImageIcon c7icon = new ImageIcon ("clubs-8-75.jpg");
ImageIcon c8icon = new ImageIcon ("clubs-9-75.jpg");
ImageIcon c9icon = new ImageIcon ("clubs-10-75.jpg");
Can be substituted with this :
ImageIcon[] clubSuitIcon = new ImageIcon[10-2]
for(int i = 0; i < clubSuitIcon.length; ++i){
String card= "clubs-" + String.valueOf(i + 2) + "-75.jpg";
clubSuitIcon[i] = new ImageIcon(card);
}
Making similar changes can make your code much better.
Who the h--l taught you that? He should be shot.
Please don't help anyone else. You are teaching very bad habits -- even worse than instructors teaching Turbo-C and
conio.h
I am not sure behind your rational. Why do something twice, when one
is enough ? Maybe if you give a good example and reasoning then
I might see your point. Until then just saying that its a bad habit
doesn't cut it. Curious on what other people think about this.
P.S :
Just curious, because you disagreed with one of my rational, why do you say not to help anyone else?
Did you witness me "preaching" this to anyone else? Did you find any other advice that I gave bad advice?
>>Bad form. Open a file? Close the file. Period.
Adding extra code when not needed just clutters the code. I see
no point to close the file when the destructor will do the job, its redundant, not Bad Form.
>>Do you also never free dynamic memory because when the program exits it cleans up?
I don't use dynamic memory. I let STL handle those and let their
destructor free it.
1) Change you function name. What the hell is "mv" ? I wouldn't know
just by looking at the name.
2) There is no need to throw error. It just congests the code.
3) You should not use dynamic memory if memory on stack would do.
4) There is no need to close the file, it is done by the destructor.
5) You could make your code more readable, by seperating the reading
and writing functionality. Make a seperate function that reads the content. Make another function that writes the content. Then use them
in the "mv" function. And Change that function name, ASAP.
But this much better than your first post.
You cannot do that with regular arrays, sorry. But its a simple fix if you
use vectors.
Try something like this :
void doSomething(const int updateValue, const int maxLoop){
if(updateValue < 0 ) return;
for(int curr = 0; curr != maxLoop; curr += updateValue){
/* do something */
}
}
and call it like so :
int main(){
//stuff
whileGameIsNotOver(){
doSomething( player.updateSpeed(), Wall.MaxPosition());
doSomething(2,50); //loop statement 50 times, with increments of 2.
doSomething(2*enemy.speed(),75);
}
//stuff
}
int funcReturn() {
int a[3] = {33, 44, 55};
for( int i = 0; i < 3; i++) {
return a[i];
}
}
This is not doing what you think it is. After it hits the return statement
it returns. And every time you call this function, the "i" will be always 0.
Try using static like so( warning not a good way )
int funcReturn() {
static int index = -1;
index = (index+1) % 3;
int a[3] = {33, 44, 55};
for(;i < 3; i++) {
return a[i];
}
}
A better way is to pass an array to function. Also you are missing the
"int" part in "int main()". main has to be declared int by def.
I'm gonna post your code here :
#include <iostream>
using namespace std;
int ShowMenu();
int main()
{
int choice = 1;
// end if user picks 4 or with invalid entry, continue program otherwise
while ( choice != 9 )
{
do {
if (choice > 9 || choice <= 0)
{
cout << "\nAi da da! we don't have that option. Please select again. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
}
choice = ShowMenu();
} while ( choice < 0 || choice > 9 );
if ( choice != 9 )
switch (choice) // branch to an appropriate selection
{
case 1:
cout << "\nOption #1 was selected. distance english to metric. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 2:
cout << "\nOption #1 was selected. distance metric to english. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 3:
cout << "\nOption #2 was selected. weight english to metric. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 4:
cout << "\nOption #1 was selected. weight metric to english. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 5:
cout << "\nOption #3 was selected. volume english to metric. \n";
cout << "\nPress <Enter> key to continue ... ";
fflush(stdin);
cin.get();
break;
case 6:
cout << "\nOption #1 was selected. weight metric to english. \n";
cout << "\nPress <Enter> …
Can you compile a simple program with either openGL or direct3D?
>> i have forgotten how to do the setw for the output
You also forgot to use Code Tags. Thus I forgot to help, sorry.
>>It may sound harsh, but if you can't figure this one out, you might want to consider switching your major. It doesn't get any easier.
Well not everyone can be like us :).
Generally there are :
CE : computer engineering
CS : computer science
CSE : computer science engineering
Each one has its own percentage of software and hardware teaching.
For example, in my school CE is about 70% hardware and 30% software. CS is about 10% hardware and 90% software, and CSE
is about 50-50.
Do you have to insert after the index, before the index, or at the index.
Very nice. Now if we can only get what the problem is, then it would be nice of you.
You forgot to include the string header.
I believe this is useless:
currNode->next = head;
just take it. Also whats up with the index parameter in your addLast(). You are not
doing anything useful with it, so just remove it. And finally you are not returning anything with
your addLast() function if index >= 0.
If you provide a definition for the constructor for that class, you need
to define the constructor. That statement is what the error is saying.
For example :
Correct way
class Test{
int i;
public:
Test();
};
Test::Test(){ i = 10; }
Incorrect or wrong
class Test{
int i;
};
Test::Test(){ i = 10; }
My code I included only works out the bottom 10 digits so
9559449000
Guess you probably didn't need any help after all :)I don't know if you wanted a broader approach to say calculating...
the last 128 digits from the sum of 1000 numbers to a random power I can go through method two if it will help.
I would recommend a computing approach over the mathematical sequences as it is difficult to format maths without paper and pencil.
My signature is an exercise for the people willing to give it a try.
I just write it in a weird way. Anyway, nice job for the solution.
What does it supposed to do, make the user specify the shape of the
function? For example, is the input supposed to be something like this :
#####
# #
# #
#####
Should the user input a hallow box?
Do something like this :
//mainSrc is the source where the secondarySrc will compare from
//function returns the percentage of matching words
float checkList(const std::vector<string>& mainSrc, const std::vector<string> secondarySrc){
size_t matchingWords = 0;
//for each word in secondarySrc, check if mainSrc has the secondarySrc[i]. If so then increment matchingWords, else move forward
//return matchingWords/mainSrc.size();
}
Well not to be mean but think about it for a second. If I only use one step for the functions its going to go down then go back to the beginning then go left and back to the beginning.
The function fills the bottom row first, until it hits the wall.
When it hits the wall it fills the top row, then again when it hits the wall it fills the
left column, and then it fills the right column. When you subtract or
add by two, you might have invalid memory access. If it bothers you
that much, you can always change the orientation of how the
recursive function is called, for example you can call it like so :
fillArray(shape,row+1,column);//down
fillArray(shape,row,column-1);//left
fillArray(shape,row-2,column);//up
fillArray(shape,row,column+2);//right
As for your getShape function, I cannot help you because
I don't know what the requirements are.
I will suggest the following :
1) Create a vector
2) Fill the vector with numbers 0 through MAX
3) Find one fib number from the vector and delete it.
4) Find the next fib number and delete it.
5) Repeat 3-4 until MAX is reached
6) Print the vector
I'm not sure what your answer, but here is what I believe is the answer :
1^1 % 10e10 = 1
3^3 % 10e10 = 27
5^5 % 10e10 = 3125
7^7 % 10e10 = 823543
9^9 % 10e10 = 387420489
11^11 % 10e10 = 85311670611
13^13 % 10e10 = 75106592253
15^15 % 10e10 = 90380859375
17^17 % 10e10 = 86336764177
19^19 % 10e10 = 13589123979
21^21 % 10e10 = 21381124421
23^23 % 10e10 = 55032910567
25^25 % 10e10 = 33447265625
27^27 % 10e10 = 19149892803
29^29 % 10e10 = 16126483469
31^31 % 10e10 = 56044734431
33^33 % 10e10 = 37183380513
35^35 % 10e10 = 96435546875
37^37 % 10e10 = 70199442517
39^39 % 10e10 = 17009951959
41^41 % 10e10 = 25137953641
43^43 % 10e10 = 67198995507
45^45 % 10e10 = 28173828125
47^47 % 10e10 = 96385062863
49^49 % 10e10 = 29325062449
51^51 % 10e10 = 8231315051
53^53 % 10e10 = 1256150373
55^55 % 10e10 = 99365234375
57^57 % 10e10 = 4855688057
59^59 % 10e10 = 99552427939
61^61 % 10e10 = 48560431661
63^63 % 10e10 = 79138342847
65^65 % 10e10 = 83837890625
67^67 % 10e10 = 50052277723
69^69 % 10e10 = 92201741429
71^71 % 10e10 = 65129996471
73^73 % 10e10 = 90062013833
75^75 % 10e10 = 83544921875
77^77 % 10e10 = 42530996797
79^79 % 10e10 = 51339775919
81^81 % 10e10 = 31371782481
83^83 % 10e10 = 47212640587
85^85 % 10e10 = 84814453125
87^87 % 10e10 = 18054601383
89^89 % 10e10 = 36592384409
91^91 % 10e10 = 29136642691
93^93 % 10e10 …