#include <stdio.h>
int main()
{
int array[8] = {4, 1, 5, 4, 2, 7, 4, 2};
int i, j, k;
int len = 8;
for(i = 0; i < len; i++)
{
for(j = i + 1; j < len;)
{
if(array[j] == array[i])
{
for(k = j; k < len - 1; k++)
{
array[k] = array[k + 1];
}
len--;
}
else
{
j++;
}
}
for(i = 0; i < len; i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
return 0;
}
Hi,
Above is the code to remove duplicated numbers in an array, but when compiles and executes, I get 4 1 5 2 7 2--which isn't right because I'm supposed to get 4 1 5 2 7.
It seems that I have a problem with len but couldn't figure out where and what in the code specifically needs to be fixed.
Don't know if it is a mistake in indentation, or you miss-copied the code from somewhere ?
anyways to get it run, make this minor changes and it runs (Don't expect an explanation I doubt you don't need it)
#include <stdio.h>
int main()
{
int array[8] = {4, 1, 5, 4, 2, 7, 4, 2};
int i, j, k;
int len = 8;
for(i = 0; i < len; i++)
{
for(j = i + 1; j < len;)
{
if(array[j] == array[i])
{
for(k = j; k < len - 1; k++)
{
array[k] = array[k + 1];
}
len--;
}
else
{
j++;
}
}
} // close the main `for` loop
// you used to print out after each iteration!
for(i = 0; i < len; i++)
{
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
Related
I was recently assigned the knight's tour problem.
Here is my try at it:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int count = 1;
int movej[8] = {1, -1,-2, 2, -2, -1, 1, 2};
int movei[8] = {2, 2, 1, 1, -1, -2, -2, -1};
void boardprinter(int **board)
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
printf("%d ", board[i][j]);
}
printf("\n");
}
printf("\n");
}
_Bool safe(int **board, int i, int j)
{
if ((board[i][j] == (-1)) && i >= 0 && i < 8 && j >= 0 && j < 8)
{
return 1;
}
return 0;
}
_Bool solve(int **board, int si, int sj)
{
if (count == 64)
{
boardprinter(board);
return 1;
}
int i=0;
while(i<8)
{
if (safe(board, (si + movei[i]), (sj + movej[i])))
{
board[si + movei[i]][sj + movej[i]] = count++;
if (solve(board, (si + movei[i]), (sj + movej[i])))
{
return 1;
}
else
{
board[si + movei[i]][sj + movej[i]] = -1;
}
}
i++;
}
return 0;
}
int main()
{
int **board = (int **)malloc(8 * sizeof(int *));
for (int i = 0; i < 8; i++)
{
*(board + i) = (int *)malloc(8 * sizeof(int));
for (int j = 0; j < 8; j++)
{
board[i][j] = -1;
}
}
// board initiallized
int si, sj;
scanf("%d %d", &si, &sj);
board[si][sj] = 1;
count++;
_Bool c = solve(board, si, sj);
printf("%d",c);
return 0;
}
I applied recursion and backtracking in this but the code crashes after reaching (4,2),now I think this fails because the while loop doesn't seem to behave properly (it gets teminated somehow)But I dont know why..
I have been stuck over this and tried all sorts of things to debug this
Kindly help me out!!
Thanks to Mr Tom Karzes for pointing out the mistakes in my code.
Cause of Error
The Problem with my code was that I was indexing into my array before checking if it was out of bounds,i.e. This happened because I assumed that the condition :
(board[i][j] == (-1)) && i >= 0 && i < 8 && j >= 0 && j < 8 would automatically fail if the array was out of bounds, but it turns out that I forgot that the compiler first checks (board[i][j] == (-1)) before checking the next mentioned
conditions,Which was the cause of the undefined behavior of my program.
Fixed Code
Here is the fixed code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int count = 0;
int movej[8] = {1, -1,-2, 2, -2, -1, 1, 2};
int movei[8] = {2, 2, 1, 1, -1, -2, -2, -1};
void boardprinter(int **board)
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
printf("%d ", board[i][j]);
}
printf("\n");
}
printf("\n");
}
_Bool safe(int **board, int i, int j)
{
if (i >= 0 && i < 8 && j >= 0 && j < 8)
{
if((board[i][j] == (-1)))
{
return 1;
}
}
return 0;
}
_Bool solve(int **board, int si, int sj)
{
if (count == 64)
{
boardprinter(board);
return 1;
}
int i=0;
while(i<8)
{
if (safe(board, (si + movei[i]), (sj + movej[i])))
{
board[si + movei[i]][sj + movej[i]] = count++;
if (solve(board, (si + movei[i]), (sj + movej[i])))
{
return 1;
}
else
{
board[si + movei[i]][sj + movej[i]] = -1;
count--;
}
}
i++;
}
return 0;
}
int main()
{
int **board = (int **)malloc(8 * sizeof(int *));
for (int i = 0; i < 8; i++)
{
board[i] = (int *)malloc(8 * sizeof(int));
for (int j = 0; j < 8; j++)
{
board[i][j] = -1;
}
}
// board initiallized
int si, sj;
scanf("%d %d", &si, &sj);
board[si][sj] = 0;
count++;
_Bool c = solve(board, si, sj);
printf("%d",c);
return 0;
}
I need help with code for sorting matrix by sum of columns.
On this site there is already a similar program with sorting by sum of rows.I copied that code and tried to adapt it for columns and it doesn't work.
And one more question:I don't understand the operation in function sum.
If someone could help me with this.
Thank you.
#include<stdio.h>
int sum(int *row, int size) {
int s = 0;
for (size--; size >= 0; size--) s += row[size];
return s;
}
int main() {
int matrix[3][4] = {{1, 2, 3, 4},
{9, 10, 11, 12},
{5, 6, 7, 8}};
int tmp;
printf("Before:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Bubble sort.
for (int i = 0; i < 2; i++) {
for (int j = i + 1; j < 3; j++) {
// Comparing row value.
if (sum(matrix[i], 4) > sum(matrix[j], 4)) {
// Swapping.
for (int k = 0; k < 4; k++) {
tmp = matrix[i][k];
matrix[i][k] = matrix[j][k];
matrix[j][k] = tmp;
}
}
}
}
printf("After:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Here we are given a martix and we have to find out whether the matrix is symmetrix or not. I want to optimise it using array pointer or passsing by reference or you can suggest better approach , which uses multiple concepts, and please provide an explanation , it would be helpful.
I am learning arrays so, that why I am asking you to use multiple concepts , I want to see if there is a better approach
#include <stdio.h>
#include <stdlib.h>
void swap(int arr[3][3], int i, int j)
{
int temp;
temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
void check(int arr[3][3], int i, int j)
{
static int count = 0;
if (arr[i][j] == arr[j][i])
{
count++;
if (count == 9)
{
printf("matrix is symmetric");
}
}
}
int main()
{
int arr[3][3] = {1, 3, 3,
3, 1, 5,
3, 5, 5};
int i, j;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 3; j++)
{
swap(arr, i, j);
}
printf("\n");
}
printf("THE TRANSPOSE MATRIX IS \n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
check(arr, i, j);
if (arr[i][j] != arr[j][i])
{
printf("matrix is non-symmetric");
exit(0);
}
}
}
}
To find symmetric matrix simply use the following code:
#include <stdio.h>
#define dim 4
int main()
{
int i=0;
int arr[dim][dim]= {1, 3, 2, 3,
3, 1, 5, 4,
2, 5, 5, 16,
3, 4, 16, 7};
int isSymmetric = 1;
while (isSymmetric && i<dim)
{
for (int j = i; j < dim; j++)
{
if(arr[i][j]!=arr[j][i]) {
isSymmetric = 0;
break;
}
}
i++;
}
isSymmetric==1?printf("Symmetric"):printf("notSymmetric");
return 0;
}
The complexity of this algorithm at most is O(N/2) when N is the number of elements.
For example an array: {1, 2, 3}
How do I make a program that loops this array to get an output of {1, -1, 2, -2, 3, -3}?
I tried doing something like:
#include <stdio.h>
int main()
{
int n = 3;
int arr[3] = {1, 2, 3};
int result[6];
int i, j, k;
for(i = 0; i < n*2; i++)
{
for(j = 0; j < n; j++)
{
for(k = 0; k < 2; k++)
{
if((i+1) % 2 != 0)
{
result[i] = arr[j];
}
else if((i+1) % 2 == 0)
{
result[i] = -arr[j];
}
}
}
}
for(i = 0; i < n*2; i++)
{
printf("%d ", result[i]);
}
}
But it only outputs {3, -3, 3, -3, 3, -3}
I thought my logic was perfect xD . Can anyone help?
Simple implementation: just store positive and negative values.
#include <stdio.h>
int main(void)
{
int n = 3;
int arr[3] = {1, 2, 3};
int result[6];
int i;
for (i = 0; i < n; i++)
{
result[i * 2] = arr[i];
result[i * 2 + 1] = -arr[i];
}
for(i = 0; i < n * 2; i++)
{
printf("%d\n", result[i]);
}
return 0;
}
This block seems to work:
int src[] = { 1, 2, 3};
int dest[(sizeof(src) / sizeof(int)) * 2];
int destCount = 0;
for (int i = 0; i < sizeof(src) / sizeof(int); i++) {
dest[destCount++] = abs(src[i]);
dest[destCount++] = -(abs(src[i]));
}
for (int i = 0; i < (sizeof(src) / sizeof(int)) * 2; i++) {
printf("%d\n", dest[i]);
}
I put in abs() calls to make sure you get a positive number followed by a negative number. If this isn't what you want, then just get rid of them.
Or if you just want output you could do this:
#include <stdio.h>
int main(void)
{
int n = 3;
int arr[3] = {1, 2, 3};
int i;
for (i = 0; i < n; i++)
{
printf("%d ",arr[i]);
printf("%d ",-1*arr[i]);
}
return 0;
}
I'm trying to implement counting sort as given the CLRS book. My code looks like this:
#include <stdio.h>
#include <stdlib.h>
void counting_sort(int numbers[], int k, int len)
{
int *temp = malloc(k*sizeof(int));
int *res = malloc(len*sizeof(int));
int i;
for(i = 0; i < k; i++)
{
temp[i] = 0;
}
for(i = 0; i < len; i++)
{
temp[numbers[i]]++;
}
for(i = 1; i < k; i++)
{
temp[i] += temp[i-1];
}
int j;
for(i = len-1; i >= 0; i--)
{
res[temp[numbers[i]]] = numbers[i];
temp[numbers[i]]--;
}
for(j = 0; j < len; j++)
printf("%d ", res[j]);
printf("\n");
free(temp);
free(res);
}
void main()
{
int numbers[] = {2, 5, 3, 0, 2, 3, 0, 3};
counting_sort(numbers, 6, 8);
}
But the output I get is: 0 0 0 2 2 3 3 3. The output is almost correct, I should get 0 0 2 2 3 3 3 5.
Any idea what is wrong? I have a feeling it's the last loop that's messing it up.
This line is wrong:
res[temp[numbers[i]]] = numbers[i];
changed it to this:
res[temp[numbers[i]]-1] = numbers[i];
It seem to work fine.