unable out figure out error in below program [closed] - c++

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.

Related

my bool function not working,dont know why [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 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

What is the problem of c++ no value sort()? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
While I was studying algorithms, I was trying to run a sort order, but there was an error.
It says there are no members in the value, but there is an error even if you declare the structure. Do you know why?
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct table{
int startValue;
int finishValue;
};
bool cmp(table f, table s)
{
if (f.finishValue == s.finishValue)
return f.startValue < s.startValue;
else
return f.finishValue < s.finishValue;
}
int main(int argc, char *argv[])
{
int maxTimeTable;
cin >> maxTimeTable;
vector<table>t(maxTimeTable);
for (int i = 0; i < maxTimeTable; i++)
{
cin >> t[i].startValue >> t[i].finishValue;
}
sort(t.startValue(), t.finishValue(), cmp); //Error Occurrence Point
int cnt = 0;
int n = 0;
for (int i = 0; i < t.size(); i++)
{
if (n <= t[i].startValue)
{
n = t[i].finishValue;
cnt++;
}
}
}
You are treating t as a table-object
sort(t.startValue(), t.finishValue(), cmp); //Error Occurrence Point
but it's not, right? t is defined as vector<table>t(maxTimeTable);. And correctly, the compiler provides an error, since vector<table> does not have a method startValue nor a finishValue.
You probably meant to sort the vector by accessing the iterator to the first element, and to the last element:
sort(t.begin(), t.end(), cmp);

Value of array index not increasing [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
Here if the condition occurs the value of i store in the array prime. But the index of the array is not increasing, all the values are storing at [0] index and destroy the previous value.
Tried all the other methods but didn't find anything.
I tried prime[x++] and prime[x+1], but they both didn't work for me. If someone gives me a solution then I'll be very thankful to you.
#include<iostream>
using namespace std;
int main()
{
int num = 20, prime[20], x;
for (int i = 1; i <= num; i++) {
if (i % 2 != 0) {
prime[x] = i;
}
}
for (int k = 1; k <= num; k++) {
cout << prime[x] << "\t";
}
}
You have the variable x un-initialized and you are using it, in the line
prime[x] = i;
assuming that it has been initialized. This invokes undefind behavior in your program and the result could not be predicted. Initialize it with the appropriate number to make the program to have a defined behavior.
Regarding prime numbers, see this SO post: Printing prime numbers from 1 through 100.
defining x as uninitialized is a undefined behaviour.
Initialize x as 0 (int x = 0;)
Try with bellow :
int x = 0;
for(int i=1; i<=num; i++){
if(i%2!=0){
prime[x] = i;
x++;
}
}
Now you have the number of prime array elements :
Now print prime array :
for(int k=0; k<x; k++){
cout << prime[k] << "\t";
}
The output (it seems your code detect odd numbers) :
1 3 5 7 9 11 13 15 17 19
Test the code online

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.

Sum of the first 5 even numbers from the array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
So, I need to get the sum of the first 5 even numbers from my array, this is the code that I've got so far, have no clue what to do next. It runs, but the result is incorrect
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int niz[10];
cout << "Unesi 10 brojeva:";
for (int i = 0; i < 10; i++) {
cin >> niz[i];
}
int suma = 0;
int parni[5];
int j = 0;
for (int i = 0; i < 10; i++) {
if (niz[i] % 2 == 0) {
niz[i] == parni[j];
j++;
if (j == 5) {
break;
}
}
}
for (int i = 0; i < 5; i++) {
suma = parni[i] + suma;
}
cout << suma;
system("PAUSE");
return 0;
}
This line:
niz[i] == parni[j];
does nothing. (It tests if niz[i] happens to be equal to the current, uninitialized value of parni[j], and throws away the result of the comparison.)
You want to store niz[i] in parni[j], so do this:
parni[j] = niz[i];
Incidentally, there is a problem if there are fewer than 5 even numbers in the niz array. In that case, you still sum up all five entries of the parni array, using uninitialized values, which is Bad. One way to avoid this is to just sum up the even entries as you find them, without using a secondary array.
IE, do suma += niz[i] at the line in question, and get rid of parni altogether.
Unless you're really required to use arrays here, vectors will work much more nicely.
You can also use a couple of standard algorithms to make your life easier (especially std::copy_if and std::accumulate).
// for the moment I'll ignore the code to read the input from the user:
auto input = read_input();
auto pos = std::remove_if(input.begin(), input.end(),
[](int i) { return i % 2 != 0; });
// assume that `input` always contains at least 5 elements
pos = std::min(pos, input.begin() + 5);
sum = std::accumulate(input.begin(), pos, 0);