How to pass a full array in an array of pointers - c

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

Related

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

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

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

How to create all posible combinations (not permutations) of array of ints in C?

I would like to generate all posible combinations of ints, an example:
for set [1, 2, 3]; there all combinations are:
[
[], [ 3 ],
[ 2 ], [ 3, 2 ],
[ 1 ], [ 3, 1 ],
[ 2, 1 ], [ 3, 2, 1 ]
]
I know, how to do that in javascript, but cannot find out, how to do the same in C. The javascript version:
const combinations = (elements) => {
if (elements.length === 0) return [[]];
const firstEl = elements[0];
const rest = elements.slice(1);
const combsWithoutFirst = combinations(rest);
const combsWithFirst = [];
combsWithoutFirst.forEach(combs => {
const combWithFirst = [...combs, firstEl];
combsWithFirst.push(combWithFirst);
});
return [...combsWithoutFirst, ...combsWithFirst];
}
console.log(combinations([1, 2, 3]));
So what could be the c version?
EDIT: the version I have so far, but is not working correctly:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int *values;
size_t size;
} comb;
comb *combinations;
size_t depth;
int count;
void init()
{
count = 1; // number of combintations so far
int size = depth;
for (int i = 0; i < depth; i++)
{
size *= 2;
}
combinations = malloc(sizeof(comb) * size);
}
comb *ctor(int *values, size_t size)
{
comb *newone = malloc(sizeof(comb));
newone->values = values;
newone->size = size;
return newone;
}
comb *pop_front(comb x)
{
int newSize = x.size - 1;
int *newVals = malloc(sizeof(int) * newSize);
memcpy(newVals, x.values + sizeof(int), newSize);
return ctor(newVals, newSize);
}
comb *push_back(comb x, int newValue)
{
int newSize = x.size + 1;
int *newVals = malloc(sizeof(int) * newSize);
memcpy(newVals, x.values, x.size * sizeof(int));
newVals[newSize - 1] = newValue;
return ctor(newVals, newSize);
}
void addComb(comb *x, comb y)
{
}
comb *func(comb *elements)
{
if (!depth)
{
return ctor(NULL, 0);
}
int firstEl = elements->values[0];
comb *rest = pop_front(*elements);
depth--;
comb *combsWithoutFirst = func(rest);
comb *combsWithFirst = ctor(NULL, 0);
for (int i = 0; i < count; i++)
{
comb *combWithFirst = push_back(combsWithoutFirst[i], firstEl);
// now add that combWithFirst to the combsWithFirst
}
// merge the two arrays
}
int main()
{
}
One simple way to do this is using a bitset. I guess 64 bits is far enough.
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
void comb(int *set, size_t n) {
assert(n < 64);
uint64_t bits = 0;
uint64_t end = 1 << n;
while (bits < end) {
printf("[");
for (size_t i = 0; i < n; ++i) {
if (bits & (1 << i)) {
printf("%d ", set[i]);
}
}
printf("]\n");
bits++;
}
}
int main(void) {
int set[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
comb(set, 10);
return 0;
}

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

Bubble sort an Array of pointers of another arrays in a function (C)

I want to make a bubble sort function of an array of pointers that each of the pointers point to another arrays - inside of function and i'm getting a error that i violated a writing location (Visual Studio)
P.S, I do (*parr)++ because the first value of each array shows the length of the array without the first value so i need to start bubble sorting from the second box (arr[1] and not arr[0] for example ).
can someone write to me how can i fix it?
Thanks for help
(I need to sort the values of the original arrays not the pointer of the arrays).
int main(void){
int i = 0;
int arr0[4] = { 3, 9, 6, 7 };
int arr1[3] = { 2, 5, 5 };
int arr2[1] = { 0 };
int arr3[2] = { 1, 6 };
int arr4[5] = { 4, 5, 6, 2, 1 };
int* parr[5] = { arr0, arr1, arr2, arr3, arr4 };
func1(parr);
system("PAUSE");
return (0);
}
void func1(int** parr)
{
int i;
int temp;
int j;
int k;
int length;
for (i = 0; i < 5; i++, (parr)++)
{
length = **parr;
(*parr)++;
for (j = 0; j < length-1; j++)
{
for (k = 0; k < length - j - 1; k++, (*parr)++)
{
if ((**parr)>(*(*parr + 1)))
{
temp = **(parr);
**(parr) = (*(*parr + 1));
(*(*parr + 1)) = temp;
}
}
}
}
}
This seems to work. It is easier in func1 to use dereferenceing as parr[i][k] rather than moving the pointer.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void func1(int** parr);
int main(void){
int j;
int arr0[4] = { 3, 9, 6, 7 };
int arr1[3] = { 2, 5, 5 };
int arr2[1] = { 0 };
int arr3[2] = { 1, 6 };
int arr4[5] = { 4, 5, 6, 2, 1 };
int* parr[5] = { arr0, arr1, arr2, arr3, arr4 };
func1(parr);
for (j = 1; j <= arr0[0]; j++)
{
printf ( "arr0[%d] %d\n", j, arr0[j]);
}
for (j = 1; j <= arr4[0]; j++)
{
printf ( "arr4[%d] %d\n", j, arr4[j]);
}
return (0);
}
void func1(int** parr)
{
int i;
int temp;
int j;
int k;
int length;
for (i = 0; i < 5; i++)
{
length = **parr;
for (j = 0; j < length; j++)
{
for (k = 1; k < length - j; k++)
{
temp = *((*parr)+k);
if (*((*parr)+k)>*((*parr)+k+1))
{
temp = *((*parr)+k);
*((*parr)+k) = *((*parr)+k+1);
*((*parr)+k+1) = temp;
}
}
}
*parr++;// next array
}
}

Resources