The function that I am having a problem with is below, Visual studio is saying that "expression must be a modifiable value". Is there something that I am doing wrong, or is it a problem with the compiler? Thank you for any help/suggestions.
int primeCheck(int number){
int count = 0;
for (int i = 2; i < number; i++){
if (number % i = 0){//the error is coming up with the number variable here
count++;
};
};
if (count > 0){
return 0;
}
else{
return 1;
};
}
You are trying to assign 0 to number % i.. Use '==' not =
Related
When I am passing this Input I am getting wrong answer
coin[] = {5,6}
Amount(W) = 10
my answer = 1
Correct Answer should be 2 i.e {5,5}
void coin_make(int W, vector<int> coin){
int n = coin.size();
int dp[n+1][W+1];
for(int i = 0; i <=W; i++){
dp[0][i] = INT_MAX;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= W; j++){
if(coin[i-1] == j){
dp[i][j] = 1;
}
else if(coin[i-1] > j){
dp[i][j] = dp[i-1][j];
}
else {
dp[i][j] = min(dp[i-1][j],
1 + dp[i][j-coin[i-1]]);
}
}
}
cout<<dp[n][W];}
You're overflowing on dp[1][6], since you try to calculate 1 + INT_MAX. This error propagates further and finally the answer is not correct. When I ran it on my machine, I got -2147483648. You should use some other constant as "infinity" to prevent overflows (e.g. 2e9 (or -1, but this would require some additional if statements)). Then the code will work fine on your provided test case.
Currently i am doing a challenge in codewars on Balanced numbers and i wrote the code in dart and it successfully completed 100 test cases but for long numbers it is not working properly...So i think that it need some conditions for this long numbers:
String balancedNum(numb) {
// your code here
var numb1 = numb.toString();
List a = numb1.split("");
var left_sum = 0;
var right_sum = 0;
for (var i = 0; i <= a.length / 2; i++) {
left_sum += int.parse(a[i]);
}
List<String> b = a.reversed.toList();
//print(b);
for (var i = 0; i < b.length / 2; i++) {
right_sum += int.parse(b[i]);
}
//print(right_sum);
if (left_sum == right_sum) {
return 'Balanced';
} else {
return 'Not Balanced';
}
}
link to the challenge:https://www.codewars.com/kata/balanced-number-special-numbers-series-number-1/train/dart
The compiler verdict for the code is wrong answer because of a simple logical error.
This is because the length of the list has not been calculated correctly
Kindly have a look at the corrected code below, which passes all the 110 test on the platform:
String balancedNum(numb) {
// your code here
var numb1 = numb.toString();
List a = numb1.split("");
var left_sum = 0;
var right_sum = 0;
double d = ((a.length-1) / 2);
int len = d.floor();
print(len);
for (var i = 0; i < len; i++) {
left_sum += int.parse(a[i]);
}
List<String> b = a.reversed.toList();
print(b);
for (var i = 0; i < len; i++) {
right_sum += int.parse(b[i]);
}
print(left_sum);
print(right_sum);
if (left_sum == right_sum) {
return 'Balanced';
} else {
return 'Not Balanced';
}
}
Your problem is not about "long numbers" since you code also fails for the test case using the number 13.
You properly did not read the following rules:
If the number has an odd number of digits then there is only one middle digit, e.g. 92645 has middle digit 6; otherwise, there are two middle digits , e.g. 1301 has middle digits 3 and 0
The middle digit(s) should not be considered when determining whether a number is balanced or not, e.g 413023 is a balanced number because the left sum and right sum are both 5.
So you need to test if the number are even or uneven since we need to use that information to know how many digits we should use in the sum on each side.
I have done that in the following implementation:
String balancedNum(int numb) {
final numb1 = numb.toString();
final a = numb1.split("");
var split = (a.length / 2).floor();
if (a.length % 2 == 0) {
split--; // the number is even
}
var left_sum = 0;
var right_sum = 0;
for (var i = 0; i < split; i++) {
print(a[i]);
left_sum += int.parse(a[i]);
}
final b = a.reversed.toList();
for (var i = 0; i < split; i++) {
right_sum += int.parse(b[i]);
}
if (left_sum == right_sum) {
return 'Balanced';
} else {
return 'Not Balanced';
}
}
So, this code has weird outputs when running on Command-Line with different outputs, I also get a segfault (core dumped) with some cases. I suspect it's to do with the min, max, mid bounds I have set. Please help me out as to what might be going wrong.
the code is searching based off two vectors of type class (Book) where all three elements ISBN,course, and type need to match for the counter to increment. We are searching for number of r in n.
int binary_search(std::vector<Book> n, std::vector<Book> r){
std::sort(n.begin(),n.end());
unsigned int mid;
int count = 0 ;
for (unsigned int i = 0; i < r.size(); i++) {
unsigned int min = 0 ;
unsigned int max = n.size() - 1;
while(max >= min) {
mid = (max + min) / (2);
if((n[mid].isbn == r[i].isbn) && (n[mid].course == r[i].course) && (n[mid].type == r[i].type)) {
count++;
break;
} else if(n[mid].isbn < r[i].isbn){
min = mid + 1;
} else{
max = mid - 1;
}
}
}
return count;
}
So this was a problem statement from CodeLeet to find the Longest Palindromic Substring.
In the codeleet interface this solution works:
class Solution {
public:
string longestPalindrome(string s) {
int len = s.size();
int P[len][len];
memset(P, 0, len*len*sizeof(int));
int maxL=0, start=0, end=0;
for(int i =0; i< s.size(); i++){
for(int j =0; j<i; j++){
P[j][i] = (s[j] == s[i] && (i-j<2 || P[j+1][i-1]));
if(P[j][i] && maxL < (i-j+1))
{
maxL = i-j+1;
start = j;
end = i;
}
}
P[i][i] =1;
}
return s.substr(start, end-start +1);
}
};
But when the write the same thing as a function in Visual Studio:
string User::longestPalindromeStr(string s) {
int len = s.size();
int P[len][len];
memset(P, 0, len*len * sizeof(int));
int maxL = 0, start = 0, end = 0;
for (int i = 0; i< s.size(); i++)
{
for (int j = 0; j<i; j++)
{
P[j][i] = (s[j] == s[i] && (i - j<2 || P[j + 1][i - 1]));
if (P[j][i] && maxL < (i - j + 1))
{
maxL = i - j + 1;
start = j;
end = i;
}
}
P[i][i] = 1;
}
return s.substr(start, end - start + 1);
}
it says for the len variable : expression must have a constant value? Is it some problem with the VIsual studio ide. How can I solve this ?
Because variable length arrays (array declarations that use non-constexpr variables) are a C99 feature, not a C++ feature. No version of C++ offers them, and no version of Visual Studio provides that particular C99 feature.
If your code compiles on other C++ compilers, then that is because of C++ language extensions that they provide on top of the C++ standard.
There's no problem with the VS compiler. In C++, you can use new int[] to create a 1-dimensional array of runtime defined length, but new int[][] is not available unless the 2nd dimension is a constant expression.
The following attempt gives a compiler error that the 2nd dimension has to be a constant expression:
int len = 10;
int **P = new int[len][len]; // error: non-constant expression as array bound (on 2nd dimension)
This link gives a nice workaround Copy 2D array using memcpy?.
This is what I have got but it doesn't work. When I try to compile I get this error message:
int result = 0;
^^^^^^^^^^^^^^^
Unreachable code
My code:
public int sumOfOddIntegers (int n) {
if(n < 1);
return 0;
int result = 0;
for(int i = n - 1; i > 0; i--)
{
if(i % 2 != 0) {
result = result + i;
}
}
return result;
}
if(n < 1);
return 0;
is equivalent to :
if(n < 1) {
}
return 0;
It shoud be replaced by :
if(n < 1)
return 0;
or (the right way)
if(n < 1) {
return 0;
}
The statement:
if(n < 1);
Is a no op because of the semi-colon. The comparison is evaluated, and nothing is done, whatever the result of the comparison is.
Then, the next line is executed, which returns 0.
if(n < 1); is your problem. The rest of the code is unreachable beacuse the following return' is always exectued.
Remove the ; after if(n < 1).
As others have said, the semi colon on your if statement is the problem. Personally however I would just do it like this:
public int sumOfOddIntegers (int n)
{
int result = 0;
if(n < 1)
return result;
for(int i = 1; i <= n; i += 2)
{
result += i;
}
return result;
}
This way you can halve the number of iterations. We know every other number is odd, so why even bother iterating the even ones and checking if they're odd when we know they're not?
the sequence is a arithmetic progression with common difference of 2.
so its sum would be given by formula :
sum = n/2(2a+(n-1)d
where n = Math.ceil(k); where k is the given number.
and d = 2, a=1
public int sumOfOddIntegers (int n) {
if(n < 1);
return 0;
int totalNumber = Math.ceil(n/2);
return (totalNumber/2)*(2 + (totalNumber-1)*2);
`