Implementation of CountSort [closed] - c++

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()

Related

How to Increment values of an an array using a nested-loop [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
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}.

Represent spiral of numbers in a matrix [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 4 years ago.
Improve this question
How to dynamically fill the following output in a two-dimensional array with the same number of rows and columns.
Edit: Here is the solution in c++ using #TheGeneral code from c#.
#include <iostream>
using namespace std;
int main(){
int size = 10;
int half = size/2;
int matrix[size][size];
int number1 = 0;
int number2 = 0;
for(int i = 1; i<=size; i++){
for(int j = 1; j<= size; j++){
if(i > half){
number1 = size + 1 - i;
}else{
number1 = i;
}
if(j > half){
number2 = size + 1 - j;
}else{
number2 = j;
}
if(number1 < number2){
matrix[i-1][j-1] = number1;
}else{
matrix[i-1][j-1] = number2;
}
}
}
cout<<"MATRIX:"<<endl;
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
cout<<"["<<matrix[i][j]<<"] \t";
}
cout<<endl;
}
return 0;
}
For a bit of fun
The only note worthy things going on, are
Starting the index from 1 (makes things easier)
Conditional Operator to say if an index is greater than 5 reverse the count
Math.Min to make it syemtrical
Exmaple
private static int[, ] CreateArray()
{
var ary = new int[10, 10];
for (var i = 1; i <= 10; i++)
for (var j = 1; j <= 10; j++)
ary[i - 1, j - 1] = Math.Min(i > 5 ? 11 - i : i, j > 5 ? 11 - j : j);
return ary;
}
Demo here

C++ code not running as it should be [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 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.

Nested loop with arrays having repeating numbers [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 8 years ago.
Improve this question
I have an array that keeps the number of each element.
int total[5] = {2,3,4,5,6}
int num = 5; //array total has 5 elements
This means that we have 2 element 0's, 3 element 1's in our original array. We are not worried about the original array since I already have a code to keep the number of the elements.
I need a nested for loop that creates a new array that looks like this:
array[0] = 1;
array[1] = 1;
array[2] = 5;
array[3] = 5;
array[4] = 5;
array[5] = 9;
array[6] = 9;
array[7] = 9;
array[8] = 9;
and so on. That is, we store a value in our new array as many as the its value in "total" array. Values 1,5,9, etc. are stored in an array called element. I have something like this so far:
for (int i = 0; i < num; i++){
for (int j = 0; j < total[i]; j++){
array[i + j] = element[i];
}
}
Can somebody help me to figure this out?
An easy solution (though not necessarily elegant) is to do the following:
int count = 0;
for (int i = 0; i < num; i++){
for (int j = 0; j < total[i]; j++){
array[count] = element[i];
count++;
}
}
then you don't need to worry about trying to figure out what position you are at for the array.
You need to track the number of the elements:
int sum = 0;
for (int i = 0; i < num; i++){
for (int j = 0; j < total[i]; j++){
array[sum + j] = element[i];
}
sum += total[i];
}

What is wrong with this bit of code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am trying to augment a matrix with an identity matrix of similar dimensions, why is this bit of code not working?
It keeps giving the error 'vector subscript out of range'
for (i = 0; i < matrix.size(); i++)
for (j = matrix.size(); j < 2 * matrix.size(); j++)
if (i == j % matrix.size())
matrix[i][j] = 1;
else
matrix[i][j] = 0;
For a square matrix I think you are initialising j incorrectly?
for (i = 0; i < matrix.size(); i++)
for (j = 0; j < matrix.size(); j++)
if (i == j)
matrix[i][j] = 1;
else
matrix[i][j] = 0;
Edit: So to augment I think the following to extend the length of the rows
(http://en.wikipedia.org/wiki/Augmented_matrix)
for (i = 0; i < matrix.size(); i++)
matrix[i].resize(2 * matrix.size())
for (j = matrix.size(); j < 2 * matrix.size(); j++)
if (i == j % matrix.size())
matrix[i][j] = 1;
else
matrix[i][j] = 0;
You have to resize each row-vector (or row-array maybe) first. Otherwise the cell you try to access will be outside of your row range (and you receive the corresponding error).
You can do this by matrix[row].resize(2*matrix[row].size());, where row = 0 .. matrix.size()-1.