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 7 years ago.
Improve this question
Here is my C++ code for counting sort algorithm , there is no errors as well as no warnings but when I want to execute it it's give me "Counting.exe has stopped working" I think it is a run time error.
void Counting_sort()
{
int A[]={5,15,20,30,40,8,36,25,96,15,40,15,96,47,20};
int k = 15 ;
int n = 15;
int i, j;
int B[15];
int C[100];
for(i = 0; i <= k; i++)
C[i] = 0;
for(j =1; j <= n; j++)
C[A[j]] = C[A[j]] + 1;
for(i = 1; i <= k; i++)
C[i] = C[i] + C[i-1];
for(j = n; j >= 1; j--)
{
B[C[A[j]]] = A[j];
C[A[j]] = C[A[j]] - 1;
}
cout << "\nThe Sorted array is : ";
for(i = 1; i <= n; i++)
cout << B[i] << " " ;
}
void main()
{
Counting_sort();
}
for(j = n; j >= 1; j--)
{
// You are accessing A[j]
}
So A[15] is a invalid access and will lead to undefined behavior.
The valid access for array A[15] is A[0] to A[14] anything other than this is array out of bound access.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
when I trying to implement the counting sort I got this error " the problem caused the program to stop working correctly Windows will close the program and notify you if a solution is available "
void CountingSort(int *A,int size) {
int SizeC = Max(A, size);
int* B = new int[size];
int* C = new int[SizeC+1];
for (int i = 0; i < SizeC; i++) {
C[i] = 0;
}
for (int i = 0; i < size; i++) {
C[A[i]]++ ;
}
for (int i = 0; i <SizeC; i++)
{
C[i] += C[i - 1];
}
for (int j = size - 1; j >= 0; j--) {
B[C[A[j]]] = A[j];
C[A[j]] --;
}
for (int i = 0; i < size; i++) {
cout << B[i] << "\t" << endl;
}
delete[] C;
delete[] B;
}
this is the error
i < SizeC and i <SizeC should be i <= SizeC. Otherwise, the elements with value SizeC won't be treated properly.
C[i] += C[i - 1]; with i = 0 will result in out-of-range read of C[-1]. The initialization of corresponding for loop should be int i = 1, not int i = 0.
The decrementing C[A[j]] --; should happen before B[C[A[j]]] = A[j];, or out-of-range write of B[size] will happen.
This program won't work well when the array A contains negative values.
Fixed code (negative values are still not supported):
void CountingSort(int *A,int size) {
int SizeC = Max(A, size);
int* B = new int[size];
int* C = new int[SizeC+1];
for (int i = 0; i <= SizeC; i++) {
C[i] = 0;
}
for (int i = 0; i < size; i++) {
C[A[i]]++ ;
}
for (int i = 1; i <= SizeC; i++)
{
C[i] += C[i - 1];
}
for (int j = size - 1; j >= 0; j--) {
C[A[j]] --;
B[C[A[j]]] = A[j];
}
for (int i = 0; i < size; i++) {
cout << B[i] << "\t" << endl;
}
delete[] C;
delete[] B;
}
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
So I need help. The problem is that an int arr[5] = {0};
I know the array has the values {0,0,0,0,0} filling the whole array. At the end of the code the array must have the values {1,2,3,4,5} inside it. To solve it must use a nested for loop.
I have tried the following code
Sorry if there is an error in the way this question is formatted this is my first time and using mobile.
int arr[5] = {0};
for(int j = 1; j<6; j++)
{
for(int i = 0; i<5; i++)
{
arr[i] = j;
}
}
I don't know what the benefit of that but If you have to use nested for loop, the following is an option
int main()
{
int arr[5] = {0};
for(int j = 0; j < 5; ++j)
{
for(int i = 1; i < 2; ++i)
{
arr[j] = j+i;
}
}
for(int i{};i<5;++i) std::cout << arr[i] << ", ";
}
Or like #JaMit comment suggests
int main()
{
int arr[5] = {0};
for(int j = 0; j < 5; ++j)
{
for(int i = 0; i < j+1; ++i)
{
arr[j]++;
}
}
for(int i{};i<5;++i) std::cout << arr[i] << ", ";
}
I'm guessing your professor means you should generate the final results using only incrementation. In other words, I'm guessing you're not allowed to assign values after the int arr[5] = {0}; line (so an expression like arr[i] = j; is not allowed).
In that case, you could solve the problem as follows:
int arr[5] = {0};
for (int i = 0; i < 5; i++) {
for (int j = i; j < 5; j++) {
arr[j]++;
}
}
Now we have only used incrementation and the final result is {1, 2, 3, 4, 5}.
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
I just want to know why it come the question that string subscript out of range,I have already initialized the string array.Thank you.
#include<bits/stdc++.h>
#define N 100000
using namespace std;
string s[N + 5] = {}, ss[N + 5] = { " " }, fs[N + 5] = { " " };
int main()
{
int n, x; char c;
cin >> n;
for (int i = 0; i<n; ++i)
{
for (int j = 0; j<6; ++j)
{
scanf("%c", &c);//the error comes here.
if (c == ' ') continue;
s[i] += c;
}
ss[i] = s[i] + s[i];
for (int j = 5; j >= 0; --j) fs[i][j] = s[i][5 - j];
}
int flag = 0;
for (int i = 0; i<n; ++i)
{
for (int j = 0; j<n; ++j)
{
if (i == j) continue;
if (find(s[i], fs[j]) || find(s[i], ss[j]))
{
flag = 1; break;
}
}
}
}
1) Why should I not #include <bits/stdc++.h>?
2) Avoid global variables and use std::vector when size is unknown.
3) Why is using namespace std considered bad practice?
You're getting an out of range error because of fs[i][j] accessing empty strings f[i].
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 7 years ago.
Improve this question
I need to find the average of the negative elements in the two-dimensional array. This is what I've got so far:
#include <iostream>
using namespace std;
int main() {
int A[3][3];
int i, j;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
cout << "\n A[" << i + 1 << "][" << j + 1 <<"]=";
cin >> A[i][j];
}
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
cout << A[i][j] << "\t";
cout << "\n";
}
}
You could do it for example:
int negNumber = 0;
int sum = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (A[i][j] < 0) {
++negNumber; // increase negatives numbers found
sum += A[i][j]; // add to our result the number
}
}
}
sum = negNumber > 0 ? sum / negNumber : sum; // we need to check if we found at least one negative number
I hope it will help you but be careful! It will return a truncated value (an int).
the ternary operation could be difficult to undertand so you can do it:
if (negNumber > 0) {
sum /= negNumber;
}
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 8 years ago.
Improve this question
So to cut a long story short, I am trying to implement countsort for vector. I have some error somewhere in my code, though I think that I have followed the pseudo code pretty strictly.
The function:
void csort(vector<int>& a, vector<int>& b)
{
vector<int> c(a.size());
for(int i = 0; i <= c.size(); i++)
c[i] = 0;
for(int i = 0; i <= a.size(); i++ )
{
c[a[i]]++;
}
for(int i = 0; i <= c.size(); i++)
{
c[i]+= c[i-1];
}
for(int i = a.size(); i < 1; i--)
{
b[c[a[i]]] = a[i];
c[a[i]] =c[a[i]] -1;
}
}
The pesudo code:
let C[k] be a new array
for i = 0 to k
C[i] = 0
for j = 1 to A:length
C[A[j]] = C[A[j]] +1
for i = 1 to k
C[i] = C[i]+ C[i-1]
for j = A:length downto 1
B[C[A[j]]] = A[j]
C[A[j]] = C[A[j]] -1
The code as it is is very unreadable and error prone. In any case, what I first saw is that in the snippet below you will access c[-1] at the first iteration, thus incurring in undefined behaviour.
for (int i = 0; i <= c.size(); i++)
{
c[i] += c[i-1]; // Evaluation of c[i-1] is illegal for i == 0
}
for(int i = a.size(); i < 1; i--)
This code won't work at all.
for j = A:length downto 1
↑
To implement this pseudo code, you should do like this
for(int i(a.size()-1);i>=0;--i)
And for code like this( c[a[i]] ),you will get run-time error if a[i]<0 or a[i]>=c.size()