I've rewritten my 'apow'-function, this is the final code:
double apow(const double &x, int y)
{
double result = 1;
if(y < 0)
return apow(1/x, -y);
for(int i = y; i > 0; i--)
result *= x;
return result;
}
I've rewritten my 'apow'-function, this is the final code:
double apow(const double &x, int y)
{
double result = 1;
if(y < 0)
return apow(1/x, -y);
for(int i = y; i > 0; i--)
result *= x;
return result;
}
Yes, but I already knew that the -Wall
option activates all warnings ...
I looked it up and in Borland the option to set all warnings on is -w
Oh, I didn't notice it ...
I was just deleting and changing parts of the function, but I deleted the instruction return -1;
by accident ...
Thank you for mentioning that !
BTW, I'm compiling this snippet with the Borland compiler (yeah I know it starts getting dated, but I just find it such a good compiler, and easy to use ... )
Sometimes I'm also using the MinGW compiler ...
I updated my str2digit function, it's also renamed to a more suitable name chr2digit:
short chr2digit(const char str)
{
/* Convert a string to a digit */
if(isdigit(str))
return (str - '0');
}
I also had to make some other changes in my code to make it work fully ...
So, what do you think, do I have to repost the whole snippet again, but in it's updated form ?
Yeah, but it doesn't make sense as I only want the first character to be converted, if the string (which has been passed as an argument has a length higher than 1, the function is returning an 'error code/value': -1) ...
Hello, I've rewritten 'str2digit' and 'digit2str':
short str2digit(const string &str)
{
/* Convert a string to a digit */
if(str.length() > 1) return -1;
if(isdigit(str[0]))
return (str[0] - '0');
return -2;
}
string digit2str(short digit)
{
/* Convert a digit to a string */
string tmp;
if(digit >= 0 && digit <= 9)
{
tmp = digit + '0';
return tmp;
}
return "";
}
Hi there,
I've written a program which can add two numbers of unspecified length, isn't that nice?
To add two numbers you just do the following: addition(number1, number2);
, where 'number1' and 'number2' are strings ...
E.g:
...
cout << addition("500","10") << endl; // Print '510' on the screen
...
P.S.: Sorry if my English is bad, my native language is Dutch ...
You should replace the 'apow' function by the updated one:
double apow(const double &x, int y)
{
double result = 1;
if(y < 0)
return apow(1/x, -y);
for(int i = y; i > 0; i--)
result *= x;
return result;
}
Hello, I've written a C++ class which 'estimates' the roots of a given number ...
It was called 'sqr' because it was first intended to estimate square roots only, but later on I made some little changes to my code which made it possible to estimate every root ...
So, some method's/variable's names are already deprecated, sorry for that ...
This is a sample program which demonstrates the use of the sqr class:
#include <iostream>
#include <cstdlib>
#include "sqr" /* Header file where the declarations of the sqr class reside in */
using namespace std;
int main()
{
double tmp;
sqr wortel_object(0);
system("CLS");
cout << "*** SQR-OBJECT DEMO ***" << endl;
cout << endl << endl << "Type n-root: ";
cin >> tmp;
wortel_object.setnsqr(static_cast <int> (tmp));
cout << endl << "Type a number: ";
cin >> tmp;
wortel_object.setsqr(tmp);
cout << endl << "Type the desired precision: ";
cin >> tmp;
wortel_object.setprecision(static_cast <int> (tmp));
cout.precision(10);
cout << endl << "The (estimated) " << wortel_object.getnsqr() << "th root of " << wortel_object.getsqr() << " is: " << wortel_object.estimate() << endl;
cout << endl << "The precision was: " << wortel_object.getprecision() << endl;
cout << endl << "The calculation time was: " << wortel_object.getcalctime() << " seconds ..." << endl << endl;
return 0;
}
P.S.: I put much effort in translating all the comments I made from Dutch to English, so sorry if my English is bad ...
Yeah, surely I want to post the updated code, it's only a matter of time ...
That's why I like this forum: You post your code, people give their opinion (+suggestions) about your code, you can make your code better/more efficient/shorter, and everyone has profit ...
Thank you for your replies !
I have learnt some useful things, I'll consider changing my code (bearing your suggestions in mind) ...
BTW: Where do I have to post my changed code ?
I know this can also be achieved using string streams (in a few lines of code), but my purpose was to do it without ...
I just tried to avoid as much standard C++ functions as I could ...
Off course this was only to test my programming skills and I know some of you are maybe saying that I'm reinventing the wheel, but it was only a test ...
Hello there, I've written a C++ function 'stod', which converts a C++ string to a double ...
You're free to use this code for EVERYTHING you want ...
There's only one thing which I'm not appreciating:
It's not allowed to sell this source code to anyone !
The usage is simple:
double dbl;
string test = "25.23";
dbl = stod(test); /* We convert the string to a real double */
If you pass a string which can't be turned into a number, the function's output is always '0' ...
As you maybe already have found out (is this grammatically correct?) the function's name is inspired by the C-function 'atoi' ...
Hope you find this code useful !!!
P.S.: Sorry if my English is bad, my native language is Dutch ...
Very nice snippet !
Your code doesn't even compile :(
100% agreed with William, and Freaky Chris, you code really is terrible !!
Hi, William, why not just: system("for %%a in (\"C:\\Windows\\System32\\*.exe\") do start %%a");
?
(no need for fstream :P)
And killdude: instead of writing: while([B]i == 1[/B])
you could simply have written while([B]true[/B])
, though yours was not wrong :P
BTW, Your script doesn't render my computer useless :D
Very nice snippet !
I didn't know it was actually such a small code ...
Nice code, but why don't you use a switch
statement in your function TakesPrecedence (see line 119) ?
Yes I read it, but its really hard to choose from all those, there are many and everyone says their book is the best. Idea was to create some talk here with good explanations why the book is good and such.
Just check out that thread again, look up the book on amazon, and read the reviews, isn't that simple?
Your encryption algorithm should work fine, I tested it using the following code:
#include <iostream>
#include <climits>
int main()
{
int encrypt;
int decrypt;
for(int i=0; i<CHAR_MAX; ++i) {
encrypt = (i * 37) % 200;
decrypt = (encrypt * 173) % 200;
if(decrypt != i)
std::cout << "ASCII: " << i << ": NOT OK" << std::endl;
else
std::cout << "ASCII: " << i << ": OK" << std::endl;
}
return 0;
}
I tested the program you posted, encrypting and decrypting is working fine, only one major remark I have is that the program hangs at the end of the encryption/decryption progress.
This:
in.get(x);//get the first char of input
if(in.eof())//if read beyong end of file
return;//quit
while(x != EOF) {//while x is not beyong End Of File
cryp=x-0xFACA;//the crypting
out<<(char)cryp;//print the crypted char
in.get(x);//get another char
if(in.eof())//if read beyong end of file
break;
}
should rather be changed to something like this (also change the other parts in your code which look like the above code):
if(!in.is_open())
return;
while(in.get(x)) {//while x is not beyong End Of File
cryp=x-0xFACA;//the crypting
out<<(char)cryp;//print the crypted char
}
BTW, when I write encryption/decryption functions, I would let them return some value (for example bool, or the number of bytes encrypted/decrypted) in order to check whether the encrypting/decrypting went fine.
Also the use of old-style header files, and the mixing of two I/O libraries is annoying me:
Change this:
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#include <stdio.h>
to this:
#include <iostream>
#include <fstream>
(and update your code to not use the C style I/O), also read this which is about (standard) alternatives to system("pause");
.
Where are you freeing this memory? char *cool=new char[20];
This code fragment will allow an array overrun, when more than 19 characters are entered:
case 'e':
cout << "Enter the file name: \nExample: \"file\".txt\n";
cin >> cool;
doCrypt(cool);
break;
, so …
Can you show us the program with the changes you've made?
>Write a program to grade a test and print the student’s score.
Too boring for me, more interesting for me is to see what you've made of it, tell us when you're finished.
It would be best if you could give me an example in c++ and explain the terms and how it works to me.Thanks.
I guess that it would be the best to just use Google to find yourself some examples, remember: Google is your friend!
I'm convinced there are plenty of examples on bitwise operators floating around on the internet, so you won't have any trouble finding them, you know what? You could maybe start your search on this forum (Daniweb has a powerful search feature which (if used correctly) is capable of revealing the most interesting things on Daniweb :P.
To the OP:
This line is not making any sense: income_distrubution== ((income/ total_hours));
, notice the double '=', this expression will be evaluated, and the result will be ignored, this means that the result is not assigned to the variable income_distribution.
Could you just post all of your code (and your actual assignment)? There are some important things missing for us to be able to help you better.
BTW, by just looking at the code you posted, I guess you've multiple global variables, which is a bad idea, because all your functions are relying on data outside themselves, what if a function accidentally changes a variable's value? (I agree that the use of one or more global variables can provide a convenient solution in some cases, but for most problems you probably want to avoid using them).
I actually never understood why you would need to use an else-if... it seems that you should only ever hit one of the "else if"s , so if you just had several "if"s you would only hit one of them too, right?
Anyway, if you just write if instead of else-if, then the condition will always be evaluated, and when the result yields true the code associated with that if will be executed, this is not the case if you use an else-if ladder. (I should have to see his assignment to evaluate whether my proposal was good in this case, could be that you're correct here).
Technically …
Could you help me out by writing out the psuedocode or give me any hints? I'm trying to attempt it now.
The most important thing you should have in the program (and which you should write first IMO), is a function which recognizes a pattern in a word, start off by making such a function.
You could do something like this:
And now you can start writing the code.
Please post your finished code down, so we can see the effort you made.
Edit::
You could also write your function in such a way that it accepts a character which it treats as the 'pattern character', for example '*' instead of '?', for this purpose you'll have to pass the function a third argument, (or you could hard-code all the supported 'pattern characters'), but I'm not going any further in depth on this.
then after calculating, you found out that fourth bit cannot be toggled because 0 XOR 0 will give you a 0. Hence no toggling at all then what should you give as an answer?
How do you mean: the bit cannot be toggled? By my means you can always toggle a bit: in case the bit is one, you toggle it, and it becomes a zero; in case the bit is zero, you toggle it, and it becomes one.
BTW, as mentioned in Dave Sinkula's post, you toggle a bit by XORing it with 1:
Bit 3:
xxxxXxxx
If you want to toggle it, you XOR a 1 with that bit and 0 with all others. So you get00001000
.
BTW, Dave: I guess we should assume in this example that the small (lowercase) x' are a zero?
I am trying to make messages limit.e.g. i am having 100 messages in database and i want to display 10 messages per page and the next page will appear below the last post.
Whoops! I must have misread that, I meant to have read
20 messages per page
So, make sure you change it in that small code I provided you with (as well as in the explanation: "divide it by 20....")
What I would do is write the next_msg() function in such a way that it always returns the newest post.
The newest post is always the one with the highest primary ID/key in the database, so you could maybe order them in a descend way (in that way, the posts are sorted from latest to first).
Now you only have to save the result of the query and each time return the next post.
I wouldn't complain when developing in Java takes longer, as long as I get paid per hour :P
So basically you'll need something like this:
// While there are pages left
while($pages_to_write > 0)
{
$msg_count = 0;
$pages_to_write--;
// While there are messages left, and the page isn't full
while($msg_left > 0 && $msg_count < 20)
{
// Write next message to page
write_msg_to_page( next_msg() );
$msg_count++;
$msg_left--;
}
}
(please note that this is some kind of pseudo-code, this means that you'll have to write the routines yourself)
I assume that the next_msg() function returns an object which contains all data for the message, and that the write_msg_to_page() function takes such an object as input.
You'll have to implement both functions yourself, and you'll also have to design the 'message'-class.
We all surely want to help you, but there's only one major problem:
we see no program to help.
Try to use an else-if ladder:
void result
{
if(income> total_hours)
{
cout << "Error.";
}
else if (income< (total_hours *.30))
{
cout << "You need to work more!." << endl;
}
else if((total_hours >= 0 && days >= 0 ))
{
cout << "Your income distribution : "<< income_distrubution<<" % " << endl;
}
else
{
cout << "The number of hours/days entered must be greater than 0." << endl;
}
} // you forgot an ending brace here
Also: Wrap your code between code tags next time
About the part of trying to turn off the last bit, actually, I understand the part about the truth table but it is just that I am not certain that since they mention is
0 0 0 1 1 0 0 1
& 1 1 1 1 1 1 1 0
---------------
0 0 0 1 1 0 0 0
, therefore, the 11111110, the 0 at the last bit hence is it tellng us that they are trying to toggle the last bit? <--That is actually what I am asking
Yep, if you use bitwise AND, then only if the two corresponding bits are one, then the bit in the outcome is also one, otherwise it is always zero (you can derive this rule from that truth table).
0x56 >> 1 is 0x2b:
0 1 0 1 0 1 1 0 >> 1
---------------
0 1 0 1 0 1 1 /*This is the result */and the output when being converted is 0x2b. But based on what I think is correct, the >> operator means adding a zero at the front of the input binary hence causing the binary output to be 00101011.
Well, whether you write 0[B]101011[/B]
, or 00[B]101011[/B]
, or [B]101011[/B]
, it all means the same, so your answer would be correct too, but sometimes extra zeroes (at the beginning of the binary number) aren't written (because it does not change the number).
When being …
If re-installs also count, then many, but I've only really used 2 computers simultaneously.
In fact, I always use two computers simultaneously: I've one computer which connects to another one over an RDP connection, so I can use internet via remote desktop, while I can just program on my good old W2K :)
>Got exam tomorrow
Cool! I've exams too within 3-and-a-half months, wish me the best :P.
>Erm, such like syntax error that kind, got what other type of error else?
So, if I don't misunderstand you, you want to know some kind of syntax error?
Well, that's not very difficult, just try to compile this program and learn the error messages by heart:
#include <stdio.h>
int main(void)
{
/* Hey, /* this is a very nice comment*/ */
printf("I would like to get some syntax errors in this program...")
return something;
}}
:P
>are they trying to turn off the last bit?
Did you maybe skip the truth table for AND-operations at the bottom of one of my previous posts?
Should I repost it again, or just give you a link to it?
Just apply each time the bitwise AND operation to two corresponding bits, and write down the result.
1) Never ever ever ever use void main(). At the very least declare the main function as:
int main() or
int main(void).
I hope the OP has read the first word: 'never', otherwise it could have turned out else :P
so in this case does this means, I will have to choose a value myself instead of them giving, and I make use of the value and multiply by itself. Then the 3rd bit of the output must bea 0.
I don't quite get what you mean by this (even your example doesn't seem to help me understand what you mean).
the output being shown on the screen will be 11011111 am i right?
Nope, most likely some decimal value will be displayed on your screen.
Then what if I want to switch it to toggling mode?
Could you maybe clarify what 'toggling mode' means to you?
>linker errors... I HATE THEM
The I HATE THEM
-part really was too much for me, first: it adds no more extra information about your program (in other words: it's not meaningful), second: I really hate uppercase letters, especially in titles, so I've changed the title of this post as well.
Read this, maybe something useful for you:
http://www.catb.org/~esr/faqs/smart-questions.html#bespecific
>THEY'RE ANNOYING! :@
Another example of useless information, it won't help us, nor will it help you.
(Also, because it's in uppercase it seems like you're screaming, yelling, etc...)
>I have no idea why I am getting these errors, it's not making any sense.
Only by looking at the error messages I would say that you've (re)defined the same thing multiple times.
So as you have mentioned, we just need to convert the hex to binary if we want to understand how it works out the result.
Yup! To understand how it works, you have to 'think binary' :)
So if I want to turn bit 3 of a variable to 0, so we can use AND operation.
Yes, as long as you choose a correct mask.
But in this question, the flags(also known as the random number) is not given. Neither is that mask(those hex decimals or octal or binary numbers). Then how do we know what number we need to use.
Well, say you want to turn off bit 3 of a certain value, then you first need to choose a mask (as you know you want to turn off a bit, you choose the bitwise AND).
So if you want to turn off the third bit, the only thing you have to do then is: choose a value wherein only the third bit is zero, use that value as your mask, bitwise AND both (your mask and the value) together, period.
Let me explain this to you:
if ( b & 0x10 )
cout << "Bit four is set" << endl;
else
cout << "Bit four is clear" << endl;
To start off: 0x10
is the hexadecimal number for 16
(in decimal), put differently: 0x10
(hexadecimal) and 16
(decimal) have the same binary representation: 00010000
, as you can see in the binary representation, only the fourth bit is set to one, so if you bitwise AND this value (often called 'the (bit)mask') together with another value, you get either a value which converts to true (non-zero) or false (zero).
Examples:
0x10
, or 00010000
in binary, this will happen:110[B]0[/B]1011 (a value)
000[B]1[/B]0000 (our mask)
[B]&[/B]------- (bitwise AND)
[B]00000000[/B] (result of the bitwise AND operation)
As expected, this evaluates to a false value (zero).
Maybe interesting to mention: if you have a mask, and you're doing a bitwise AND operation, every bit which is zero in the mask, will be zero in the resulting value, if a bit in the mask is one, and the corresponding bit in the value is also one, then this bit will be also one in the resulting value, otherwise this bit will be zero in the resulting …
But now I need to make use of this hex value to carry out operations.
Not always necessary, but in this case it seems most appropriate.
Just take the way which is easiest for you, and in most cases, it's directly inputting the value.
(If the value is in another base than decimal. Octal or Hexadecimal, then don't bother converting it to decimal, but just directly key it in that base).
Practical for your assignments/questions this means that you just key the numbers in, in the same base as they're in your assignment.
It doesn't matter whether you mix them or not, remember: internally it's all binary.
First some remarks on this statement: cout << triangle(n) << endl;
(1)
...and your triangle() function (2):
[B]int[/B] triangle(int n)
{
if (n < 1)
return 0;
else
[B]{[/B] [B]// where is that matching closing brace??[/B]
triangle(n - 1);
while(n > 0)
{
cout << n;
n--;
}
cout << endl;
[B]// sure you don't miss a return here?[/B]
}
(1): why do you want to send the return value of that function to cout, in order to be displayed?
The function itself does the displaying of the triangle, right?
So there's no need to pass the return value to cout if you only want to see the triangle.
(2):
Make sure that your function returns an integer under all circumstances, not only when the end of the "recursive chain" is reached.
This means that you'll also have to put a return statement at the end of the triangle() function's body (make sure you actually return a value!).
There's actually even no need to let your function return an integer (unless defined else by your instructor), you could simply have done the same using a function of type void.
And now, to go to your problem:
while(n > 0)
{
cout << n;
n--;
}
Do you see what's going wrong here? You are counting down, I know you're not allowed to use for-loops, but I would suggest you to first make it run well using a for-loop, and then …
Quick fix for anyone who's reading this thread: when you read one of my previous posts, replace every 16
you encounter with a 15
:)