C- Insertion sort - c

What's wrong with this code? while loop part doesn't work. if statement is not enough to terminate while loop
int main(void) {
int data[SIZE] = { 12,2,5,8,2,45,89,0,12,4 };
int i, j, tmp;
for (i = 0; i < SIZE-1; i++) {
j = i + 1;
tmp = data[j];
while (j > 0){
if(data[j-1] > tmp){
data[j] = data[j - 1];
j--;
}
}
data[j] = tmp;
}
for (i = 0; i < SIZE; i++)
printf("%d, ", data[i]);
return 0;
}

j--; is inside your if statement. If data[j-1] > tmp is false your loop will not terminate. Apart from this your algorithm does not sort the array. Adapt your code like this:
int main (void)
{
int data[SIZE] = { 12,2,5,8,2,45,89,0,12,4 };
int i, j, tmp;
for (i = 0 ; i < SIZE - 1; i++)
{
j = i+1;
while ( j > 0 && data[j] < data[j-1])
{
tmp = data[j];
data[j] = data[j-1];
data[j-1] = tmp;
j--;
}
}
for (i = 0; i < SIZE; i++) {
printf("%d, ", data[i]);
}
return 0;
}

Related

Why when I try to run this code online it always generates runtime error/run error. but when I run it in command prompt it works [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 yesterday.
Improve this question
This code is a program that will move the index position of several names according to the size of the toga that each participant has, and according to the name that the user wants to change.
#include <stdio.h>
#include <string.h>
typedef struct
{
char nama[100];
char ukuran[100];
} toga;
int main()
{
int n, m;
scanf("%d%d", &n, &m);
toga matriks[n][m];
int jumlah[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
scanf("%s%s", &matriks[i][j].nama, &matriks[i][j].ukuran);
if(strcmp(matriks[i][j].ukuran, "XS") == 0)
{
jumlah[i][j] = 32;
}
else if(strcmp(matriks[i][j].ukuran, "S") == 0)
{
jumlah[i][j] = 36;
}
else if(strcmp(matriks[i][j].ukuran, "M") == 0)
{
jumlah[i][j] = 37;
}
else if(strcmp(matriks[i][j].ukuran, "L") == 0)
{
jumlah[i][j] = 38;
}
else if(strcmp(matriks[i][j].ukuran, "XL") == 0)
{
jumlah[i][j] = 40;
}
else if(strcmp(matriks[i][j].ukuran, "XXL") == 0)
{
jumlah[i][j] = 42;
}
else if(strcmp(matriks[i][j].ukuran, "XXXL") == 0)
{
jumlah[i][j] = 45;
}
}
}
maybe this part is the problem?
toga ditukar1[50][50];
toga ditukar2[50][50];
int cek2 = 0;
for (int i = 0; i < 50; i++)
{
for(int j = 0; j < 1; j++)
{
scanf("%s", &ditukar1[i][0].nama);
if(strcmp(ditukar1[i][0].nama, "Selesai") == 0)
{
j = 1;
i = 50;
}
else
{
scanf("%s ", &ditukar1[i][1].nama);
// cek1++;
}
}
cek2++;
}
cek2 -= 1;
// printf("ini cek1 : %d\n", cek1);
// printf("ini cek2 : %d\n", cek2);
toga matriks_baru[m][n];
for (int i = 0, a = 0; i < m && a < m; i++, a++)
{
for (int j = 0, b = 0; j < n && b < n; j++, b++)
{
strcpy(matriks_baru[a][b].nama ,matriks[j][i].nama);
}
}
int max_len[m];
max_len[0] = 0;
int terpanjang[m];
int a = 0;
for (int i = 0; i < m; i++)
{
max_len[a] = 0;
for (int j = 0; j < n; j++)
{
if(strlen(matriks_baru[i][j].nama) > max_len[a])
{
max_len[a] = strlen(matriks_baru[i][j].nama);
}
}
a++;
}
printf("Posisi awal :\n");
for(int i = 0; i < n; i++)
{
a = 0;
for(int j = 0; j < m; j++)
{
if(j != m-1)
{
printf("%s ", matriks[i][j].nama);
}
else
{
printf("%s", matriks[i][j].nama);
}
if(j != m-1)
{
for(int k = 0; k < (max_len[a]) - (strlen(matriks[i][j].nama)); k++)
{
printf(" ");
}
}
a++;
}
printf("\n");
}
printf("\n");
int baris1, baris2;
int kolom1, kolom2;
int k = 0;
int l = 0;
maybe this part is the problem?
here I will check whether the name entered by the user whose index you want to exchange is in the matrix
for(int l = 0; l < cek2; l++)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
for(int a = 0; a < n; a++)
{
for(int b = 0; b < m; b++)
{
if(strcmp(ditukar1[l][0].nama, matriks[i][j].nama) == 0)
{
baris1 = i;
kolom1 = j;
if(strcmp(ditukar1[l][1].nama, matriks[a][b].nama) == 0)
{
// printf("%d\n", l);
baris2 = a;
kolom2 = b;
if(baris1 < baris2)
{
char temp1[] = "0";
int temp2 = 0;
if(jumlah[baris1][kolom1] > jumlah[baris2][kolom2])
{
temp2 = jumlah[baris1][kolom1];
jumlah[baris1][kolom1] = jumlah[baris2][kolom2];
jumlah[baris2][kolom2] = temp2;
strcpy(temp1, matriks[baris2][kolom2].nama);
strcpy(matriks[baris2][kolom2].nama, matriks[baris1][kolom1].nama);
strcpy(matriks[baris1][kolom1].nama, temp1);
}
}
else if(baris1 > baris2)
{
char temp1[] = "0";
int temp2 = 0;
if(jumlah[baris2][kolom2] > jumlah[baris1][kolom1])
{
temp2 = jumlah[baris2][kolom2];
jumlah[baris2][kolom2] = jumlah[baris1][kolom1];
jumlah[baris1][kolom1] = temp2;
strcpy(temp1, matriks[baris1][kolom1].nama);
strcpy(matriks[baris1][kolom1].nama, matriks[baris2][kolom2].nama);
strcpy(matriks[baris2][kolom2].nama, temp1);
}
}
}
}
}
}
}
}
}
for (int i = 0, a= 0; i < m && a < m; i++, a++)
{
for (int j = 0, b = 0; j < n && b < n; j++, b++)
{
strcpy(matriks_baru[a][b].nama ,matriks[j][i].nama);
}
}
int max_len2[m];
max_len2[0] = 0;
int s = 0;
for (int i = 0; i < m; i++)
{
max_len2[s] = 0;
for (int j = 0; j < n; j++)
{
if(strlen(matriks_baru[i][j].nama) > max_len2[s])
{
max_len2[s] = strlen(matriks_baru[i][j].nama);
}
}
s++;
}
printf("Posisi akhir :\n");
for(int i = 0; i < n; i++)
{
s = 0;
for(int j = 0; j < m; j++)
{
if(j != m-1)
{
printf("%s ", matriks[i][j].nama);
}
else
{
printf("%s", matriks[i][j].nama);
}
if(j != m-1)
{
for(int k = 0; k < (max_len2[s]) - (strlen(matriks[i][j].nama)); k++)
{
printf(" ");
}
}
s++;
}printf("\n");
}
return 0;
}

