Unable to count sort more than eight elements - c

Here is my code. While choosing values, if I try to put 9 values it dumps garbage value. It has happened while doing quick sort as well
#include <stdio.h>
void printArray(int* A, int n) {
for (int i = 0; i < n; i++) {
printf("%d ", A[i]);
}
printf("\n");
}
int maximum(int A[],int n) {
int i, max = A[0];
for (i = 1; i < n; i++) {
if (A[i] > max) {
max = A[i];
}
}
return max;
}
void countSort(int A[], int n) {
int i, max = maximum(A, n);
int count[max + 1], B[n];
for (i = 0; i < max + 1; i++) {
count[i] = 0;
}
for (i = 0; i < max + 1; i++) {
count[A[i]]++;
}
for (i = 1; i < n; i++) {
count[i] += count[i - 1];
}
for (i = n - 1; i >= 0; i--) {
B[--count[A[i]]] = A[i];
}
for (i = 0; i < n; i++) {
A[i] = B[i];
}
}
int main(){
int A[] = {1, 4, 6, 2, 3, 2, 3, 2, 7};
int n = 9;
printArray(A, n); // Printing the array before sorting
countSort(A, n); // Function to sort the array
printArray(A, n); // Printing the array before sorting
return 0;
}

This code uses the wrong limit:
for(i=0;i<max+1;i++) {
count[A[i]]++;
}
It effectively iterates through the elements of A, which has n elements, not max+1.

Related

C programming find the second positive number in the second half of an array

My code is this:
#include <stdio.h>
int array_second_positive(int A[], int size) {
int j, i;
for (i = 0; i < size / 2; ++i) {
A[i] = A[0];
for (j = 0; j < size; ++j) {
A[j] = A[j + 1];
}
}
for (i = 0; i < size; ++i) {
int z = 0;
int counter = 0;
while (z < size) {
if (A[z] > 0) {
counter++;
}
if (counter == 2) {
return A[z];
}
z++;
}
}
}
int main(void) {
int size = 6;
int array[] = { 3, -14, 15, 2, 6, 5 };
int result = array_second_positive(array, size);
}
I am trying to find the second positive number in the second half of an array. If we assume array size is even and we have definitely at least two positive number in the second half but I get no results.

How do I selection sort array A to B without copying arrays?

