my bool function not working,dont know why [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
My intention was to count the sum of prime numbers in a certain range. Its working fine when the range is less than or equal to 8. When I take input 9 it shows output 26 while the answer is 17. My program identifying 9 as a prime and adding with the sum.
{
for(int i=3; i*i<=n; i+=2)
{
if(n%i==0) return false;
}
return true;
}`
`int main(){
int t,sum=0;
cin >> t;
for(int k = 0; k < t; k++){
int n;
cin >> n;
if(n>=2) sum=2;`
` for(int i=3;i<=n;i+=2)
{
ifPrime(i);
if(ifPrime) sum=sum+i;
}`
` cout<<sum<<endl;
sum=0;
}
return 0;
}

my guess
this
ifPrime(i);
if(ifPrime) sum=sum+i;
should be
if(ifPrime(i)) sum=sum+i;
but without seeing all the code its impossible to say

Related

this is showing wrong output when i am giving this more then one test cases [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
this is codechef January challenge's first problemcodechef.
When i am giving more than one test case ,it is giving wrong output. and when i am giving one test cases it is going right.
my code:-
#include<iostream>
using namespace std;
int long long N;
int main()
{
int i,j, N,t,d,k,sum=0;
cin>>t;
for(j=1;j<=t;j++)
{
cin>>N>>k>>d;
for(i=0;i<N;i++)
{
int num;
cin >> num;
sum += num;
}
for(i=0;i<N;i++)
{
sum= int(sum/k);
if(sum>d)
{
cout<<d<<endl;
}
else
{
cout<<sum<<endl;
}
}
}
return 0;
}
Your output is in the wrong place, it should be inside the test case loop not after it. You also need to print a newline after every result
Your code is like this
for(j=1;j<=t;j++)
{
...
}
if(sum>d)
{
cout<<d;
}
else
cout<<sum;
but it should be like this
for(j=1;j<=t;j++)
{
...
if(sum>d)
cout<<d<<'\n';
else
cout<<sum<<'\n';
}
Mistakes like this are much easier to spot if you get into the habit of indenting your code consistently.
Another error is that you don't reset sum to zero for each test case. Your loop should also look like this
for(j=1;j<=t;j++)
{
sum=0;
...
}
Some sloppy mistakes. To be a programmer you really have to pay attention to the details.
For free, here's another tip. You don't need to use an array for this problem. To add up numbers you don't need to put them all in an array first and then add them up later. You can just add them up as you input them.
int sum = 0;
for (int i = 0; i < N; i++)
{
int num;
cin >> num;
sum += num;
}

Minimize Number- c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
how can I Print the maximum possible number of operations that can be performed?
The operation is as follows: if all numbers are even then divide each of them by 2 otherwise, you can not perform any more operations.
First line contains a number N (1 ≤ N ≤ 200) number of elements.
Second line contains N numbers (1  ≤  A I  ≤  109).
Examples
Input
3
8 12 40
Output
2
Input
4
5 6 8 10
Output
0
I will write the cod in the comment cause I don't know how to put it here ..can you please edit it for me?
I know my code is wrong but I can't fix it so please explain it for me .
In this loop:
for(int i=0; i<n && 2<=n<=100 ;i++) {
2<=n<=100 is not doing what you mean. It evaluates to (2 <= n) <= 100) which is a different check.
You need to do:
2 <= n && n <= 100
You need to fix the other checks where you are doing this as well.
Also, since n is not being modified inside the loop, you can hoist this check out of the loop:
if (2<=n && n <=100) {
for(int i=0; i<n ;i++) {
and similarly for the other loops as well.
You are taking 'x' with n and m, but it has to be at end.
Also just use an 'index' variable to keep track of where to insert in your array. And then use the same 'index' while finding the 'x'.
#include <iostream>
using namespace std;
int main()
{
int a[100000],n,m,x;
cin>>n>>m;
bool flag=0;
int index =0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++) {
cin >> a[index];
index++;
}
}
cin >>x;
for(int i=0;i<index;i++){
if(x==a[i]) {
flag = true;
}
}
if(flag==0){
cout<<"will not take number";
} else if(flag==1){
cout<<"will take number";
}
return 0;
}

unable out figure out error in below program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I was solving a problem https://codeforces.com/contest/489/problem/B
Its a simple brute force approach,In my terminal when i am giving input
#include<bits/stdc++.h>
using namespace std;
vector <int> b;
vector <int> g;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
b.push_back(a);
}
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int a;
cin >> a;
g.push_back(a);
}
sort(b.begin(), b.end());
sort(g.begin(), g.end());
int ans = 0;
bool visited[10000];
memset(visited, sizeof(visited), false);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(!visited[j])
if (abs(b[i] - g[j]) <= 1) {
visited[j] = true;
ans++;
break;
}
}
}
cout << ans;
}
4
1 4 6 2
5
5 1 5 7 9
I am getting correct output as 3 , This is the very first test case on codeforces also and codeforces showing output as 2 and as showing as wrong answer.
Please see here Proof ,I never faced such kind of problem in competitive
programming .
accepted solution solution
There was also announcement related to this ques read below here
This is a case of undefined behavior: if(!visited[j]) is undefined. visited is not initialized because the call memset(visited, sizeof(visited), false); is wrong. You are reading uninitialized variables.
The declaration of memset is
void *memset( void *dest, int ch, size_t count );
You are writting 0 times the value 10000 into visited. On your machine this memory was filled with zeros. But on other machines there can be different values.

My code seems to have a syntax error in finding the maximum number [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
What is wrong with my code?
It has to find the maximum between n numbers.
#include<iostream>
using namespace std;
int main()
{
int n,i = 0;
cin >> n;
int a[n];
while(i < n)
{
cin >> a[i];
i++;
}
i=1;
while(i <= (n + 1))
{
if (a[i] > a[0])
{
a[0] = a[i];
}
i++;
}
cout << a[0];
return 0;
}
This is meant to be a learning thing for you so I don't want to do your code for you. That being said I would look at while(i <= (n + 1)). You are stepping outside of the bounds of your array.

program which check perfect numbers - something gets wrong [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have to write a function which gets a number, and prints all the perfect numbers that are smaller than the number given. If there are not any, the function prints an appropriate message.
The program I have just written is compiled well, but it's working only partially. If the input number is 5, for instance, for some reason I don't get any message that there are not perfect numbers in this range (=until 5) .
Can someone please explain to me what's wrong in this program?
I would appreciate any help!
#include<iostream>
using namespace std;
void check (int num, int & j);
void main()
{
int num,j, count;
cout << "List all the perfect numbers less than: ";
cin >> num;
check (num, j);
}
void check (int num, int & j)
{
int i,sum, count=0;
for(j=2;j<num;num++)
{
sum=0;
for(i=1;i<j;i++)
{
if(j%i == 0)
sum += i;
}
if(sum == j)
{cout << j <<endl;
count+=1;
}
}
if (count==0)
cout<<"there are no perfect numbers"<<endl;
}
There is an infinite loop in your code:
for(j=2;j<num;num++)
I think it should be
for(j=2;j<num;j++)