If you apply the ijkstra algorithm several times, you can see the effect of the Floyd algorithm. We've created a program that uses the Dijkstra algorithm to get the shortest path from every vertex in the graph to every other vertex in the graph, and we've created a Dijkstra code.The values shown in the image are not shown. What is the reason?
enter code here
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define TRUE 1
#define FALSE 0
#define MAX_VERTICES 100
#define INF 1000000
typedef struct GraphType {
int n;
int weight[MAX_VERTICES][MAX_VERTICES];
} GraphType;
int distance[MAX_VERTICES];
int found[MAX_VERTICES];
int choose(int distance[], int n, int found[])
{
int i, min, minpos;
min = INT_MAX;
minpos = -1;
for (i = 0; i < n; i++)
if (distance[i] < min && !found[i]) {
min = distance[i];
minpos = i;
}
return minpos;
}
void print_status(GraphType* g)
{
static int step = 1;
printf("STEP %d: ", step++);
printf("distance: ");
for (int i = 0; i < g->n; i++) {
if (distance[i] == INF)
printf(" * ");
else
printf("%2d ", distance[i]);
}
printf("\n");
printf(" found: ");
for (int i = 0; i < g->n; i++)
printf("%2d ", found[i]);
printf("\n\n");
}
void shortest_path(GraphType * g, int start)
{
int i, u, w;
for (i = 0; i < g->n; i++)
{
distance[i] = g->weight[start][i];
found[i] = FALSE;
}
found[start] = TRUE;
distance[start] = 0;
for (i = 0; i < g->n - 1; i++) {
print_status(g);
u = choose(distance, g->n, found);
found[u] = TRUE;
for (w = 0; w < g->n; w++)
if (!found[w])
if (distance[u] + g->weight[u][w] < distance[w])
distance[w] = distance[u] + g->weight[u][w];
}
}
int main(void)
{
GraphType g = { 7,
{{ 0, 7, INF, INF, 3, 10, INF },
{ 7, 0, 4, 10, 2, 6, INF },
{ INF, 4, 0, 2, INF, INF, INF },
{ INF, 10, 2, 0, 11, 9, 4 },
{ 3, 2, INF, 11, 0, INF, 5 },
{ 10, 6, INF, 9, INF, 0, INF },
{ INF, INF, INF, 4, 5, INF, 0 } }
};
for(int i=0; i<g.n; i++){
printf("from %d to other nodes>\n", i);
shortest_path(&g, i);
}
return 0;
}
Related
I know C++ has glm::make_mat4() with #include <glm/gtc/type_ptr.hpp>, but there does not seem to be a make_mat4() function available in CGLM.
Can someone please tell me how to create a CGLM Mat4 from a float[16] array?
I tried the following, but the Mat4 does not seem to print the float values I'm expecting.
#include "cglm/cglm.h"
#include "cglm/call.h"
void main() {
// Atempt #1
float aaa[16] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};
mat4 bbb = {
{ aaa[0],aaa[1],aaa[2],aaa[3] },
{ aaa[4],aaa[5],aaa[6],aaa[7] },
{ aaa[8],aaa[9],aaa[10],aaa[11] },
{ aaa[12],aaa[13],aaa[14],aaa[15] }
};
for (int i = 0; i < 4; i++) {
for (int k = 0; k < 4; k++) {
printf("%d", bbb[i][k]);
printf("\n");
}
}
printf("\n");
// Atempt #2
//void *memcpy(void *dest, const void * src, size_t n)
memcpy(bbb, aaa, sizeof(aaa));
for (int i = 0; i < 4; i++) {
for (int k = 0; k < 4; k++) {
printf("%d", bbb[i][k]);
printf("\n");
}
}
}
You need to make a void main() function as an entry point. Otherwise the compiler wont know where you program starts.
int main(){
float aaa[16] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};
float bbb[4][4] = {
{ aaa[0],aaa[1],aaa[2],aaa[3] },
{ aaa[4],aaa[5],aaa[6],aaa[7] },
{ aaa[8],aaa[9],aaa[10],aaa[11] },
{ aaa[12],aaa[13],aaa[14],aaa[15] }
};
for (int i = 0; i < 4; i++) {
for (int k = 0; k < 4; k++) {
printf("%f", bbb[i][k]);
printf("\n");
}
}
printf("\n");
}
This code works in for me.
The diffrence to your code is:
The main function
I changed the data type from the bbb array to float and used the dimensions 4 by 4
I used %f in the printf function for printing the values
i need to create a power set of array lo[], array lo[] is {5, 6, 7}; it's for my laboratories, I don't know how to do this in general power set is 5, {5, 6} {5, 7} {5, 6, 7}, all combinations and another important thing, power of array. the number of power set combinations is the same as the power. Power of array is 2^(elements of array); 2^3 = 8
should be 8 combination.
output:
!((C/A) U (A/B)): {5, 6, 7}
power set: {5}, {5, 6}, {5, 6, 7}, {6, 7}.....
#include <stdio.h>
#include <math.h>
int main() {
int a[10] = { 1,1,1,1,1,1,1,0,0,0 };
int b[10] = { 0,0,0,0,1,1,1,1,1,1 };
int c[10] = { 1,1,1,0,0,0,0,1,1,1 };
int c1[10];
int c2[10];
int c3[10];
int c4[10];
int temp[10], temp1[10], a2[10] = { 1, 2, 3, 4, 5 };
int k = 0;
for (int i = 0; i < 10; i++) {
c1[i] = c[i];
if (c[i] == a[i]) {
c1[i] = 0;
}
}
for (int i = 0; i < 10; i++) {
c2[i] = a[i];
if (a[i] == b[i]) {
c2[i] = 0;
}
}
for (int i = 0; i < 10; i++) {
if (c1[i] == 1 || c2[i] == 1) {
c3[i] = 1;
}
}
for (int i = 0; i < 10; i++) {
if (c3[i] == 0) {
c4[i] = 1;
}
else
c4[i] = 0;
} //// the main part of power set
int lo[3] = {0, 0, 0};
printf("!((C/A) U (A/B)): ");
for (int i = 0; i < 10; i++) {
if (c4[i] == 1) {
printf("%d ", i + 1);
lo[k] = (i + 1);
}
}
}
I'm writing a program calculating the shortest way from point A to point B.
I have a map (matrix) with values:
0 is block (wall, no way to pass);
1 is free way (you can pass);
2 is start point;
In the code below I declare 2 arrays: an array " map"and changed array "visited" while running program demonstrating visited points.
I check the cells in 4 directions (not diagonals) for 1 or 0. If it's 1 (possible to pass), I increase the counter for 1. For do not count the previous cell I'm trying to avoid it by the condition. I realized that in two one-dimensional arrays {1 0 -1 0} and { 0, 1, 0, -1 } to check neighbor points (what mean i check [i+1][j], [i-1][j], [i][j+1] and [i][j-1]).
As a result I wanna see "visited" matrix with a few lines which shows the way to reach to the point B (1, 2, 3, ... 15). I wanna find the way to map[7][7] point.
Right now the error here that I do count++ for the previous position. How to avoid that?
Thank you.
P.S. I wrote a few functions implementing a new array with 0 values, counting free to go cells and printing arrays.
main.c:
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#define WIDTH 8
#define HEIGHT 8
int mapZero(int map[WIDTH][HEIGHT]);
int mapPrint(int map[WIDTH][HEIGHT]);
int mapInit(int map[WIDTH][WIDTH]);
int findFreeToGoCells(int map[WIDTH][HEIGHT]);
int main(int argc, char * argv[])
{
bool stop;
unsigned int count;
unsigned int max;
int visited[WIDTH][HEIGHT];
int map[WIDTH][HEIGHT] =
{
{ 1, 1, 1, 1, 1, 0, 0, 1 },
{ 0, 1, 1, 1, 1, 1, 0, 1 },
{ 0, 0, 1, 0, 1, 1, 1, 0 },
{ 1, 0, 1, 1, 1, 0, 1, 1 },
{ 0, 0, 0, 1, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 1 },
{ 0, 1, 1, 1, 1, 1, 1, 1 },
};
mapZero(visited);
printf("Matrix of zeroed-visited cells:\n\n");
mapPrint(visited);
printf("Matrix of the map:\n\n");
mapPrint(map);
printf("Free to go cells: %d\n\n", findFreeToGoCells(map));
max = WIDTH * HEIGHT - 1;
visited[0][0] = map[0][0];
count = 0;
visited[0][0] = 0;
int di[4] = { 1, -1, 0, 0 };
int dj[4] = { 0, 0, 1, -1 };
//do
{
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
if (visited[i][j] == count)
{
for (int k = 0; k < 4; ++k)
{
int i_check = i + di[k];
int j_check = j + dj[k];
if ((i_check >= 0 && i_check < WIDTH) && (j_check >= 0 && j_check < HEIGHT) && (map[i_check][j_check] != 0))
{
visited[i_check][j_check] = count + 1;
}
}
count++;
}
}
}
}// while (visited[7][7] == 0);
if (count > max + 99999)
printf("The way couldn't be found\n");
else
{
printf("Matrix of visited cells:\n\n");
mapPrint(visited);
printf("Free to go cells from [0][0] to [7][7]: %d\n", findFreeToGoCells(visited));
}
/*************************************************************************************/
/*************************************************************************************/
int len;
int x = 7;
int y = 7;
int x_path[WIDTH * HEIGHT];
int y_path[WIDTH * HEIGHT];
len = visited[7][7];
count = len;
while (count > 0)
{
x_path[count] = x;
y_path[count] = y;
count--;
for (int k = 0; k < 4; ++k)
{
int i_check = x + di[k];
int j_check = y + dj[k];
if ((i_check >= 0 && i_check < WIDTH) && (j_check >= 0 && j_check < HEIGHT) && (map[i_check][j_check] == count))
{
x = x + di[k];
y = y + dj[k];
break;
}
}
}
x_path[0] = 0;
y_path[0] = 0;
printf("\nThe shortest way consist of %d cells\nThere are %d the shortest way to reach th the final point\n\n", len, findFreeToGoCells(visited)-len);
system("pause");
return 0;
}
int mapZero(int map[WIDTH][HEIGHT])
{
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
map[i][j] = 0;
}
}
return 0;
}
int mapPrint(int map[WIDTH][HEIGHT])
{
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
printf("%2d ", map[i][j]);
}
printf("\n\n");
}
printf("\n");
return 0;
}
int findFreeToGoCells(int map[WIDTH][HEIGHT])
{
int count = 0;
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
if (map[i][j] != 0) count++;
}
}
return count;
}
Result:
Please, help me to find and print all square submatrices of square matrix from big to small square matrices in C programming language
I wrote code that works wrong:
int main() {
int mtrx_size = 8;
int mat[8][8] = {
{ 1, 2, 3, 4, 5, 6, 7, 8},
{ 9,10,11,12,13,14,15,16},
{17,18,19,20,21,22,23,24},
{25,26,27,28,29,30,31,32},
{33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48},
{49,50,51,52,53,54,55,56},
{57,58,59,60,61,62,63,64}
};
int i,j;
int sub_mtrx_size;
for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--)
{
for(i = 0; i < sub_mtrx_size; i++)
{
for(j = 0; j < sub_mtrx_size; j++)
{
printf("%3d ", mat[i][j]);
}
printf("\n");
}
printf("\n");
}
return 0;
Here I need to find all 8x8, 7x7, 6x6, 5x5, 4x4, 3x3 and 2x2 submatrices.
Your code was just printing a single sub-matrix for each size, positioned in the upper-left corner of the matrix. You need to add i and j offsets to get the sub-matrices at all positions:
#include <stdio.h>
int main() {
int mtrx_size = 8;
int mat[8][8] = {
{ 1, 2, 3, 4, 5, 6, 7, 8},
{ 9,10,11,12,13,14,15,16},
{17,18,19,20,21,22,23,24},
{25,26,27,28,29,30,31,32},
{33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48},
{49,50,51,52,53,54,55,56},
{57,58,59,60,61,62,63,64}
};
int i, j, ioff, joff, off_cnt;
int sub_mtrx_size;
for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--) {
off_cnt = mtrx_size - sub_mtrx_size + 1;
for (ioff = 0; ioff < off_cnt; ioff++) {
for (joff = 0; joff < off_cnt; joff++) {
for (i = 0; i < sub_mtrx_size; i++) {
for (j = 0; j < sub_mtrx_size; j++) {
printf("%3d ", mat[i+ioff][j+joff]);
}
printf("\n");
}
printf("\n");
}
}
}
return 0;
}
Java implementation for a general nxm matrix:
private static void printSubMatrix(int[][] mat) {
int rows=mat.length;
int cols=mat[0].length;
//prints all submatrix greater than or equal to 2x2
for (int subRow = rows; subRow >= 2; subRow--) {
int rowLimit = rows - subRow + 1;
for (int subCol = cols; subCol >= 2; subCol--) {
int colLimit = cols - subCol + 1;
for (int startRow = 0; startRow < rowLimit; startRow++) {
for (int startCol = 0; startCol < colLimit; startCol++) {
for (int i = 0; i < subRow; i++) {
for (int j = 0; j < subCol; j++) {
System.out.print(mat[i + startRow][j + startCol] + " ");
}
System.out.print("\n");
}
System.out.print("\n");
}
}
}
}
}
#include <stdio.h>
int main() {
int mtrx_size = 8;
int mat[8][8] = {
{ 1, 2, 3, 4, 5, 6, 7, 8},
{ 9,10,11,12,13,14,15,16},
{17,18,19,20,21,22,23,24},
{25,26,27,28,29,30,31,32},
{33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48},
{49,50,51,52,53,54,55,56},
{57,58,59,60,61,62,63,64}
};
int i, j, ioff, joff, off_cnt;
int sub_mtrx_size;
/* if we make terminating condition sub_mtrx_size>=1 then we will have all
possible square sub matrices */
for(sub_mtrx_size = mtrx_size; sub_mtrx_size >= 1 ; sub_mtrx_size--) {
off_cnt = mtrx_size - sub_mtrx_size + 1;
for (ioff = 0; ioff < off_cnt; ioff++) {
for (joff = 0; joff < off_cnt; joff++) {
for (i = 0; i < sub_mtrx_size; i++) {
for (j = 0; j < sub_mtrx_size; j++) {
printf("%3d ", mat[i+ioff][j+joff]);
}
printf("\n");
}
printf("\n");
}
}
}
return 0;
}
Here is my code
#include<stdio.h>
void main() {
int a[4][4] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 15, 6, 5 },
{ 4, 3, 2, 1 } };
int max = a[0][0];
int mIndexF, mIndexE, addition = 0, multiplication = 1, i, j, status = 0, k,
l;
// this is for find out maximum value
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (max < a[i][j]) {
max = a[i][j];
mIndexF = i;
mIndexE = j;
}
}
}
for (k = 0; k < 4; k++) {
for (l = 0; l < 4; l++) {
if ((a[k][l] < max) && (status == 0)) {
addition += a[k][l];
} else {
status++;
if (a[k][l] != max) {
multiplication *= a[k][l];
}
}
}
}
printf("Addition is %d\n", addition);
printf("Multiplication is %d", multiplication);
return 0;
}
I want to find the maximum value. Also want to print addition of the values which are in before of the maximum value and want to print multiply value of the values which are in after the maximum value.
The following should do the trick:
#define MAX_INT (((unsigned int)(-1))>>1)
#define MIN_INT (~(MAX_INT))
void minmax(int a[4][4])
{
int i, j, maxi=0, maxj=0, max=MIN_INT, sum=0, mul=1;
// this is for find out maximum value
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (max < a[i][j]) {
max = a[i][j];
maxi = i;
maxj = j;
}
}
}
// this is to add and multiply
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (i< maxi || (i==maxi && j<maxj)) // this is "before"
sum += a[i][j];
else if (i==maxi && j==maxj) // this is "same"
; //..nothing to do
else mul *= a[i][j];
}
}
printf("i,j=%d,%d; sum= %d, mul= %d\n", maxi, maxj, sum, mul);
}
EDIT: Added definitions of MAX_INT and MIN_INT
Your code seems to be correct: just initialize
max=INT_MIN using include<limits.h>
Your second loops seems to slight inappropriate you could use:
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(status==0)
add+=disp[i][j];
else if(status==1)
mul*=disp[i][j];
if(i==loc_i && j==loc_j)
status=1;
}
Afterwards just subtract
add-=disp[mIndexF][mIndexE];
#include<stdio.h>
void main(){
int a[4][4]={
{10,11,12,13},
{14,15,16,17},
{18,19,20,21},
{22,2,3,3}
};
int max = a[0][0],mIndexF,mIndexE,addition = 0,multiplication = 1,i,j,status=0,k,l;
// this is for find out maximum value
for(i=0;i<4;i++){
for(j=0;j<4;j++){
if(max<a[i][j]){
max = a[i][j];
mIndexF=i;
mIndexE=j;
}
}
}
printf("The maximum value is %d\n", max);
for(k=0;k<4;k++){
for(l=0;l<4;l++){
if((a[k][l]<max) &&(status==0)){
addition+=a[k][l];
}else{
status++;
if(a[k][l]!=max){
multiplication*=a[k][l];
}
}
}
}
printf("Addition is %d\n",addition);
printf("Multiplication is %d",multiplication);
return 0;
}