How to create a cglm Mat4 from a float[16] array? - c

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

Related

How can I create a power set at the end of the program?

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);
}
}
}

Checking for numbers within arrays in C

The function below is looking through Order1-3 and looks if any of the elements are within each of the 3 column values of each of the 8 rows inside Winning_order. So in this case Order1 values are within the first row of Winning_order so it comes out as true as well as Order2. However Order2 is not a valid output for the program, how can I modify the iterating function below so that it checks to be true. Order3 is meant to be a false as well since its not within Winning_order. The code has been gotten from the answer of this issue issue.
#include <stdio.h>
// Iterating function
int match_arrays(int *arr1, int *arr2, int len)
{
for (int i = 0; i < len; i++) {
if (arr1[i] != arr2[i]) {
return 0;
}
}
return 1;
}
// Main function
int main(void)
{
int Order1[3] = {1,5,9};
int Order2[4] = {1,2,5,3};
int Order3[3] = {4,4,4};
int Winning_order[8][3] = {{1,2,3}, {4,5,6}, {7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};
for (int i = 0; i < 5; i++) {
if (match_arrays(Order1, Winning_order[i], 3)) {
printf("Order1");
}
if (match_arrays(Order2, Winning_order[i], 3)) {
printf("Order2");
}
if (match_arrays(Order3, Winning_order[i], 3)) {
printf("Order3");
}
}
return 0;
}
Expected Output
Order 1 Order 2
This matching method will output
Order2Order1
Could you explain some detail why Order2 will be true?
#include <stdio.h>
int match_arrays(int* arr_order, int* arr_win, int len_order, int len_win)
{
int idx = 0;
int err = 0;
for (int i = 0; i < len_order; i++) {
if (arr_order[i] != arr_win[idx]) {
err++;
if (err > len_order - len_win) {
return 0;
}
} else {
idx++;
if (idx == len_win)
{
return 1;
}
}
}
return 1;
}
int main()
{
int Order1[3] = {1, 5, 9};
int Order2[4] = {1, 2, 5, 3};
int Order3[3] = {4, 4, 4};
int Winning_order[8][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {1, 4, 7}, {2, 5, 8}, {3, 6, 9}, {1, 5, 9}, {3, 5, 7}};
for (int i = 0; i < 8; i++) {
if (match_arrays(Order1, Winning_order[i], 3, 3)) {
printf("Order1");
}
if (match_arrays(Order2, Winning_order[i], 4, 3)) {
printf("Order2");
}
if (match_arrays(Order3, Winning_order[i], 3, 3)) {
printf("Order3");
}
}
return 0;
}

Dijkstra algorithm~

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;
}

How to pass a full array in an array of pointers

I wrote a program that sorts the values of an array. It has 5 arrays,
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
and an array of pointers that holds those 5 arrays,
int* pArr[LEN] = { arr1, arr2, arr3, arr4, arr5 };
I want to sort the values of each array, but when I pass the arrays to the sorting function
sortArrValues(&pArr[i]);
it sees the first index of each array (arr1,arr2...) as the element of that array (pArr[i]), so pArr[i] is (3,2,0,1,4).
But I want pArr[i] to be the full array it meant to be, so in the first iteration pArr[i] will be (3,9,6,7).
Notes: the first index of each array indicates the length of that array (not including that index).
The sorting will skip the first index.
There are two additional functions,they're not used (you can skip them).
Here's the full code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 5
void sortArrValues(int** arr);
void sortArrAdress(int* pArr);
void printArr(int * pArr);
int main()
{
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
int* pArr[LEN] = { arr1, arr2, arr3, arr4, arr5 };
int i = 0;
for (i = 0; i < LEN; i++)
{
sortArrValues(&pArr[i]);
}
//sortArrAdress(pArr);
//printArr(pArr);
return 0;
}
/*
this function will sort the given arrays' values.
in: array
out: none
*/
void sortArrValues(int** arr)
{
int tmp = 0;
int i = 0;
for (i = 1; i < *arr; i++)
{
if (*arr[i] > *arr[i+1])
{
tmp = *arr[i];
*arr[i] = *arr[i + 1];
*arr[i + 1] = tmp;
}
}
}
There is some errors in your code.
First, you don't need to send &pArr[i], pArr[i] is enough to order your arrays. then your void sortArrValues(int** arr) become void sortArrValues(int* arr) which is clearer to read.
Second, in your sortValues, you when you change a value, you should restart (I know that's not very optimized but if you would want to make something really faster, you should use a quick sort. with your code, { 4, 5, 6, 2, 1 } will become { 4 5 2 1 6 }.
So that's your code with the fixes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 5
void sortArrValues(int* arr);
void sortArrAdress(int* pArr);
void printArr(int * pArr);
int main()
{
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
int* pArr[LEN] = { arr1, arr2, arr3, arr4, arr5 };
int i = 0;
for (i = 0; i < LEN; i++)
{
sortArrValues(pArr[i]);
}
//sortArrAdress(pArr);
//printArr(pArr);
for (int i = 0 ; i < LEN ; i++) {
for (int j = 0 ; j < pArr[i][0] + 1 ; j++)
printf("%d ", pArr[i][j]);
printf("\n");
}
return 0;
}
/*
this function will sort the given arrays' values.
in: array
out: none
*/
void sortArrValues(int* arr)
{
int tmp = 0;
int i = 0;
for (i = 1; i < *arr; i++)
{
if (arr[i] > arr[i+1])
{
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
i = 0;
}
}
}
I tried to modify your program to fix issues. Some of these issues are already highlighted by others. There were some there were not. Here is the working snippet. Let me know if you need more explanation on any modification. Note that I have only modified your sort function. The idea is not to give you the fastest sorting algorithm, but a working one.
int main()
{
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
int* pArr[] = { arr1, arr2, arr3, arr4, arr5 };
int i = 0;
for (i = 0; i < LEN; i++)
{
sortArrValues(&pArr[i]);
}
}
void sortArrValues(int** arr)
{
int tmp = 0;
int i = 0, j = 0;
for (i = 0; i < (**arr) - 1; i++)
{
for(j=i+1; j < (**arr) - 1; j++) {
if ((*arr)[i+1] > (*arr)[j+1])
{
tmp = (*arr)[i+1];
(*arr)[i+1] = (*arr)[j + 1];
(*arr)[j+1] = tmp;
}
}
printf("%d ", (*arr)[i+1]);
}
printf("%d\n", (*arr)[i+1]);
}

How can choose the five largest values in an array and put them in a new array?

How can choose the five largest values in an array and put them in a new array?
int[] a = { 1, 2, 5, 2, 4, 6, 8, 9, 1, 19 };
int[] largestValues = new int[5];
for (int i=0; i < 5; i++ ) {
System.out.println(largestValues[i]);
}
You can use following code
For Eg.
public static void main(String args[]) {
int i;
int large[] = new int[5];
int array[] = { 33, 55, 13, 46, 87, 42, 10, 34, 43, 56 };
int max = 0, index;
for (int j = 0; j < 5; j++) {
max = array[0];
index = 0;
for (i = 1; i < array.length; i++) {
if (max < array[i]) {
max = array[i];
index = i;
}
}
large[j] = max;
array[index] = Integer.MIN_VALUE;
System.out.println("Largest " + j + " : " + large[j]);
}
}

Resources