#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int sum;
int rem;
cout << "division, type in a number \n";
cin >> a;
cout << "tpye in another number \n";
cin >> b;
rem = a % b;
sum = a / b;
cout << "your number is " << sum << " your rem is " << rem << endl;
return 0;
}
My first (sorta) program in C++
Page 1 of 18 Replies - 237 Views - Last Post: 28 January 2013 - 08:31 PM
#1
My first (sorta) program in C++
Posted 21 January 2013 - 10:45 PM
Hey this is my first program using C++ any tips to a newbie would be appeciated
Replies To: My first (sorta) program in C++
#2
Re: My first (sorta) program in C++
Posted 21 January 2013 - 10:46 PM
What specific problems or errors are you encountering?
#3
Re: My first (sorta) program in C++
Posted 21 January 2013 - 10:47 PM
#4
Re: My first (sorta) program in C++
Posted 21 January 2013 - 10:56 PM
nice program keep it up
prob worth at LEAST couple hundred thousand dollars
prob worth at LEAST couple hundred thousand dollars
#5
Re: My first (sorta) program in C++
Posted 21 January 2013 - 10:58 PM
Well, consider your a to be 3 and b to be 2
on execution of line 19, you will get the value of sum to be 1 which is not the correct answer.
So, it would be better if you declare sum as float so that there won't be any truncation of data.
Also, you need to replace your line 19 to be like this.
One more thing, your variable names must be logically sounding correct...in your case, sum does nothing even closely related to addition, which it actually means!!
A small suggestion would be to replace the name sum by quo, as it is the quotient you are getting.
regards,
Raghav
on execution of line 19, you will get the value of sum to be 1 which is not the correct answer.
So, it would be better if you declare sum as float so that there won't be any truncation of data.
Also, you need to replace your line 19 to be like this.
sum = (float)a / b;
One more thing, your variable names must be logically sounding correct...in your case, sum does nothing even closely related to addition, which it actually means!!
A small suggestion would be to replace the name sum by quo, as it is the quotient you are getting.
regards,
Raghav
#6
Re: My first (sorta) program in C++
Posted 21 January 2013 - 10:59 PM
Nice job on your first program. I'm not sure if you are aware, but here is a defined floating point variable (single and double precision).
#7
Re: My first (sorta) program in C++
Posted 27 January 2013 - 09:08 PM
Try this:
Nice job though.
I think that his intent was to truncate the data, and then use the "%" operator to find the remainder. Using 3 and 2 in this program would give you 1 remainder 1, which is correct.
Nice job though.
#include <iostream>
using namespace std;
int main()
{
int a, b;
int answer, remainder;
cout << "Division: type in a number \n";
cin >> a;
cout << "Type in another number \n";
cin >> b;
remainder = a % b;
answer = a / b;
cout << "the answer is " << answer << " and the remainder is " << remainder << endl;
return 0;
}
raghav.naganathan, on 21 January 2013 - 10:58 PM, said:
Well, consider your a to be 3 and b to be 2
on execution of line 19, you will get the value of sum to be 1 which is not the correct answer.
So, it would be better if you declare sum as float so that there won't be any truncation of data.
on execution of line 19, you will get the value of sum to be 1 which is not the correct answer.
So, it would be better if you declare sum as float so that there won't be any truncation of data.
I think that his intent was to truncate the data, and then use the "%" operator to find the remainder. Using 3 and 2 in this program would give you 1 remainder 1, which is correct.
#8
Re: My first (sorta) program in C++
Posted 28 January 2013 - 06:49 AM
raghav.naganathan, on 21 January 2013 - 11:58 PM, said:
Well, consider your a to be 3 and b to be 2
on execution of line 19, you will get the value of sum to be 1 which is not the correct answer.
So, it would be better if you declare sum as float so that there won't be any truncation of data.
Also, you need to replace your line 19 to be like this.
One more thing, your variable names must be logically sounding correct...in your case, sum does nothing even closely related to addition, which it actually means!!
A small suggestion would be to replace the name sum by quo, as it is the quotient you are getting.
regards,
Raghav
on execution of line 19, you will get the value of sum to be 1 which is not the correct answer.
So, it would be better if you declare sum as float so that there won't be any truncation of data.
Also, you need to replace your line 19 to be like this.
sum = (float)a / b;
One more thing, your variable names must be logically sounding correct...in your case, sum does nothing even closely related to addition, which it actually means!!
A small suggestion would be to replace the name sum by quo, as it is the quotient you are getting.
regards,
Raghav
First forcing floating point in the calculation is unwarranted. As already stated by others the purpose here is to find the integral part of the equation, thus the idea is to truncate the data.
Quote
One more thing, your variable names must be logically sounding correct
This is also incorrect, variables can be named almost anything. I do agree however that variable names should be meaningful in the content of the code, but this is not an absolute requirement. I also don't usually recommend using abbreviations, use the actual word instead.
Jim
#9
Re: My first (sorta) program in C++
Posted 28 January 2013 - 08:31 PM
Thank you for that information sir, I guess I misread the question and the 'sum' part threw me off as it was a bit confusing.
As for the variable names, what I meant was...it is good programming practice to use the names of variables as logically sounding names, so that when you look at the code after a long time, you still can understand it without any difficulty...
...also, if any modifications are to be made and the original programmer who wrote the code is not available, it would be helpful for any other programmer to understand the code better if logically sounding variable names are used 
regards,
Raghav
As for the variable names, what I meant was...it is good programming practice to use the names of variables as logically sounding names, so that when you look at the code after a long time, you still can understand it without any difficulty...
regards,
Raghav
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote







|