I tried making it but, i couldn't understand where do I put the last if statement to be shown in the flowchart.
int j,i;
for (i = 2; i <= 50; i++) {
for (j = 2; j <= i; j++) {
if(i%j==0) {
break;
}
}
if (i==j) {
System.out.println(j);
}
}
Related
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;
}
first time using the site. I am trying to figure out how to solve a 'maze' using the shortest path. The code works for traditional mazes but the path I am trying to work on is essentially more open. When run the current path goes right, then down, then left and goes up then turns right before finally reaching B. My solution needs to go right then up then left to B. Any help would be appreciated!
9,11
xxxxxxxxxxx
x......B..x
x...xxxx..x
x...xxxx..x
x....A....x
x..xx.xx..x
x.........x
x.........x
xxxxxxxxxxx
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
char** maze;
int** checked;
int rows;
int cols;
int start_row;
int start_col;
enum area
{
space,
wall,
end,
trail,
};
void alloc_maze() {
maze = malloc(rows * sizeof(char*));
for (int i = 0; i < rows; i++) {
maze[i] = malloc(cols * sizeof(char*));
}
}
void alloc_checked() {
checked = malloc(rows * sizeof(char*));
for (int i = 0; i < rows; i++) {
checked[i] = malloc(cols * sizeof(char*));
}
}
void get_maze(const char* file_name)
{
char c;
char rows_t[3] = { '\0' };
char cols_t[3] = { '\0' };
int rows_i = 0;
int cols_i = 0;
int swap = 0;
FILE* file = fopen(file_name, "r");
if (file) {
while ((c = getc(file)) != EOF) {
if (c == '\n') {
break;
} else if (c ==',')
{
swap = 1;
}
else if (!swap) {
rows_t[rows_i] = c;
rows_i++;
}
else {
cols_t[cols_i] = c;
cols_i++;
}
}
}
rows = atoi(rows_t);
cols = atoi(cols_t);
alloc_maze();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
c = getc(file);
if (c == '\n') {
c = getc(file);
}
maze[i][j] = c;
if (c == 'A') {
start_row = i;
start_col = j;
}
}
}
fclose(file);
}
void get_checked() {
alloc_checked();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (maze[i][j] == 'x') {
checked[i][j] = wall;
}
else if (maze[i][j] == 'B') {
checked[i][j] = end;
}
else {
checked[i][j] = space;
}
}
}
}
int search(int row, int col) {
int* current = &checked[row][col];
if (*current == end) {
printf("\n congrats you found the shortest path is");
return 1;
}
if (*current == space) {
*current = wall;
if (search(row, col + 1)) {
*current = trail;
printf("E");
return 1;
}
if (search(row + 1, col)) {
*current = trail;
printf("N");
return 1;
}
if (search(row - 1, col)) {
*current = trail;
printf("S");
return 1;
}
if (search(row, col - 1)) {
*current = trail;
printf("W");
return 1;
}
}
return 0;
}
void add_trail() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (maze[i][j] != 'A'){
if (checked[i][j] == trail) {
maze[i][j] = 'O';
}
}
}
}
}
void print_checked() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", checked[i][j]);
}
printf("\n");
}
printf("\n");
}
void print_maze() {
printf("\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%c", maze[i][j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
get_maze("quickest_route_4.txt");
get_checked();
print_maze();
search(start_row, start_col);
add_trail();
print_maze();
return 0;
}
You have implemented a DFS - Depth First Search maze searching algorithm. This means that your searching algorithm is going to explores as far as possible along a direction before trying another one.
In your code this means that it will always try all of the options going right and then all of the options that are going down and this causes your code to not find the shortest path but just finding a path.
If you do want to find the shortest path you should implement a BFS - Breadth First Search algorithm, it will find you the shortest path since it is progressing the search in all of the active nodes at the same time. It will be a bit harder to implement though since it uses a queue data structure.
Good luck
Also notice that the path you are printing is coming out in reverse order.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I was attempting a problem to check symmetry in logos (link : https://www.hackerearth.com/practice/data-structures/arrays/multi-dimensional/practice-problems/algorithm/roy-and-symmetric-logos-1/description/). This is the function accepting input of the array called logo[][]:
void take_Logo(int logo[N_Max][N_Max], int N) //N is number of rows and columns
{
int i = 0, j = 0;
char in[80];
for(i = 0; i<N; i++)
{
for(j = 0; j<N; j++)
{
//gets(in); //error in input here
scanf("%s", in);
logo[i][j] = in[j] - '0';
}
}
}
The trouble is that when I run the code, for N = 2, it takes 4 inputs:
2
01
01
01
01
How do I correct it?
I am entering the 01's as a binary string.
Here is the complete code:
#include<stdio.h>
#define N_Max 32
int check_Symm(int logo[N_Max][N_Max], int N);
void take_Logo(int logo[N_Max][N_Max], int N);
int main(void)
{
int i = 0, T, N, logo[N_Max][N_Max];
scanf("%d", &T);
i = T;
while(i > 0)
{
--i;
scanf("%d", &N);
take_Logo(logo,N);
//check_Symm(logo, N) == 0 ? printf("NO\n") : printf("YES\n");
for(int k = 0; k<N; k++)
{
for(int l = 0; l<N; l++)
{
printf("%d ", logo[k][l]);
}
printf("\n");
}
}
}
void take_Logo(int logo[N_Max][N_Max], int N)
{
int i = 0, j = 0;
char in[80];
for(i = 0; i<N; i++)
{
for(j = 0; j<N; j++)
{
//gets(in); //error in input here
scanf("%s", in);
logo[i][j] = in[j] - '0';
}
}
}
int check_Symm(int logo[N_Max][N_Max], int N)
{
int test = 1;
if(N % 2 == 0)
{
for(int i = 0; i < N/2; i++)
{
for(int j = 0; j<N; j++)
{
if(logo[i][j] == logo[N-1-i][j])
continue;
else
{
test = 0;
return test;
}
}
}
for(int i = 0; i < N/2; i++)
{
for(int j = 0; j<N; j++)
{
if(logo[j][i] == logo[j][N-1-i])
continue;
else
{
test = 0;
return test;
}
}
}
}
else
{
for(int i = 0; i < N/2; i++)
{
for(int j = 0; j<N; j++)
{
if(logo[i][j] == logo[N-1-i][j])
continue;
else
{
test = 0;
return test;
}
}
}
for(int i = 0; i < N/2; i++)
{
for(int j = 0; j<N; j++)
{
if(logo[j][i] == logo[j][N-1-i])
continue;
else
{
test = 0;
return test;
}
}
}
for(int i=0; i < N/2 ; i++)
{
if(logo[N/2][i] == logo[N/2][N-i-1])
continue;
else
{
test = 0;
return test;
}
}
for(int i=0; i < N/2 ; i++)
{
if(logo[i][N/2] == logo[N-i-1][N/2])
continue;
else
{
test = 0;
return test;
}
}
}
return test;
}
The trouble is that when I run the code, for N = 2, it takes 4 inputs:
This is expected for N == 2:
// Do loop twice
for(i = 0; i<N; i++) {
// Do loop twice
for(j = 0; j<N; j++) {
// Read 2 * 2 times
scanf("%s", in);
logo[i][j] = in[j] - '0';
}
}
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;
}
}
}
I have this question? How to make histogram equalization on image in C? I wrote this code, but I don't get the correct result.
void histogram(unsigned char image_in [64][64],unsigned char image_out [64][64], unsigned long hist[256],unsigned long eHist[256],float cdf[256]) {
#define lines 64
#define columns 64
int i,j;
int pixels = lines*columns;
// original histogram
for (i = 1; i < 256; i++) {
hist[i]=0;
}
for (i = 0; i < lines; i++) {
for (j = 0; j < columns; j++) {
hist[image_in[i][j]]++;
}
}
// Cumulative Distribution Function
float cdfmax=256, cdfmin=1;
for (i = 1; i < 256; i++) {
cdf[i] = 0;
for (i = 1; i < 256; i++) {
cdf[i] += hist[i];
}
}
// Equalized Histogram
for (i = 1; i < 256; i++) {
eHist[i] = ((cdf[i]-cdfmin)/((lines*columns)-cdfmin))*255;
}
// Final Image
for (i = 0; i < lines; i++) {
for (j = 0; j < columns; j++) {
image_out[i][j] = cdf[image_in[i][j]]*255;
}
}
}
That is my main function:
void main(void) {
FILE *fp;
fp = fopen("../lena_eye.raw","rb");
int i,j;
for (i = 0; i < 64; i++) {
for (j = 0; j < 64; j++) {
image_in[i][j] = getc(fp);
}
}
fclose(fp);
histogram(image_in, image_out, hist, eHist,cdf);
}
Тhe results I received can be seen in the images.
[1]: https://i.stack.imgur.com/zjhp9.png -image_in
[2]: https://i.stack.imgur.com/itDNE.png -Hist
[3]: https://i.stack.imgur.com/74Ulm.png -eHist
[4]: https://i.stack.imgur.com/qFwUw.png -image_out
This bit of code is wrong:
for (i = 1; i < 256; i++) {
cdf[i] = 0;
for (i = 1; i < 256; i++) {
cdf[i] += hist[i];
}
}
The inner loop changes the value of i, messing up the outer loop. What you wanted to write is something like this:
for (i = 0; i < 256; i++) {
cdf[i] = 0;
for (j = 0; j <= i; j++) {
cdf[i] += hist[j];
}
}
...but it would be simpler to write:
cdf[0] = hist[0];
for (i = i; i < 256; i++) {
cdf[i] = cdf[i-1] + hist[i];
}
Also, when you compute the histogram and the equalized histogram, you have loops starting at 1, they should start at 0.
I found my mistake in the code and I share what I wrote.
void Histogramm(unsigned char *image_in, unsigned char *image_out)
{
int i, j;
const unsigned long pixels = lines * columns;
unsigned long cdf_min;
const unsigned char* limit = image_in + pixels;
unsigned char* img;
// original histogram
for (i = 0; i < 256; i++)
{
hist[i] = 0;
}
for (img = image_in; img < limit; img++)
{
hist[*img]++;
}
// Cumulative Distribution Function
unsigned long count = 0;
for (i = 0; i < 256; i++)
{
count += hist[i];
cdf[i] = count;
}
for (i = 0; i < 256; i++)
{
if (cdf[i] != 0)
{
cdf_min = cdf[i];
break;
}
}
// Equalized Histogram
//eHist[i]=round((cdf[i]-cdf_min)/(pixels-cdf_min)*255)=round(k*(cdf[i]-cdf_min))
const double k = 255.0 / (pixels - cdf_min);
for (i = 0; i < 256; i++)
{
eHist[i] = (unsigned long)(k*(cdf[i] - cdf_min));
}
// Final Image
for (img = image_in; img < limit; img++)
{
*image_out = eHist[*img];
image_out++;
}
}