Sort a matrix row wise (c programming)

I wrote this code for sorting an nxn matrix: odd rows in descending order, double rows in ascending order, but it doesn't pass the compiler stage.
What did I do wrong?
It mostly tells me this: assignment to 'int' from 'int *' makes integer from pointer without a cast (how can I solve this problem)?
#include <stdio.h>
#include <stdlib.h>
int comp_a(int a, int b) {
if (a < b) {
return 1;
} else {
return 0;
}
}
int comp_d(int a, int b) {
if (a > b) {
return 1;
} else {
return 0;
}
}
void sort(int a[], int n, int (*comp)(int, int)) {
int t;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (comp(a[j], a[j + 1]) == 0) {
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
}
int main() {
int n;
printf("Enter the dimension of matrix(n): ");
scanf("%d", &n);
int *mat = (int*)calloc(n, sizeof(int));
for (int i = 0; i < n; i++) {
mat[i] = (int*)calloc(n, sizeof(int));
}
for (int i = 0; i < n; i++) {
printf("Enter row [%d]: ", i);
for (int j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
for (int i = 0; i < n; i++) {
if (i%2 == 0) {
sort(mat[i], n, &comp_a);
} else {
sort(mat[i], n, &comp_d);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
You should change your matrix allocation to this:
int **mat = (int**)calloc(n, sizeof(int*)); // (1)
for(int i = 0; i < n; i++) {
mat[i] = (int*)calloc(n, sizeof(int)); // (2)
}
(1): Declaring an array of pointers of size n x int*.
(2): Where each pointer in this array points to an array of integers of size n x int.
I changed your matrix allocation
int *mat = (int*)calloc(n, sizeof(int));
for(int i = 0; i < n; i++) {
mat[i] = (int*)calloc(n, sizeof(int));
}
to
int mat[n][n];
I changed for loop of ordred
for(int i = 0; i < n; i++) {
for(int j = 0; j < n - 1; j++) {
if(comp(a[j], a[j + 1]) == 0)
because he take a lot of time (time of running) to
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(comp(a[i], a[j]) == 0)
my code:
#include <stdio.h>
#include <stdlib.h>
int comp_a(int a, int b) {
if(a < b) {
return 1;
}
else {
return 0;
}
}
int comp_d(int a, int b) {
if(a > b) {
return 1;
}
else {
return 0;
}
}
void sort(int a[], int n, int (*comp)(int, int)) {
int t;
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(comp(a[i], a[j]) == 0) {
t = a[j];
a[j] = a[i];
a[i] = t;
}
}
}
}
int main() {
int n;
do
{
printf("Enter the dimension of matrix(n): ");
scanf("%d", &n);
}while(n<1);
int mat[n][n];
for(int i = 0; i < n; i++) {
printf("Enter row [%d]: ", i);
for(int j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
for(int i = 0; i < n; i++) {
if(i%2 == 0) {
sort(mat[i], n, &comp_a);
}
else {
sort(mat[i], n, &comp_d);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}

How to make the program see the new matrix I created?

I want to realize the matrix multiplication using Strassen method. Since my input matrix will not be 2^n *2^n all the time, for example, it could be 3*3 matrix, I need to fill the edge area with 0. But I came across a problem when I create new matrix A_new[][] for A[][], B_new[][] for B[][]. I can successfully generate the A_new[][] and B_new[][] for both square(2*2, etc.) or (3*3,etc.). But when I want to further use A_new[][] and B_new[][], which I created before, the code outside the "if" statement couldn't see it.
Is there some ways that I could make the code which is outside the "if" statement see it and process it?
Here are my code.
void strassen(int A[][N], int B[][N], int C[][N]) {
if (N == 1) {
C[0][0] = A[0][0] * B[0][0];
}
if (N > 1 && N % 2 != 0) {
int A_new[N + 1][N + 1];
int B_new[N + 1][N + 1];
int i, j;
for (i = 0; i < N + 1; i++) {
if (i == N) {
for (j = 0; j < N + 1; j++) {
A_new[i][j] = 0;
B_new[i][j] = 0;
}
} else {
for (j = 0; j < N + 1; j++) {
if (j == N) {
A_new[i][j] = 0;
B_new[i][j] = 0;
} else {
A_new[i][j] = A[i][j];
B_new[i][j] = B[i][j];
}
}
}
}
for (i = 0; i < N + 1; i++) {
for (j = 0; j < N + 1; j++) {
C[i][j] = 0;
}
}
} else {
int i, j;
int A_new[N][N];
int B_new[N][N];
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
A_new[i][j] = A[i][j];
}
}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
B_new[i][j] = B[i][j];
}
}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
C[i][j] = 0;
}
}
}
if you move int A_new[N + 1][N + 1]; and int B_new[N + 1][N + 1]; before the if you can use A and B in the two branches of the if without problem, and they are available after the if too of course. You do not use all the cells in the else branch but that has no consequences
int A_new[N + 1][N + 1];
int B_new[N + 1][N + 1];
if (N > 1 && N % 2 != 0) {
int i, j;
for (i = 0; i < N + 1; i++) {
if (i == N) {
for (j = 0; j < N + 1; j++) {
A_new[i][j] = 0;
B_new[i][j] = 0;
}
} else {
for (j = 0; j < N + 1; j++) {
if (j == N) {
A_new[i][j] = 0;
B_new[i][j] = 0;
} else {
A_new[i][j] = A[i][j];
B_new[i][j] = B[i][j];
}
}
}
}
for (i = 0; i < N + 1; i++) {
for (j = 0; j < N + 1; j++) {
C[i][j] = 0;
}
}
} else {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
A_new[i][j] = A[i][j];
}
}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
B_new[i][j] = B[i][j];
}
}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
C[i][j] = 0;
}
}
}
As chux says in a remark you missed an else (or you just return if N == 1 ?) and the code can be
int A_new[N + 1][N + 1];
int B_new[N + 1][N + 1];
if (N == 1) {
C[0][0] = A[0][0] * B[0][0];
}
else if (N > 1 && N % 2 != 0) {
int i, j;
for (i = 0; i < N + 1; i++) {
if (i == N) {
for (j = 0; j < N + 1; j++) {
A_new[i][j] = 0;
B_new[i][j] = 0;
}
} else {
for (j = 0; j < N + 1; j++) {
if (j == N) {
A_new[i][j] = 0;
B_new[i][j] = 0;
} else {
A_new[i][j] = A[i][j];
B_new[i][j] = B[i][j];
}
}
}
}
for (i = 0; i < N + 1; i++) {
for (j = 0; j < N + 1; j++) {
C[i][j] = 0;
}
}
} else {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
A_new[i][j] = A[i][j];
}
}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
B_new[i][j] = B[i][j];
}
}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
C[i][j] = 0;
}
}
}