#include <stdio.h>
int size;
void SelectionSort(int a[], int size) {
int i, j, t, min, b[] = { 0,0,0,0,0,0,0,0 };
printf("\nelements to sort : ");
for (t = 0; t < size; t++) printf("%d ", a[t]);
printf("\n\n<<<<<<<<<<<<<<<<< selection sort >>>>>>>>>>>>\n");
for (i = 0; i < size - 1; i++) {
min = i;
for (j = i + 1; j < size; j++) {
if (a[j] < a[min]) min = j;
}
b[i] = a[min];
printf("\nA ARRAY %d : ", i + 1);
for (t = 0; t < size; t++) printf("%3d", a[t]);
printf("\nB ARRAY %d : ", i + 1);
for (t = 0; t < size; t++) printf("%3d", b[t]);
}
}
void main() {
int list[8] = { 69, 10, 30, 2, 16, 8, 31, 22 };
size = 8;
SelectionSort(list, size);
getchar();
}
the issue is that whenever the comparing is done the number 2 is copied in the array
what should i do to fix this?
Each step of the selection sort needs to
Identify the least element in the subarray
Remove the element
Place it at the front
Your code is skipping step 2. So it keeps finding the 2 until i increments past it.
Instead of copying into a different array b, swap the lowest element with a[i].
t = a[min];
a[min] = a[i];
a[i] = t;
BTW, it would be helpful in this sort of question to show us the output so potential answerers don't need to compile and run the program themselves.
Here is a modification of the usual selection sort, that takes duplicates into account.
In each inner loop, the whole array is read. Only values larger than the preceding minimum (lmin) are considered, and values equal to the current accepted minimum (min) are counted (nmin is the count). Then, the array b is updated with nmin values equal to min. The first loop is a special case as there is no preceding minimum.
#include <stdio.h>
void selection_sort(int n, int a[n], int b[n]) {
int j, k, lmin, min, nmin, first;
min = a[0];
nmin = 1;
for (j = 1; j < n; j++) {
if (a[j] == min) {
nmin++;
} else if (a[j] < min) {
min = a[j];
nmin = 1;
}
}
for (k = 0; nmin > 0; nmin--) {
b[k++] = min;
}
while (k < n) {
lmin = min;
first = 1;
for (j = 0; j < n; j++) {
if (a[j] > lmin) {
if (first) {
first = 0;
min = a[j];
nmin = 1;
} else if (a[j] == min) {
nmin++;
} else if (a[j] < min) {
min = a[j];
nmin = 1;
}
}
}
for ( ; nmin > 0; nmin--) {
b[k++] = min;
}
}
}
#define N 20
int main(void) {
int a[] = {2, 5, 9, 8, 4, 7, 1, 7, 5, 0, 3, 8, 3, 3, 6, 1, 8, 0, 2, 8};
int b[N];
selection_sort(N, a, b);
for (int i = 0; i < N; i++) {
printf("%d ", a[i]);
}
printf("\n");
for (int i = 0; i < N; i++) {
printf("%d ", b[i]);
}
printf("\n");
return 0;
}
The purpose of the boolean first is to detect the first accepted current minimum (that is, the first value that is larger than lmin).
Another way would be to set min = INT_MAX before the beginning of the inner loop, where INT_MAX is the maximum int value (defined in limits.h): we know lmin < INT_MAX, otherwise we would already have exited the function. Here is this variant:
#include <stdio.h>
#include <limits.h>
void selection_sort(int n, int a[n], int b[n]) {
int j, k, lmin, min, nmin;
min = a[0];
nmin = 1;
for (j = 1; j < n; j++) {
if (a[j] == min) {
nmin++;
} else if (a[j] < min) {
min = a[j];
nmin = 1;
}
}
for (k = 0; nmin > 0; nmin--) {
b[k++] = min;
}
while (k < n) {
lmin = min;
min = INT_MAX;
nmin = 0;
for (j = 0; j < n; j++) {
if (a[j] > lmin) {
if (a[j] < min) {
min = a[j];
nmin = 1;
} else if (a[j] == min) {
nmin++;
}
}
}
for ( ; nmin > 0; nmin--) {
b[k++] = min;
}
}
}
#define N 20
int main(void) {
int a[] = {2, 5, 9, 8, 4, 7, 1, 7, 5, 0, 3, 8, 3, 3, 6, 1, 8, 0, 2, 8};
int b[N];
selection_sort(N, a, b);
for (int i = 0; i < N; i++) {
printf("%d ", a[i]);
}
printf("\n");
for (int i = 0; i < N; i++) {
printf("%d ", b[i]);
}
printf("\n");
return 0;
}
Note that in this version, nmin is set to zero before the inner loop: it doesn't matter if there are values smaller than INT_MAX. But if all remaining values are equal to INT_MAX, we are going to count them with nmin++, so we must start at zero.

My function doesn't do anything with arrays

