This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
I'm newbie in c++ and i am doing some exercises.
I have a code
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void) {
int i=0;
int a,b,c;
a = i++;
b = i++;
c = i++;
cout << a << b << c;
return 0;
}
and, whe I run it, the result is: 012
But when I run it without variables a, b and c,
like in following code
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void) {
int i=0;
cout << i++ << i++ << i++;
return 0;
}
I get result in reverse order: 210
Why is this happening? I think it should be again 012 (I am using NetBeans)
You are violating the rules of the language that has multiple increment and decrement in the same expression or as different arguments to the same call as undefined.
Related
I am writing a program to resolve the request:
Count the number of match sticks used to create numbers in each test case
Although it is a simple problem, the thing that makes me quite confusing is that the program has no error but the output is not as expected.
Source Code:
#include <bits/stdc++.h>
using namespace std;
int main() {
map<char,int> digits={
{'0',6},{'1',2},{'2',5},{'3',5},{'4',4},{'5',5},{'6',6},{'7',3},{'8',7},{'9',6}
};
map<char,int> peterMap;
int t; cin >> t;
string peterNum[t];
for(string &a:peterNum) cin >> a;
for(string b:peterNum){
int sticks = 0;
string tomNum, n;
for(char c:b) ++peterMap[c];
for(auto d:peterMap) sticks += d.second*digits[d.first];
cout << sticks << ' ';
}
return 0;
}
Input:
5 (Number of test cases)
1 0 5 10 15
Output:
2 8 13 21 28
Expected Output:
2 6 5 8 7
There are 3 problems with your code
don't use <bits/stdc++.h>, it is non-standard and promotes bad practice.
variable-length arrays are not standard C++, use std::vector instead. But this is actually not necessary in this case, because...
peterMap is completely unnecessary and needs to be removed, it is screwing up your result.
Try this instead:
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
map<char,int> digits = {
{'0',6},{'1',2},{'2',5},{'3',5},{'4',4},{'5',5},{'6',6},{'7',3},{'8',7},{'9',6}
};
int t; cin >> t;
for (int i = 0; i < t; ++i) {
string a; cin >> a;
int sticks = 0;
for(char ch : a) sticks += digits[ch];
cout << sticks << ' ';
}
return 0;
}
Online Demo
Problem is here:
for(char c:b) ++peterMap[c]; // <<--here
for(auto d:peterMap) sticks += d.second*digits[d.first];
You are increasing number in map and use it in next statement without reseting for next input entry.
But there are several problems with your code:
Don't use #include <bits/stdc++.h>. I hate hackerrank for using this in their solution template.
Using string peterNum[t]; is not standard as mentioned in comments.
From my point of view, you don't need to use std::map for peterMap at least. Just iterate over characters of each string.
This question already has answers here:
Convert char to int in C and C++
(14 answers)
Closed 1 year ago.
could someone try explain the what the difference between these two pieces of codes is?
// Example program
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
char keypad[10][5]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
int idx = 2;
string digits = "1324";
int curidx=digits[idx] - '0';
cout << curidx << endl;
}
In this case, with the inclusion on line 12, the output is 2.
// Example program
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
char keypad[10][5]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
int idx = 2;
string digits = "1324";
int curidx=digits[idx];// - '0';
cout << curidx << endl;
}
In this result, the output result is 50. What does the inclusion of - '0' do?
In C and Cpp, everything is inherently treated as a "number". Even char manipulations should be treated as number-operations...that makes it easier to logic-out the requirements.
Every char is indeed, an integer, equivalent to its ASCII value.
Hence, '2' = 50. Also, 'A' = 65 and 'a' = 97 and so on...
So, your operation '2' - '0' actually does 50-48, which results in 2
When you do not subtract and print '2' as integer, it prints its ascii value, which is 50. If you would print it as a char or string, it would have printed 2.
This question already has answers here:
Fixed Decimal Precision [duplicate]
(4 answers)
Closed 2 years ago.
I am trying a problem. I need to print the ans in 6 decimal digits. For example if the ans is 64, I want to print 64.000000
I tried the following way.
what I did wrong?
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin>>t;
float n;
while(t--)
{
cin>>n;
float s=(n-2)*180.000000;
float w=(s/n)*1.000000;
cout<<w*1.000000<<setprecision(6)<<endl;
}
return 0;
}
You can make use of std::fixed:
#include <iostream>
#include <iomanip>
void MyPrint(int i)
{
double d = static_cast<double>(i);
std::cout << std::setprecision(6) << std::fixed << d << std::endl;
}
int main() {
MyPrint(64);
MyPrint(100);
return 0;
}
Running the above code online results in the following output:
64.000000
100.000000
This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Why pow(10,5) = 9,999 in C++
(8 answers)
Closed 3 years ago.
I have this code and the function pow() is behaving differently in two cases.
For "p" I'm using the number 3, in the first output is showing 124 but the second shows 125.
#include <iostream>
#include <math.h>
using namespace std;
int main(){
long long int p, ans;
cin >> p; //3
ans = pow(5,p);
cout << ans << endl; //124
cout << pow(5,p) << endl; //125
return 0;
}
This question already has answers here:
Convert char to int in C and C++
(14 answers)
Closed 9 years ago.
My code is this:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
int a;
cout << "Enter a number" << endl;
cin >> a;
string a_str;
ostringstream temp;
temp << a;
a_str = temp.str();
for(int i = 0; i < a_str.length(); i++) {
char c = a_str[i]; //How this character convert to integer number
}
system("pause");
return EXIT_SUCCESS;
}
How char c convert to int ?
I need this for
I need this because I need to get the highest digit
If you want to get char '8' to int 8 for example, this would be enough for ascii
int i = a_str[0] - '0';
const int i = atoi(&c);
should work too
char is already an integral type. I think you meant the following
char c = a_str[i] - '0';
atoi(c) for integers
atof(c) for floats
do the trick
You can also write it yourself:
This works for normal keys (not arrows keys or...)
#include <iostream>
using namespace std;
int main () {
char c;
cin >> c;
cout << (int)c << endl;
}
if you just have digits, you can do this:
int num = c - '0';