Comparing sorting methods in C

I have to compare these 3 sorting functions (bubble, insertion, and selection), in their worst, best, and the random possible way to sort an array (when talking about the idea of numbers of comparison).
I built and ran the program. The compiler is working, everything seems fine. The problem is that after the cmd window appears, nothing happens. The program is not returning anything (neither 0 (because of return 0) or writing sth in the file). How can I fix it?
#include <stdio.h>
#include <stdlib.h>
int b[11000];
int w[11000];
int r[11000];
int n;
int comp;
int attr;
FILE*f;
//CREAREA SIRURILOR
//best, worst, random
void sir(int n)
{ int i;
for(i=0; i<n; i++)
{
b[i] = i;
r[i] = rand() % n;
w[i] = n-i-1;
}
}
//INSERTION SORT
void insertion (int a[1000])
{
int j, i, index;
comp = 0;
attr = 0;
for (i = 0; i < n; i++)
{
index = a[i];
j = i;
attr+=1;
while ((j > 0) && (a[j-1] > index))
{ comp++;
a[j] = a[j-1];
j = j-1;
attr+=1;
}
while ((j > 0) || (a[j-1] > index))
{ comp++;
}
a[j] = index;
attr++;
}
fprintf(f,"\t%d\t", comp);
fprintf(f,"\t%d\t", attr);
fprintf(f,"\t%d\t", attr+comp);
}
//Selection Sort
void selection (int a[1000])
{
int j, i, min, temp;
comp = 0;
attr = 0;
for (i = 0; i < n-1; i++)
{
min = i;
for (j = i+1; j < n; j++){
comp++;
if (a[j] < a[min])
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
attr+=3;
}
fprintf(f,"\t%d\t", comp);
fprintf(f,"\t%d\t", attr);
fprintf(f,"\t%d\t", attr+comp);
}
//Bubble Sort
void bubblesort (int array[2000])
{
int sorted;
int swap,d;
int c;
comp = 0;
attr = 0;
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0; d < n - c - 1; d++)
{
comp++;
if (array[d] > array[d + 1])
{
swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
attr += 3;
}
}
}
fprintf(f,"\t %d\t", comp);
fprintf(f,"\t %d\t", attr);
fprintf(f,"\t%d\t", attr+comp);
}
void InsertionSort()
{
insertion(b);
insertion(r);
insertion(w);
}
void SelectionSort()
{
selection(b);
selection(r);
selection(w);
}
void BubbleSort()
{
bubblesort(b);
bubblesort(r);
bubblesort(w);
}
int main ()
{
f = fopen("iesire.txt","w+");
if(f == NULL)
{
printf("Error!");
exit(-1);
}
fprintf(f,"\n Bubble Sort\t\n");
fprintf(f,"\tBest\t\t\t\t\t\tAverage\t\t\t\t\t\tWorst\t\t\n");
fprintf(f,"\tComparatii\t\tAtribuiri\t\tComp+Atr\t\tComparatii\t\tAtribuiri\t\tComp+Atr\t\tComparatii\t\tAtribuiri\t\tComp+Atr\n");
for (n = 100; n<=10000; n+=500)
{
sir(n);
BubbleSort();
fprintf(f,"\n");
}
fprintf(f,"\n\n Selection Sort\t\n");
fprintf(f,"\tBest\t\t\t\t\t\tAverage\t\t\t\t\t\tWorst\t\t\n");
fprintf(f,"\tComparatii\t\tAtribuiri\t\tComp+Atr\t\tComparatii\t\tAtribuiri\t\tComp+Atr\t\tComparatii\t\tAtribuiri\t\tComp+Atr\n");
for (n = 100; n<=10000; n+=500)
{
sir(n);
InsertionSort();
fprintf(f,"\n");
}
fprintf(f,"\n\n Insertion Sort\t\n");
fprintf(f,"\tBest\t\t\t\t\t\tAverage\t\t\t\t\t\tWorst\t\t\n");
fprintf(f,"\tComparatii\t\tAtribuiri\t\tComp+Atr\t\tComparatii\t\tAtribuiri\t\tComp+Atr\t\tComparatii\t\tAtribuiri\t\tComp+Atr\n");
for (n = 100; n<=10000; n+=500)
{
sir(n);
SelectionSort();
fprintf(f,"\n");
}
fclose(f);
printf(" Result: 'iesire.txt'.");
return 0;
}