Function could replace first n elements from array A with last n elements of array B.
Output is
Array A
1
2
3
4
5
Array B
6
7
8
9
10
I tried to put printf (i) in every loop and it seems to be work fine but it do nothing with arrays a and b :(
void reparray(int *a, int *b, int n){
int i;
int last_element;
int help[5] = {};
i = 0;
for (i; i<n; i++){
help[i] = a[i];
}
last_element = 4;
for (last_element; last_element>=n; last_element--){
help[last_element] = b[last_element];
}
i = 0;
for (i; i<n; i++){
a[i] = help[i];
}
last_element = 4;
for (last_element;last_element >= n; last_element--){
b[last_element] = help[last_element];
}
}
int main()
{
int a[5] = {1, 2, 3, 4, 5};
int b[5] = {6, 7, 8, 9, 10};
int n,i;
reparray(a, b, 2);
printf("Array A\n");
i = 0;
for (i; i<5; i++){
printf("%d\n", a[i]);
}
printf("Array B\n");
i = 0;
for (i; i<5; i++){
printf("%d\n", b[i]);
}
return 0;
}
your 2 last loops in void reparray do nothing except copying exact things which were already in a and b
void reparray(int* a, int* b, int n) {
int i;
int last_element;
int help[5] = {};
i = 0;
for (i; i < n; i++) {
help[i] = a[i];
}
last_element = 4;
for (last_element; last_element >= n; last_element--) {
help[last_element] = b[last_element];
}
last_element = 4-1;
i = 0;
for (i = 0; i < n; i++) {
a[i] = help[last_element];
last_element++;
}
i = 0;
for (i; i< n; i++) {
b[i] = help[i];
}
}
for other input change void reparray
void reparray(int* a, int* b, int n) {
int i;
int last_element;
int help[5] = {};
i = 0;
for (i; i < n; i++) {
help[i] = a[i];
}
last_element = 4;
for (last_element; last_element >= n; last_element--) {
help[last_element] = b[last_element];
}
last_element = 4 - 1;
i = 0;
for (i = 0; i < n; i++) {
a[i] = help[last_element];
last_element++;
}
last_element = 4;
int first_element = 0;
for (i = last_element - n + 1; i < last_element + 1; i++) {
b[i] = help[first_element];
first_element++;
}
}

C_(visual)_Debug Error_ Run-Time Check Failure #2 -S

I want to reverse numbers of array,
but I can't understand why it didn't run.
Thanks for explaining what does Debug Error_ Run-Time Check Failure #2 -S mean..
Thanks,
#include <stdio.h>
int main()
{
int arr[] = { 1,2,3,4,5 };
int size, i, j;
int temp = 0;
size = sizeof(arr) / sizeof(arr[0]); //use this for changing size
printf("first_array :");
for (i = 0; i < size; i++)
{
printf("%d", arr[i]);
}
printf("\n");
for (i = 0; i <= (size / 2); i++)
{
j = size - i;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
printf("Riv_array :");
for (i = 0; i < size; i++)
{
printf("%d", arr[i]);
}
return 0;
}
Array index starts from 0 in C and your array has 5 elements so arr[4] is the last element but your code:
j = size - i;
arr[j];
when i=0 access to arr[5], this is your code error: Array index out of bound.
you should use j = size - i-1; to point to the last element of array, not j = size - i;
see this working sample (your sample code with some edit):
#include <stdio.h>
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int size, i, j;
int temp = 0;
size = sizeof(arr) / sizeof(arr[0]); //use this for changing size
printf("first_array :");
for (i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
for (i = 0; i <= (size / 2); i++)
{
j = size - i - 1;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
printf("Riv_array :");
for (i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
another way to reverse array using pointers:
#include <stdio.h>
void reverse(int* p, int count){
int temp;
int* q = p + count - 1; // point to the end
count /= 2;
while (count--) {
temp = *p;
*p++ = *q;
*q-- = temp;
}
}
void print_array(int *p, int count){
while (count--) printf("%d ", *p++);
printf("\n");
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int count = ((&arr)[1] - arr);
print_array(arr, count);
reverse(arr, count);
print_array(arr, count);
return 0;
}
output:
1 2 3 4 5
5 4 3 2 1

How to write all combination with given array

I have some array with numbers. When I want to write all combination with 3 digits I wrote this. But now I need edit this code to return all combination with 1 to numbers.size digits. How should I edit it ?
int items[] = {1, 2, 3, 4, 5};
int itemSize = 5;
for (int i = 0; i < itemSize - 2; i++) {
for (int j = i + 1; j < itemSize - 1; j++) {
for (int k = j + 1; k < itemSize; k++)
printf("%d%d%d\n", items[i], items[j], items[k]);
}
}
it is not perfect solution, but it is well enough to work. you can get all sub array from an array, from 1 element to length array
void permute(char* previous, char *a, int i, int n,int nmax)
{
int j;
if (i == n)
{
char c = a[nmax];
a[nmax] = 0;
if (strstr(previous,a) == 0)
{
printf("%s\n", a);
strncpy(previous,a,n);
}
a[nmax] = c;
}
else
{
for (j = i; j < n; j++)
{
char c = a[i];
a[i] = a[j];
a[j] = c;
permute(previous,a, i+1, n,nmax);
c = a[i];
a[i] = a[j];
a[j] = c;
}
}
}
void subarrays(char *a, int len)
{
int i=1;
char *previous = strdup(a);
*previous = 0;
for(i=1;i<len+1;i++)
{
permute(previous,a,0,len,i);
}
}
int main(void) {
char *arr = strdup("abcde");
subarrays(arr,strlen(arr));
return EXIT_SUCCESS;
}
Since you're already doing a web search for this, here is a generic solution.
#include <stdio.h>
int next_combination(size_t *I, size_t k, size_t n)
{
size_t i, j;
i = k-1; /* find next element to increment */
while(I[i] == (n-k+i)){
--i;
if(i == (size_t)-1){ /* if done */
for(i = 0; i < k; i++) /* return with initial combination */
I[i] = i;
return(0);
}
}
I[i] += 1; /* increment element */
for(j = i+1; j < k; j++) /* create increasing string */
I[j] = I[i]+j-i;
return(1); /* return with new combination */
}
int main(int argc, char **argv)
{
int A[5] = {1, 2, 3, 4, 5};
size_t I[5];
size_t i, k, n;
n = sizeof(A)/sizeof(A[0]); /* n things */
for(k = 1; k <= n; k++){ /* n things k at a time */
for(i = 0; i < k; i++) /* create initial combination */
I[i] = i;
do{ /* display combinations */
for(i = 0; i < k; i++)
printf("%2d", A[I[i]]);
printf("\n");
}
while(next_combination(I, k, n));
}
return(0);
}

Resources