|
Okay, so I've got that cleared. Now, I need to tackle all these 'floats', 'doubles', 'integers', etc. What are the names of these? Are they the 'return data types'?
They're just data types, they can be used to return but they're not 'return data types'
|
On June 29 2009 00:16 Uligor wrote:Show nested quote +On June 29 2009 00:15 tec27 wrote:On June 28 2009 23:47 AcrossFiveJulys wrote:On June 28 2009 23:42 konadora wrote: I don't really quite understand the concept of 'returning an integer', what does it mean?
Also, if you go for int main(), you must end off with a return 0; , but if you use void main(), you don't have to, right? no. if you make main's return type int, then you can return any integer you want. the point of this is so that a program that calls your main function (such as the program you are using to test it, like a terminal or visual studio) can retrieve the return value. you can use this communication to return error codes... i.e., return 0 if all goes well, or return > 0 if an error occurred. if you make main void, then it has no return type, and will always return 0 to the caller. Just to be clear here, making main type void makes it return *no* value which is not at all the same as returning 0. At a lower level, the return value of a function is usually stored in a register, therefore, if the operating system (which in this case acts as a 'calling function' expects main to be of type int and calls it that way, it will receive an effectively random value in return if it is type void. As you might imagine, thats a bad thing that just happens to turn out okay sometimes. In short, make your main function type int. You're wrong, the compiler automatically makes the main function return 0 when it's specified as void. (xor eax, eax before ret)
That was my understanding as well. To his defense, of course, if you are using a compiler which does not do this, you might have problems. But overall this is a very petty concept.
|
konadora
Singapore66063 Posts
My teachers didn't explain any of these differences, just gave us terms, they're like, 'put these here and here, you get a program. now try it'.
That's why... :S
What data type do I use if I want to use a fraction? I'm currently doing this exercise where I'm supposed to convert inputted Farenheit temperature into centigrade and vice versa, and I need to use the fraction 5/9.
|
On June 28 2009 23:49 konadora wrote: Oh I see, so it's just that int main() allows you to identify whether your program has an error or not, but void main() can't?
This is essentially true. Some people prefer to always use int as it is "good form." Honestly though, if you don't understand the difference yet, which one you use will make no difference at all.
As for the second program, you read in a NRIC, which is apparently some sort of barcode or something. You then check to see if it's valid by using a check digit, a computable extra number added to the string:
int weight[8] = { 0,2,7,6,5,4,3,2 }; for (int i=1; i<=7; i++) sum = sum + ( nric[i] - '0' ) * weight[ i ];
// nric[ i ] is the ith character of the nric // nric[ i ] - '0' is a kind of kludge that will turn a character representation of a digit into a numerical represention of the same digit. That is '9' - '0' = 9 and '6' - '0' = 6, et cetera. Obviously '0' - '0' = 0. // You then multiply each digit of the nric by some pre-determined "weight" and add them up. // This algorithm is arbitrary, but as long as it's reproducible any check-digit algorithm is fine.
char digit[ 12 ]= { 'x', 'A', 'B','C','D','E','F','G','H','I','Z','J' }; int checkdigit = 11 - ( sum % 11 ); if( digit[ checkdigit ] == nric[ 8 ] )
// You then take the resulting sum and use modulus arithmetic to reduce it to the range [ 1, 11 ] // Then you look up the character with that index (from 1 to 11) in the "digit" array to get the 'secret' character that belongs with this string. If they match, you then output saying so. Otherwise you output an error.
Examples:
If your NRIC is 12345678H then your sum is 2*2 + 3*7 + 4*6 + 5*5 + 6*4 + 7*3 + 8*2 = 135 (Note that you ignore the first character, since its index is 0 and i starts from 1) Then you see that 135 % 11 = 3, so your "check digit" is 11 - 3 = 8. So you then look up 8 in the digit[] array to get 'H'. You compare the calculate check digit with the check digit at the end of the number. Lo and behold, a match!
Now, if your NRIC is 93843248B then your sum is 2*3 + 3*8 + 4*4 + 5*3 + 6*2 + 7*4 + 8*8 = 165. Then you see that 165 % 11 = 0, so your check digit is 11 - 0 = 11. You look up digit[ 11 ] to get 'J', and compare that to the check digit on the end of the NRIC. They don't match, so clearly I inputted my number wrong.
|
On June 29 2009 00:13 konadora wrote:Show nested quote +On June 29 2009 00:09 treason wrote:On June 28 2009 23:36 konadora wrote:#include <iostream> #include <string> using namespace std;
void main() { char digit[12]= { 'x', 'A', 'B','C','D','E','F','G','H','I','Z','J'}; int weight[8] = { 0,2,7,6,5,4,3,2 }; string nric; int sum=0; // init cumulative sum to zero
// get NRIC input cout << "Enter NRIC: "; getline (cin,nric); cout << nric << endl; // calculate check digit for (int i=1; i<=7; i++) { sum = sum + (nric[i]-'0')*weight[i]; } cout << sum << endl; int checkdigit = 11 - (sum%11); // check if the user's NRIC check digit matches to confirm valid nric if (digit[checkdigit]==nric[8]) cout << "valid NRIC number" << endl; else cout << "Invalid NRIC number" << endl; } char digit[12]= { 'x', 'A', 'B','C','D','E','F','G','H','I','Z','J'};creates an array of characters with the length of 12, digit[0] == 'x', digit[1] == 'A', ... , digit[11] == 'J' int weight[8] = { 0,2,7,6,5,4,3,2 };same with integer values cout << "Enter NRIC: "; getline (cin,nric);here you get an input from the user, he types something on his keyboard, and the input is saved in the string (string = array of char) nric. for (int i=1; i<=7; i++) { sum = sum + (nric[i]-'0' )*weight[i]; }this for-loop iterates through the array the user has typed. Its strange, that you dont start with i = 0, since arrays in C, Java and C++ are beginning with the index 0. I need help with explanation on the 'arrays' part, I can't find the notes online on my school's e-learning platform. Step-by-step explanation would help so much, I would love you for life An array is a field of values. Lets say, you want to save 5 numbers: int number1, number2, number3, number4, number5; number1 = 0; number2 = 3; number3 = 7; number4 = 12; number5 = 1; Thats really annoying, you have to write alot and you can make some mistakes if you are sleepy :D Thats why arrays were invented:
int example[5]; example[0] = 0; example[1] = 3; example[2] = 7; example[3] = 12; example[4] = 1;
Thats also to long, you can write it in a shorter way: int example[] = {0,3,7,12,1}; the first line int example[5]; declares a field of integer values with the length of 5, in the next 5 lines i put some values into this field. Now, 5 values are saved into this field, and you can access its content via the [ ] operator. If you want to print the first number, you can do: cout >> number1>> endl; and with the array: cout >> example[0] >> endl; Another good thing about arrays is, that you can use a for-loop the access all contents of the field: for(int i = 0; i<=4; i++){ cout >> example[i] >> endl; } will write: 0 3 7 12 1
on the screen.
|
konadora
Singapore66063 Posts
@ms291052:
// nric[ i ] - '0' is a kind of kludge that will turn a character representation of a digit into a numerical represention of the same digit. That is '9' - '0' = 9 and '6' - '0' = 6, et cetera. Obviously '0' - '0' = 0. // You then multiply each digit of the nric by some pre-determined "weight" and add them up. Why is the -0 necessary? (Great first post btw, thanks lol)
@treason: so if you put number[5], you'll have 5 different values, but if you want to access the nth number, you must use number[n-1]?
Also, this part
for(int i = 0; i<=4; i++){ cout >> example[i] >> endl; }
Means that something like:
First, display the 0th line's number, and since it's not greater than example[4], display 0+1 (making it 1th) line's number, and continue until it reaches i = 4 (where it cannot exceed i = 4 and ends there), right?
|
On June 29 2009 00:32 konadora wrote:@ms291052: Show nested quote +// nric[ i ] - '0' is a kind of kludge that will turn a character representation of a digit into a numerical represention of the same digit. That is '9' - '0' = 9 and '6' - '0' = 6, et cetera. Obviously '0' - '0' = 0. // You then multiply each digit of the nric by some pre-determined "weight" and add them up. Why is the -0 necessary? (Great first post btw, thanks lol) @treason: so if you put number[5], you'll have 5 different values, but if you want to access the nth number, you must use number[n-1]?
The character '0' has a different value than int 0 (char '0' is in fact 48 int, '1' is 49 etc). But when you subtract the character with '0', you essentially do -48. So when the character is '1' (49), it becomes 49-48 = 1.
|
konadora
Singapore66063 Posts
On June 29 2009 00:38 Uligor wrote:Show nested quote +On June 29 2009 00:32 konadora wrote:@ms291052: // nric[ i ] - '0' is a kind of kludge that will turn a character representation of a digit into a numerical represention of the same digit. That is '9' - '0' = 9 and '6' - '0' = 6, et cetera. Obviously '0' - '0' = 0. // You then multiply each digit of the nric by some pre-determined "weight" and add them up. Why is the -0 necessary? (Great first post btw, thanks lol) @treason: so if you put number[5], you'll have 5 different values, but if you want to access the nth number, you must use number[n-1]? The character '0' has a different value than int 0 (char '0' is in fact 48 int, '1' is 49 etc). But when you subtract the character with '0', you essentially do -48. So when the character is '1' (49), it becomes 49-48 = 1. Oooh, okay thanks, didn't know that!
|
konadora
Singapore66063 Posts
My next question:
What data type do I use if I want to use a fraction? I'm currently doing this exercise where I'm supposed to convert inputted Farenheit temperature into centigrade and vice versa, and I need to use the fraction 5/9.
|
On June 29 2009 00:48 konadora wrote:My next question: Show nested quote +What data type do I use if I want to use a fraction? I'm currently doing this exercise where I'm supposed to convert inputted Farenheit temperature into centigrade and vice versa, and I need to use the fraction 5/9.
float or double.
|
konadora
Singapore66063 Posts
Isn't double for numbers with decimal points? What's the difference between float and double?
(Sorry if these questions are very basic, but no revision + 1 month of missing school + bad teacher = sucky at language)
|
On June 29 2009 00:16 Uligor wrote:Show nested quote +On June 29 2009 00:15 tec27 wrote:On June 28 2009 23:47 AcrossFiveJulys wrote:On June 28 2009 23:42 konadora wrote: I don't really quite understand the concept of 'returning an integer', what does it mean?
Also, if you go for int main(), you must end off with a return 0; , but if you use void main(), you don't have to, right? no. if you make main's return type int, then you can return any integer you want. the point of this is so that a program that calls your main function (such as the program you are using to test it, like a terminal or visual studio) can retrieve the return value. you can use this communication to return error codes... i.e., return 0 if all goes well, or return > 0 if an error occurred. if you make main void, then it has no return type, and will always return 0 to the caller. Just to be clear here, making main type void makes it return *no* value which is not at all the same as returning 0. At a lower level, the return value of a function is usually stored in a register, therefore, if the operating system (which in this case acts as a 'calling function' expects main to be of type int and calls it that way, it will receive an effectively random value in return if it is type void. As you might imagine, thats a bad thing that just happens to turn out okay sometimes. In short, make your main function type int. You're wrong, the compiler automatically makes the main function return 0 when it's specified as void. (xor eax, eax before ret) I'm sorry, but you're wrong except for very specific compilers. Microsoft's compiler does this, but the standards do not support that. It was merely done to make bad code that they/other people had written in the past still work.
If you hop over to g++, however, you'll find that you can't even compile a program with 'void main':
voidmain.cpp:1: error: '::main' must return 'int'
Now, what is within the standards is to auto-return 0 for main functions declared as type int (IE: if no return statement is specified inside). While this is still bad practice, it should work on all standards-compliant compilers.
In short, don't encourage someone to not follow standards on a simple matter like this just because a certain compiler will fix the mistake for them. Its incredibly bad practice, and this is the kind of thing that, even though its small, will make lots of people have zero respect for you and your code.
|
On June 29 2009 00:53 konadora wrote: Isn't double for numbers with decimal points? What's the difference between float and double?
(Sorry if these questions are very basic, but no revision + 1 month of missing school + bad teacher = sucky at language) Both double and float are for numbers with decimal points. A double has more precision than a float (IE: can store longer decimals), but is also therefore larger in memory.
Edit: Also, you probably don't need to use a floating-point format at all, you can just plug the 5/9 into the calculation itself.
IE: int celsius = (int)((farenheit - 32) * 5 / 9);
Should work (although it will be truncated to an integer).
|
konadora
Singapore66063 Posts
On June 29 2009 00:58 tec27 wrote:Show nested quote +On June 29 2009 00:53 konadora wrote: Isn't double for numbers with decimal points? What's the difference between float and double?
(Sorry if these questions are very basic, but no revision + 1 month of missing school + bad teacher = sucky at language) Both double and float are for numbers with decimal points. A double has more precision than a float (IE: can store longer decimals), but is also therefore larger in memory. Oh okay, understand now.
Thanks for all the responses so far, I just need to refresh my memory since I did practice quite a bit... a few months ago. Currently doing an exercise, will post questions once again here if I get stuck, can't say this enough but thanks again!
|
On June 29 2009 00:53 konadora wrote: Isn't double for numbers with decimal points? What's the difference between float and double?
(Sorry if these questions are very basic, but no revision + 1 month of missing school + bad teacher = sucky at language)
double and float are for numbers like 1.234, yes. The difference between double and float is float is 32 bit and double is 64 bit.
Maybe I misunderstood your question, you wanted to know what datatype you should use in an algorithm to convert Farenheit temperature into centigrade and viceversa? Or did you want to know how to output a fraction in the console?
On June 29 2009 00:56 tec27 wrote:Show nested quote +On June 29 2009 00:16 Uligor wrote:On June 29 2009 00:15 tec27 wrote:On June 28 2009 23:47 AcrossFiveJulys wrote:On June 28 2009 23:42 konadora wrote: I don't really quite understand the concept of 'returning an integer', what does it mean?
Also, if you go for int main(), you must end off with a return 0; , but if you use void main(), you don't have to, right? no. if you make main's return type int, then you can return any integer you want. the point of this is so that a program that calls your main function (such as the program you are using to test it, like a terminal or visual studio) can retrieve the return value. you can use this communication to return error codes... i.e., return 0 if all goes well, or return > 0 if an error occurred. if you make main void, then it has no return type, and will always return 0 to the caller. Just to be clear here, making main type void makes it return *no* value which is not at all the same as returning 0. At a lower level, the return value of a function is usually stored in a register, therefore, if the operating system (which in this case acts as a 'calling function' expects main to be of type int and calls it that way, it will receive an effectively random value in return if it is type void. As you might imagine, thats a bad thing that just happens to turn out okay sometimes. In short, make your main function type int. You're wrong, the compiler automatically makes the main function return 0 when it's specified as void. (xor eax, eax before ret) I'm sorry, but you're wrong except for very specific compilers. Microsoft's compiler does this, but the standards do not support that. It was merely done to make bad code that they/other people had written in the past still work. If you hop over to g++, however, you'll find that you can't even compile a program with 'void main': Now, what is within the standards is to auto-return 0 for main functions declared as type int (IE: if no return statement is specified inside). While this is still bad practice, it should work on all standards-compliant compilers. In short, don't encourage someone to not follow standards on a simple matter like this just because a certain compiler will fix the mistake for them. Its incredibly bad practice, and this is the kind of thing that, even though its small, will make lots of people have zero respect for you and your code.
It is true that it is compiler specific (I use MSVS08 and I was under the impression kona used this as well, hence the windows explanation of what happens with the exitcode). I apologize because I used "the compiler" instead of specifying the compiler I was talking about, this made it seem like I was belittling your knowledge of compiler specifics.
|
konadora
Singapore66063 Posts
Maybe I misunderstood your question, you wanted to know what datatype you should use in an algorithm to convert Farenheit temperature into centigrade and viceversa?
Yup, which datatype.
Also, fyi, I'm using Microsoft Visual C++ Express Edition.
|
Also, sorry if I'm coming off as rude, Uligor. I just think its a really bad idea to teach newer programmers to use 'void main()' before they even know how it affects anything, since its super-easy to learn the other way at the beginning, but somewhat harder to fix old habits later on.
|
konadora
Singapore66063 Posts
Okay, I've managed to complete the exercise, using this:
#include <iostream> using namespace std;
int main() { double c,f; cout << "Please enter the temperature in Centigrade." << endl; cin >> c; f = ((c*1.8)+32); cout << "The temperature is " << f << " degrees Farenheit." << endl;
cout << "Please enter the temperature in Farenheit." << endl; cin >> f; c = ((f-32)*5/9); cout << "The temperature is " << c << " degrees Centigrade." << endl;
return 0; }
But I want the user to choose which one he's inputting first (whether he's going to input Farenheit or Centigrade), and use that, and end the program. Which loop do I use, and how?
|
I don't think you need to use a loop for that, just use an if/else statement. Just input a character to decide which one to use or whatever and then check it:
if(myChar == 'c' ) { // do Celsius -> Farenheit calculation } else if(myChar == 'f' ) { // do Farenheit -> Celsius calculation } else { // Tell the user to follow instructions, better luck next time }
|
konadora
Singapore66063 Posts
What is the myChar?
Or is that the 'character'? lol
|
|
|
|