Trying to find number of possible paths going through all points exactly once starting at the center of a 5x5 array. Diagonal movement is also allowed

#include <iostream>
using namespace std;
long long int acc = 0;
bool isValid(int x, int y, int m[5][5])
{
if(x > -1 && x < 5 && y > -1 && y < 5 && m[x][y] == 0)
{
return true;
}
else
{
return false;
}
}
void start(int x, int y, int m[5][5],int count)
{
if(isValid(x,y,m))
{
count++;
if(count == 25)
{
acc++;
}
else
{
m[x][y] = 1;
start(x+1,y,m,count);
start(x-1,y,m,count);
start(x,y+1,m,count);
start(x,y-1,m,count);
start(x+1,y+1,m,count);
start(x-1,y-1,m,count);
start(x+1,y-1,m,count);
start(x-1,y+1,m,count);
}
}
}
int main()
{
int map[5][5];
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
map[i][j] = 0;
}
}
start(2,2,map,0);
cout<<acc;
}
or another code which i tried
#include <iostream>
#include <vector>
using namespace std;
long long int acc = 0;
class Point
{
public:
int row;
int column;
int count;
int map[5][5];
};
vector<Point> pts;
bool isValid(Point *b)
{
if(b->column > -1 && b->column <5 && b->row > -1 && b->row < 5 && b->map[b->column][b->row] != 1)
{
return true;
}
else
{
return false;
}
}
void start(vector<Point> A)
{
if(A.size() == 0)
{
cout<<acc;
}
Point *temp = &A.back();
A.pop_back();
if(isValid(temp))
{
temp->map[temp->column][temp->row] = 1;
temp->count = temp->count + 1;
if(temp->count == 25)
{
acc++;
}
else
{
Point *p = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
p->map[i][j] = temp->map[i][j];
}
}
p->column = temp->column + 1;
p->row = temp->row;
p->count = temp->count;
A.push_back(*p);
Point *q = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
q->map[i][j] = temp->map[i][j];
}
}
q->column = temp->column - 1;
q->row = temp->row;
q->count = temp->count;
A.push_back(*q);
Point *r = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
r->map[i][j] = temp->map[i][j];
}
}
r->column = temp->column;
r->row = temp->row + 1;
r->count = temp->count;
A.push_back(*r);
Point *s = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
s->map[i][j] = temp->map[i][j];
}
}
s->column = temp->column;
s->row = temp->row - 1;
s->count = temp->count;
A.push_back(*s);
Point *t = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
t->map[i][j] = temp->map[i][j];
}
}
t->column = temp->column + 1;
t->row = temp->row + 1;
t->count = temp->count;
A.push_back(*t);
Point *u = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
u->map[i][j] = temp->map[i][j];
}
}
u->column = temp->column - 1;
u->row = temp->row - 1;
u->count = temp->count;
A.push_back(*u);
Point *v = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
v->map[i][j] = temp->map[i][j];
}
}
v->column = temp->column + 1;
v->row = temp->row - 1;
v->count = temp->count;
A.push_back(*v);
Point *w = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
w->map[i][j] = temp->map[i][j];
}
}
w->column = temp->column - 1;
w->row = temp->row + 1;
w->count = temp->count;
A.push_back(*w);
}
}
cout<<A.size();
start(A);
}
int main()
{
pts.clear();
Point *p = new Point;
p->map[2][2] = 0;
p->column = 2;
p->row = 2;
p->count = 0;
cout<<p<<endl;
pts.push_back(*p);
start(pts);
}
the first runs for about 150 iterations and then outputs 0, to indicate no completepaths, which is definitely wrong.
the second seems an error in pointers and addresses, which i still cannot get my head around.

Resources