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 6 years ago.
Improve this question
Isn't the below code supposed to initialize all the array elements to 0 using the for loop?
#include<iostream>
using namespace std;
int main()
{
int a[10];
for(int i=0; i==9; i++){
a[i] = 0;
}
for(int i=0;i<10;i++){
cout << a[i] << endl;
}
}
Output:-
164752
6
0
0
-13120
0
-13211
0
0
0
Check here corrected-code
#include<iostream>
using namespace std;
int main()
{
int a[10];
for(int i=0; i<10; i++){ // <= i < 10 instead of i == 9
a[i] = 0;
}
for(int i=0;i<10;i++){
cout << a[i] << endl;
}
}
No, because the comparison in the for loop is not correct. It just checks if i is 9 at the beginning, which turns out to be false, and hence, the first for loop doesn't execute at all.
The correct condition should be i<=9 (or i<10) instead of i==9, which is what you have written in the 2nd loop.
In a for loop like for (int i = 0; i == 9; i++) the second expression (i == 9 is the condition for continuing to run the loop. Since i is 0 the first time through the loop, i == 9 is false, and the loop immediately ends; execution continues with the next statement. Change it to i < 10; that way, on the first time through the loop, when i is 0, the condition is true, and execution continues. In fact, it continues until i < 10 becomes false, which happens after the tenth execution of the body of the loop.
Corrected code:
#include <iostream>
int main()
{
int a[10];
for (int i = 0; i != 10; i++)
a[i] = 0;
for (int i = 0; i != 10; i++)
std::cout << a[i] << '\n';
}
Related
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.
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
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
I just recently learned to do selection sorting an array. I am also using X-Code on a mac.
I thought I did everything correctly, but I seem to keep getting this error message on the if statement :
Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff5fc00000).
What am I doing wrong?
using namespace std;
void selectionSorting(int array[], int n)
{
for(int i = 0; i < n-1; n++)
{
int min = i;
for(int j = i + 1; j < n; j++)
{
if(array[j] < array[min]) //Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff5fc00000)
min = j;
}
int temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}
int main()
{
int n = 10;
int array[]= {10,9,8,7,6,5,4,3,2,1};
selectionSorting(array, n);
for(int x=0; x < n; x++)
{
cout << array[x] << " ";
}
return 0;
}
You have a logical error at for(int i = 0; i < n-1; n++). It should be for(int i = 0; i < n-1; i++) (iterate through the elements of the array).
Also EXC_BAD_ACCESS suggests that your are trying to access a piece of memory that is no longer accessible or it doesn't go well with the intended use.
See that this occurs at if(array[j] < array[min]), which is obvious because j is going beyond the array length as you do n++.
As suggested in the comments try using a debugger.
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
I am currently going through a fibonacci practice problem on hackerrank and am having a malloc memory corruption error. This is the link to the problem I am doing:
https://www.hackerrank.com/contests/programming-interview-questions/challenges/fibonacci-returns/
Input is 0-10, each number separated by a new line.
For each input, the value at that point in the sequence is printed. It works for small inputs, but after 6 it gets the malloc error. It doesn't seem that the size of the sequence is an issue either, just how many are done in succession.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
vector<int> bigFib(1);
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int x;
while(cin >> x){
if(bigFib.size()-1 >= x){
cout << bigFib[x] << endl;
}
else{
vector<int> fib(x);
fib[0] = 0;
fib[1] = 1;
for(int j = 2; j <= x; j++){
fib[j] = fib[j-1] + fib[j-2];
}
bigFib = fib;
cout << fib[x] << endl;
}
}
return 0;
}
I am pretty new to C++ and can't find the problem. Thanks for your time.
When you create std::vector of size N, you can access elements with index [0, N-1] - which is N elements. You create vector of size x and in your loop:
for(int j = 2; j <= x; j++){
fib[j] = fib[j-1] + fib[j-2];
}
and in this statement
cout << fib[x] << endl;
you try to access element with index equal to x, which is UB. If you do need to access index x create vector with at least x+1 size
In vector<int> fib(x); you declare a vector<int> that has x elements. Those elements are fib[0] through to fib[x - 1]. However, in for(int j = 2; j <= x; j++){ fib[j] = ... you assign to an element out of bounds.
Imagine if x is 1, then you'd expect your fib vector to contain only one element: fib[0]... yet your loop is assigning to fib[1]. Problem? Yup.
I reckon for(int j = 2; j <= x; j++){ should probably be for(int j = 2; j < x; j++){...
... and cout << fib[x] << endl; should be cout << fib[x - 1] << endl;
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 8 years ago.
Improve this question
I've got a 2d array airport[30][5] which prints out like this:
Departure, Destination, Airline, Price, Duration
I want the user to enter in their own departure and destination (stored in variables x and y), and then I will search through the array to see if the first two elements of each row matches what the user has input. If they both match, I want to print out all the rows where they both match.
So far I have a simple loop but it prints out all the lines in the array:
for(int i = 0; i < 5; i++) {
for(int j=0; j < 30; j++) {
if(airport[i][0] == x && airport[i][1] == y) {
cout << Line(s) from array
}
}
}
something like this?
for(int j=0; j < 30; j++) {
if(airport[j][0] == x && airport[j][1] == y) {
for (int i=0; i < 5; i++) {
cout << airport[j][i] << " ";
}
cout << endl;
}
}
You need only one loop
for ( int i = 0; i < 30; i++) {
if ( airport[i][0] == x && airport[i][1] == y) {
// cout << Current Line from array
}
}