| | |
Please help i am new in c++ programming
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 14
Reputation:
Solved Threads: 0
Write a program which will calculate, for every integer that is being input, a code in the following way
first digit multiplied by 3
plus
second digit multiplied with the third digit
minus
fourth digit
thus the number 3124(of type int) should generatethe code 7, because 3*3 +1*2 - 4=7.
you may assume that the given integers will all consist of four digits. submit printouts of the program and output with he six integers below as input. use a for loop iterating from 1 to 6. NB: You may not input the digits separately, but should input the number as a whole. (hint:Use / and % in the calculation.)
3255
1067
1393
2892
1111
2020
i tried this program below but it gives me (-2answer)
please help me
first digit multiplied by 3
plus
second digit multiplied with the third digit
minus
fourth digit
thus the number 3124(of type int) should generatethe code 7, because 3*3 +1*2 - 4=7.
you may assume that the given integers will all consist of four digits. submit printouts of the program and output with he six integers below as input. use a for loop iterating from 1 to 6. NB: You may not input the digits separately, but should input the number as a whole. (hint:Use / and % in the calculation.)
3255
1067
1393
2892
1111
2020
i tried this program below but it gives me (-2answer)
please help me
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main( ) //Initialize integers { int number,first,second,third,fourth,answer; //For loop for(int x=1;x <= 6;x++) //Input of number from user cout << "Enter in number: "; cin >> number,first,second,third,fourth; //Get the first digit in the thousands place first = number / 1000; number = number % 1000; //Get the second digit in the hundreds place second = number / 100; number = number % 100; //Get the third digit in the tens place third = number / 10; number = number % 10; //Remaining is the ones place fourth = number; //Calculate and give answer answer = first *3+second *third -fourth ; cout << "Your aswer is" << answer; cin >> answer; return 0; }
Last edited by Ancient Dragon; Apr 27th, 2008 at 6:02 pm. Reason: add code tags
Its not
but
and kindly use code tags in future
C++ Syntax (Toggle Plain Text)
cin >> number,first,second,third,fourth;
but
C++ Syntax (Toggle Plain Text)
cin >> number>>first>>second>>third>>fourth;
and kindly use code tags in future
There are 10 types of people in the world, those who understand binary and those who don't.
All generalizations are wrong. Even this one.
All generalizations are wrong. Even this one.
•
•
Join Date: Oct 2007
Posts: 280
Reputation:
Solved Threads: 19
it looks like you are prompting for 6digits and you specified that the requirement is to get a whole number...then change your input to get the number as a whole
your for loop doesnt look controlled, why are you prompting the user for "answer"??
C++ Syntax (Toggle Plain Text)
cin>>number;
your for loop doesnt look controlled, why are you prompting the user for "answer"??
Last edited by joshmo; Apr 27th, 2008 at 2:42 pm.
You might want to repost your program (using code tags) to see if it really was fixed.
Also, you may want to change something like
to
floor() is declared in math.h (so add #include<cmath> or #include<math.h> to the top of your source). i think it would be better than simply relying on the integer round down, but it won't really change your program's results.
Also, you may want to change something like
C++ Syntax (Toggle Plain Text)
first = number/1000;
C++ Syntax (Toggle Plain Text)
first = floor(number/1000);
floor() is declared in math.h (so add #include<cmath> or #include<math.h> to the top of your source). i think it would be better than simply relying on the integer round down, but it won't really change your program's results.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Secondly . LOETO
I think you should remove
Or
And just use the
As YOU only require to take in one number each time and get the first,second,third and fourth out from the main number.
I think you should remove
C++ Syntax (Toggle Plain Text)
cin >> number,first,second,third,fourth;
C++ Syntax (Toggle Plain Text)
cin >> number>>first>>second>>third>>fourth;
And just use the
C++ Syntax (Toggle Plain Text)
cin>>number;
As YOU only require to take in one number each time and get the first,second,third and fourth out from the main number.
Last edited by Sky Diploma; Apr 27th, 2008 at 4:08 pm. Reason: Sorry For Posting Wrong Info.
As for the floor() thing, I think you are getting floor() confused with a rounding function. floor() always rounds down, so floor(3.812) would be 3, not 4. conversely, ceil() always rounds up. so ceil(3.224) would be 4, not 3.
if you used abs() to truncate the non-integral part of the number, you would just be doing the same thing that he's currently doing, only doing indirectly through the abs() function, and not on your own.
if you used abs() to truncate the non-integral part of the number, you would just be doing the same thing that he's currently doing, only doing indirectly through the abs() function, and not on your own.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Well i am sorry .. MAde a bad comment on that one. I respond with DEEP apologies.
And edit my post as well
And edit my post as well
![]() |
Similar Threads
- Good books on Shell Programming/Perl (Perl)
- Shell Programming (Shell Scripting)
- Programming (C++)
- Professional web design and programming services (Post your Resume)
- Where to get started with Web Programming (IT Professionals' Lounge)
- Programming in Linux? (*nix Software)
- Efficient Programming (Computer Science)
- New To Programming = ME!! (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ 2D vector help please.
- Next Thread: New to C++
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





