The problem is as follows:
Input is the "t" number of data sets, followed by t 11-digit numbers. There's a cipher included in the code as an array. The code is supposed to multiply the subsequent digits from the input number by the corresponding digits from the cipher, thus creating a sum of 11 multiplications. After that the code checks whether the sum is divisible by 10. If so, it returns "correct", if not - "incorrect".
I've written a code which works as intended, but I'd like to simplify this code, specifically to include the modulus operator instead of fmod to extract the digits from the 11-digit input numbers. I've tried using modulus, but it can only be utilized for an int.
I found a code for a simple reversed order digit extractor (using the while loop and %10), but I'm having some trouble implementing it in my code... Any help would be appreciated.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
long long int number;
int cipher [11] = {1,3,7,9,1,3,7,9,1,3,1};
int t, digit, sum;
cin >> t;
for (int i=0; i<t; i++)
{
sum = 0;
cin >> number;
for (int j=10; j>=0; j--)
{
digit = fmod(number/(pow(10,(10-j))),10);
sum = sum + digit*cipher[j];
}
if (sum%10==0)
cout << "Correct" << endl;
else
cout << "Incorrect" << endl;
}
return 0;
}
An example of a correct number is 44051401458. We're assuming all the input numbers are always 11 digits.
I think this is what you are referring to. You can just keep dividing number by 10, which will shift the decimal point. It will not turn into a float since you defined number as an int (long long int), so any decimal point just gets erased and you can use modulus freely.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
long long int number;
int cipher [11] = {1,3,7,9,1,3,7,9,1,3,1};
int t, digit, sum;
cin >> t;
for (int i=0; i<t; i++)
{
sum = 0;
cin >> number;
for (int j=10; j>=0; j--)
{
digit = number%10;
number = number/10;
sum = sum + digit*cipher[j];
}
if (sum%10==0)
cout << "Correct" << endl;
else
cout << "Incorrect" << endl;
}
return 0;
}
Related
So, what needs to be done is: enter a real number and print the sum of its first 4 digits after the decimal point. E.g.: I enter 5.1010. I get to the point where I need to multiply 0.1010 by 10000 so it can become an integer, but the result I'm getting is 1009 instead of 1010 and everything falls apart after that.
I'd be forever thankful if someone can explain to me why does that happen.
#include<iostream>
using namespace std;
int main()
{
double n;
cout<<"Enter a positive real number: ";
do
{
cin>>n;
if(n<=0) cout<<"The number must be positive, enter again: ";
}while(n<=0);
//storing the fractional part in a var
int y=n;
double fr=n-y;
//turning the fractional part into an integer
int fr_int=fr*10000;
cout<<fr_int<<endl;
//storing each of the digits in a var
int a=fr_int/1000;
int b=fr_int/100%10;
int c=fr_int/10%10;
int d=fr_int%10;
cout<<"The sum of the first 4 digits is: " << a+b+c+d;
return 0;
}
You could simply change the code as follows, then it should be working.
n *= 10000;
int Integer = n;
int i = 4;
int sum = 0;
while(i--)
{
sum += (Integer%10);
Integer /= 10;
}
std::cout << "The sum of the first 4 digits is: " << sum;
Here is the output: https://www.ideone.com/PevZgn
Update:A generalized soln would be using std::string. However, would be great if the code is capable of handling exceptions in the case of non-numeric has been submitted by the user.
#include <iostream>
#include <string>
int main()
{
std::string Number;
double tempNum = 0.0;
std::cout << "Enter a positive real number: ";
do
{
std::cin >> Number;
tempNum = std::stof(Number);
if(tempNum <= 0)
std::cout << "The number must be positive, enter again: ";
}while(tempNum <= 0);
bool Okay = false;
int sum = 0;
int i = 4;
for(const auto& it: Number)
{
if(Okay && i > 0)
{
sum += static_cast<int>(it - '0');
--i;
}
if(it == '.') Okay = true;
}
std::cout << "The sum of the first 4 digits is: " << sum;
return 0;
}
I think you should add 0.5 before casting because the compile will always truncate the number.
In C++11 you can use std::round.
Floating points and doubles in C++ aren't able to represent all decimal numbers accurately. In particular, 0.1, it cannot represent faithfully.
If you must be guaranteed that you get accurate results, you should either use fixed point math or a bignumber library.
I have a number, which I have to write backward. I already separated it's digits in an array, but how can I make the new number from this array?
You don't need to store digits in an array to reverse digits of number.
#include <iostream>
using namespace std;
int main()
{
int input;
cin>>input;
int reverse = 0;
while(input)
{
reverse *= 10;
reverse += input % 10;
input /= 10;
}
cout << reverse << endl;
return 0;
}
I wrote a simple program which is:
#include<iostream>
#include<cmath>
int main()
{
int t, n;
int count = 0;
std::cin>>t;
for(int i = 0; i < t; i++)
{
std::cin>>n;
int num = n;
while(num > 0)
{
num = num/2;
count++;
}
}
std::cout<<"\n count "<<count<<std::endl;
std::cout<<pow(2, count-1)<<std::endl;
return 0;
}
Here I want my program to work for input like
t = 1
n = 1000000000000000 (10^15)
Now when I use this input then the input buffer of program never ends(i.e to close the program we have to press ctrl + c). I guess that there is problem in the power function. So I want to know how to avoid such condition in c++.
I want to know if it is possible to calculate values of 2^32 in C++ ? either by pow() or through any other manual method.
Problem
You are using data types that do not have sufficient space to store a value that is greater than 2^32.
Solution
Use a data type that can store values that are larger than 2^32. Try using long long instead of int. How large these data types are will depend on your compiler.
#include<iostream>
#include<cmath>
int main()
{
long long t, n;
long long count = 0;
std::cin>>t;
for(long long i = 0; i < t; i++)
{
std::cin>>n;
long long num = n;
while(num > 0)
{
num = num/2;
count++;
}
}
std::cout<<"\n count "<<count<<std::endl;
std::cout<<pow(2, count-1)<<std::endl;
return 0;
}
If you do not require negative values then you can increase the maximum value a long long or int can hold by declaring them unsigned.
It works with pow and powl. There are values greater than 2^32, but you need to know that here we use mantissa:
#include <iostream>
#include <iomanip>
#include <cmath>
int main() {
using namespace std;
cout << sizeof(int) << endl;
int n = 1024 - 1;
cout << fixed << setprecision(0) << pow(2,n) << endl;
cout << sizeof(pow(2,n)) << endl;
n = 1024 * 16 - 1;
cout << fixed << setprecision(0) << powl(2,n) << endl;
cout << sizeof(powl(2,n)) << endl;
return 0;
}
Output:
4
89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164812112068608
8
594865747678615882542879663314003565381722343548255118736337410616630679090241843452244297736306019957557718742419654629448833690654343713137262349170782503040435817183002448761071625809765723422976172854741067923518323732415492392357140483922807069238022169202443061452643427656618079347999942895053178509060407681660390482161856378582145306703437601208682661975133940044533758686135305417823772877890396715811106725951908929815345155671925328769680324822596641589145883829482702642556778067184896640862944007954207337644916269031709617444299949490311557012560837236025936219660661599201471352670683475637369507296908449144497222586700182308964188569037205672895924286797538585218822095871944822442688842369161120304119539530699737837667369892008245871310742614507423836167988948579198667113174867405720826538879125494463015447394802338076552128630070903411513794001720975727663850799035640794798584706982804219752491585627531141013313100024021074904100001030496716840618811928940313739863536438741419219352524017082316668506692702999020350954331193650802509094131286861883139620399465858854403950870132703965488209824438934802008758845969343994044004472125629413484844182097066972890078922182473026356827727453163593714265947550139347559661748404351815218096963796346172410406417148682239343431032084521229277568266027525254094945933423431899958823773645685786750350507598779548726520016515760341759108247097818348038874055299142450671805734607137060905247538989637778322582491925031025533258542323684732018320284669732418586091676478436956021320001805809394639097855026047281380653351775920165055322550997717583813344334813881910302171240178953207677106366473378036503453544435248062525034078329626380648832032749173746330899412031156105204637292282793632423208825080061587937017363130978644540733098825776915372212354849317376813885178113563072526274562614724020074557397840679937984256404287622135935727727042447493077510397403490469607829027659582820840552983227079975738454291564860751649408292571036530740444010884909169208564698439185729787923026291571464223624851849274062647887960468225011325713624974790354101983041423775460945576066660524005986941818288912766662994426078162719667510657656067040695225510627681853951747958481562962100583938595054467627957269744108448558971634686804319537236396375558357563553198212540676776568606776445269901301489322659897550488216469545962330114439456450327105059143649149353691079858592284770257701514586653646227195894784337109820380725586800308876093495956683418516943600791035812934123566552256657548637356721364170303321445203248318052221608876405613735014581429046863850524823249770110491990966393306602127113232121844805053714961598819340772918780886767784492268026813617212138552880462432011890814832763157455453480244036737608502560568155935219962881254333016283106875208347859959837111605303362360686735617010806770356094119954850985971972173740157108951943158883889960769946088667172184453775159400416773426172185163544642073750820294724241000627118693340037228670955466887445979840508258034553074952786212905447793469416533745102450184312083150984276502843520142547725242420036764321913285201883578643256190127554977259428506738294094650002069424857941569933035773787408238363817558217731402200556355696264590285397096711343409176606399534486123848595737134078956097986897096403649443476180550440132129400660464020005964076985400565370669775001649507962489129968487179363143071990260056227184635557041873959503901703298160676708502034434721702736070337981820498702504612901752836363232547753133669634446212182280948830953449212093385245517672040199624163548955856440570085192091029300807379142100375091750164679249845932033295269830354534768690800943839523328879827294000968558885672349163214396311447169008056222766769723543731024881704573771049624407760697964694003855586008947448896853302136740492580514407729393955580489556711216778774585452721013198637847641603652665922709995374673905262003097098600295826073933596848127168932490801916573177100850314408973588759057608837176008255586173863863537610028088874109464298579173372270668553679213878959830281291941911631089480845893613059432816382467144386202929877438879934617765326964968950596805834503736177373180382300936221015689972069912183414349395106461498087096364312945860028806254674550241272982076023238962557223250366082054549672629899727845047788394343198743530974427374512431803960928917102896898594417389828136739556194292853212418189536177705143393509263700826967109944180530974835980527534343480734009517814874712043293597520502202457633238136380535255784193531700632068258618605704958229398173812474607952266968605468760232899150087704008769431156359521180518564669448293014075023298039436222182782240272844516787977851494198359872264106492071289241977002542132163865420492710010704534742706160402634260047073399438055207291585195236991244449614045909106967144147839858684971576230223513645334982033408
16
If you need accuracy for big numbers when the better way is to use BigInteger from non-standard libraries because C++ Standard Library doesn't support it.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main(int argc, char **argv)
{
string c;
int k = 0, decval, i;
cout << "Please input your number starting from lowest value number to highest" << endl;
cin >> c;
//the for loop takes a backwards integer and makes it forwards.
for(i = 0; i < c.length(); i++){
decval += (c[i] - '0') * pow(10, k);
++k;
}
cout << decval;
return 0;
}
so my problem is when I input something like 564(wanting to get 465 in return) I get 462. I haven't been able to spot the logic error in the code. Note that I am both new to coding and stack overflow so please don't be too harsh. Any help would be greatly appreciated.
You forgot to initialize decval to 0. It probably contains an arbitrary value which messes up your result.
This code:
(c[i] - '0') * pow(10, k);
Converts an integral type to floating point, performs floating-point math, then converts back to an integral type. (See this question)
You absolutely risk rounding errors along the lines of 59.99999 getting rounded down to 59.
Adjusting your logic to only use integer math will fix it.
int multiplier = 1;
for(i = 0; i < c.length(); i++, multiplier *= 10){
decval += (c[i] - '0') * multiplier;
++k;
}
This is also a solution and is pretty simple I think:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(int argc, char **argv)
{
string str;
cout << "Please input your number starting from lowest value number to highest" << endl;
cin >> str;
reverse(str.begin(), str.end());
int number = stoi(str);
cout << number << endl;
return 0;
}
Both Drew and nicebyte are correct in what they have pointed out. Just wanted to add that you can do this without k, an extra multiplier variable, calling pow(), or rounding issues:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main(int argc, char **argv)
{
string c;
int decval = 0, i;
cout << "Please input your number starting from lowest value number to highest" << endl;
cin >> c;
//the for loop takes a backwards integer and makes it forwards.
for( i=c.size()-1 ; 0<=i ; --i ) decval = c[i] - '0' + 10*decval;
cout << decval << endl;
return 0;
}
Explanation of for loop:
Take the number 4321 for example. Start at the end of the string, and work backwards. I.e.
After first loop, decval = 1.
After second loop decval = 12.
After third loop, decval = 123.
After fourth loop, decval = 1234.
Each time you are multiplying decval by 10, and adding the new digit. By doing it this way, you don't have to multiply by 10 the first time, 100 the second time, 1,000 the third time, etc.
I designed this program that can print the Fibonacci Series (series[i] = series[i-1] + series[i-2]) but i can't get more than 47 numbers because the 48th they become negative and strange numbers (i think this happens when the list is out of range or the item is null):
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
int length;
string again = "";
do {
cout << "Enter the length you want in your sequence: ";
cin >> length;
vector<int> series(length);
for (int n=0; n<=1; n++) series[n] = n;
for (int number=2; number<=length; number++) {
series[number] = series[number-1] + series[number-2];
}
for (int i=0; i<length; i++) cout << series[i] << " ";
cout << endl << "Do it again ? <y/n> ";
cin >> again;
cout << endl;
} while (again == "y");
}
EDIT:
"Improved" code:
#include <iostream>
#include <vector>
#include <string>
std::vector<int> fibonacci (int length)
{
std::vector<int> series(length);
series[0] = 0;
series[1] = 1;
for (int num=2; num<length; num++) {
series[num] = series[num-1] + series[num-2];
}
return series;
}
int main ()
{
std::string again;
do {
std::cout << "Enter how many numbers you want in your series: ";
int length;
std::cin >> length;
std::vector<int> series(length);
series = fibonacci(length);
for (int n=0; n<length; n++) std::cout << series[n] << " ";
std::cout << "\nDo it again <y/n> ? ";
std::cin >> again;
std::cout << std::endl;
} while (again == "y");
}
When you get to the 47th value, the numbers go out of int range. The maximum int value is 2,147,483,647 and the 46th number is just below at 1,836,311,903. The 47th number exceeds the maximum with 2,971,215,073.
Also, as LeonardBlunderbuss mentioned, you are exceeding the range of the vector with the for loop that you have. Vectors start with 0, and so by having number<=length; the range+1 element will be called. The range only goes up to length-1.
You are encountering integer overflow, meaning that you are trying to calculate a number that is outsize of the bounds of INT_MAX and INT_MIN. In the case of an unsigned number, it just overflows to zero and starts over, while in the case of a signed integer, it rolls over to INT_MIN. In both cases this is referred to as integer overflow or integer wraparound.
You could put a band-aid on the solution by using long long int (likely 64-bits on most modern systems) instead of int for your primitive data type, or you could use a better approach like a library that supports (almost) arbitrarily long data types, like libBigInteger.
References
Integer Overflow, Accessed 2014-03-04, <http://en.wikipedia.org/wiki/Integer_overflow>
C++ Big Integer Library, Accessed 2014-03-04, <https://mattmccutchen.net/bigint/>
The limits.h Header File, Accessed 2014-03-04, <http://tigcc.ticalc.org/doc/limits.html>
This is my solution to calculating BIG fibonacci numbers
// Study for algorithm that counts n:th fibonacci number
#include <iostream>
#include <cstdlib>
#include "boost/multiprecision/cpp_int.hpp"
#define get_buffer(a) buffer[(a)%2]
#define BIG boost::multiprecision::cpp_int
int main(int argc, const char* argv[])
{
// atoi returns 0 if not integer
if(argc != 2 || atoi(argv[1]) < 1){
std::cout << "You must provide one argument. Integer > 0" << std::endl;
return EXIT_SUCCESS;
}
// ring buffer to store previous two fibonacci number, index it with [i%2]
// use defined function get_buffer(i), it will do the magic for you
BIG buffer[2]={ 1, 1 };
// n:th Fibonacci
unsigned int fn = atoi(argv[1]);
// count loop is used if seeked fibonacci number is gt 2
if(fn > 2){
for(unsigned int i = 2; i < fn; ++i){
get_buffer(i) = get_buffer(i-1) + get_buffer(i-2);
// get_buffer(i-1) + get_buffer(i-2) == buffer[0] + buffer[1]
// if you want to print out every result, do it here
}
}
// Result will be send to cout
std::cout << "Fibonacci[" << fn << "] is " << get_buffer(fn-1) << std::endl;
return EXIT_SUCCESS;
}