Point to specific value of an array - c

i have an array, int* array, with more than 10.000 int values, but i want to point to each 100 position, it means that I will have int ** matrix, where:
matrix[i][j], I want i from my matrix to point to array[i * 100], how can y substitute the address ? here is what I've done:
u_int8_t **matrix = (u_int8_t **)malloc(width * sizeof(u_int8_t *));
int width_cr = 0;
for (int i = 0; i < width; i ++) {
if (i % 100 == 0) {
u_int8_t *position = matrix[width_cr];
position = &array[i];
width_cr ++;
}
}
the problem is that it points to the beginning of the array

Store the address of array[i] in matrix[i / 100].
#define HOW_MUCH_NUMBERS 10000
[...]
{
int array[HOW_MUCH_NUMBERS];
int i = 0;
int **matrix;
matrix = malloc(sizeof(*matrix) * (HOW_MUCH_NUMBERS / 100));
while (i < HOW_MUCH_NUMBERS)
{
matrix[i / 100] = &array[i];
i += 100;
}
[...]
}

Related

As a result of processing arrays -nan(ind)

I am writing a program that creates arrays of a given length and manipulates them. You cannot use other libraries.
First, an array M1 of length N is formed, after which an array M2 of length N is formed/2.
In the M1 array, the division by Pi operation is applied to each element, followed by elevation to the third power.
Then, in the M2 array, each element is alternately added to the previous one, and the tangent modulus operation is applied to the result of addition.
After that, exponentiation is applied to all elements of the M1 and M2 array with the same indexes and the resulting array is sorted by dwarf sorting.
And at the end, the sum of the sines of the elements of the M2 array is calculated, which, when divided by the minimum non-zero element of the M2 array, give an even number.
The problem is that the result X gives is -nan(ind). I can't figure out exactly where the error is.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
const int A = 441;
const double PI = 3.1415926535897931159979635;
inline void dwarf_sort(double* array, int size) {
size_t i = 1;
while (i < size) {
if (i == 0) {
i = 1;
}
if (array[i - 1] <= array[i]) {
++i;
}
else
{
long tmp = array[i];
array[i] = array[i - 1];
array[i - 1] = tmp;
--i;
}
}
}
inline double reduce(double* array, int size) {
size_t i;
double min = RAND_MAX, sum = 0;
for (i = 0; i < size; ++i) {
if (array[i] < min && array[i] != 0) {
min = array[i];
}
}
for (i = 0; i < size; ++i) {
if ((int)(array[i] / min) % 2 == 0) {
sum += sin(array[i]);
}
}
return sum;
}
int main(int argc, char* argv[])
{
int i, N, j;
double* M1 = NULL, * M2 = NULL, * M2_copy = NULL;
double X;
unsigned int seed = 0;
N = atoi(argv[1]); /* N равен первому параметру командной строки */
M1 = malloc(N * sizeof(double));
M2 = malloc(N / 2 * sizeof(double));
M2_copy = malloc(N / 2 * sizeof(double));
for (i = 0; i < 100; i++)
{
seed = i;
srand(i);
/*generate*/
for (j = 0; j < N; ++j) {
M1[j] = (rand_r(&seed) % A) + 1;
}
for (j = 0; j < N / 2; ++j) {
M2[j] = (rand_r(&seed) % (10 * A)) + 1;
}
/*map*/
for (j = 0; j < N; ++j)
{
M1[j] = pow(M1[j] / PI, 3);
}
for (j = 0; j < N / 2; ++j) {
M2_copy[j] = M2[j];
}
M2[0] = fabs(tan(M2_copy[0]));
for (j = 0; j < N / 2; ++j) {
M2[j] = fabs(tan(M2[j] + M2_copy[j]));
}
/*merge*/
for (j = 0; j < N / 2; ++j) {
M2[j] = pow(M1[j], M2[j]);
}
/*sort*/
dwarf_sort(M2, N / 2);
/*sort*/
X = reduce(M2, N / 2);
}
printf("\nN=%d.\n", N);
printf("X=%f\n", X);
return 0;
}
Knowledgeable people, does anyone see where my mistake is? I think I'm putting the wrong data types to the variables, but I still can't solve the problem.
Replace the /* merge */ part with this:
/*merge*/
for (j = 0; j < N / 2; ++j) {
printf("%f %f ", M1[j], M2[j]);
M2[j] = pow(M1[j], M2[j]);
printf("%f\n", M2[j]);
}
This will print the values and the results of the pow operation. You'll see that some of these values are huge resulting in an capacity overflow of double.
Something like pow(593419.97, 31.80) will not end well.

problem calculating the inverse of a matrix

I'm trying to calculate the inverse of a square matrix of any rank N x N. I'm using a struct to store the values of the matrix which I can to effectively and I am already able to calculate the determinant. But there must be some issue with the inverse function. This is the code
struct m{
size_t row;
size_t col;
double *data;
};
void inverse(size_t n, struct m *A) /*Calculate the inverse of A */
{
size_t i,j,i_count,j_count, count=0;
double det = determinant(n, A);
size_t id = 0;
double *d;
struct m C; /*The Adjoint matrix */
C.data = malloc(sizeof(double) * n * n);
C.row = n;
C.col = n;
struct m *minor; /*matrices obtained by removing the i row and j column*/
if (!(minor = malloc(n*n*(n+1)*sizeof *minor))) {
perror ("malloc-minor");
exit(-1);
}
if (det == 0){
printf("The matrix is singular\n");
exit(1);
}
for(id=0; id < n*n; id++){
d = minor[id].data = malloc(sizeof(double) * (n-1) * (n-1));
for(count=0; count < n; count++)
{
//Creating array of Minors
i_count = 0;
for(i = 0; i < n; i++)
{
j_count=0;
for(j = 0; j < n; j++)
{
if(j == count)
continue; // don't copy the minor column element
*d = A->data[i * A->col + j];
d++;
j_count++;
}
i_count++;
}
}
}
for(id=0; id < n*n; id++){
for(i=0; i < n; i++){
for(j=0; j < n; j++)
C.data[i * C.col + j] = determinant(n-1,&minor[id]);//Recursive call
}
}
transpose(&C);
scalar_product(1/det, &C);
*A = C;
}
The determinant is calculated recursively with this algorithm:
double determinant(size_t n, struct m *A)
{
size_t i,j,i_count,j_count, count=0;
double det = 0;
if(n < 1)
{
printf("Error\n");
exit(1);
}
if(n==1) return A->data[0];
else if(n==2) return (A->data[0]* A->data[1 * A->col + 1] - A->data[0 + 1] * A->data[1*A->col + 0]);
else{
struct m C;
C.row = A->row-1;
C.col = A->col-1;
C.data = malloc(sizeof(double) * (A->row-1) * (A->col-1));
for(count=0; count < n; count++)
{
//Creating array of Minors
i_count = 0;
for(i = 1; i < n; i++)
{
j_count=0;
for(j = 0; j < n; j++)
{
if(j == count)
continue; // don't copy the minor column element
C.data[i_count * C.col + j_count] = A->data[i * A->col + j];
j_count++;
}
i_count++;
}
det += pow(-1, count) * A->data[count] * determinant(n-1,&C);//Recursive call
}
free(C.data);
return det;
}
}
You can find the complete code here: https://ideone.com/gQRwVu.
Use some other variable in the loop after :
det + =pow(-1,count) * A->data[count] *determinant (n-1,&C)
Your calculation of the inverse doesn't quite correspond to the algorithm described e. g. for Inverse of a Matrix
using Minors, Cofactors and Adjugate, even taken into account that you for now omitted the adjugate and division step. Compare your outermost for loop in inverse() to this working implementation:
double Rdata[(n-1)*(n-1)]; // remaining data values
struct m R = { n-1, n-1, Rdata }; // matrix structure for them
for (count = 0; count < n*n; count++) // Create n*n Matrix of Minors
{
int row = count/n, col = count%n;
for (i_count = i = 0; i < n; i++)
if (i != row) // don't copy the current row
{
for (j_count = j = 0; j < n; j++)
if (j != col) // don't copy the current column
Rdata[i_count*R.col+j_count++] = A->data[i*A->col+j];
i_count++;
}
// transpose by swapping row and column
C.data[col*C.col+row] = pow(-1, row&1 ^ col&1) * determinant(n-1, &R) / det;
}
It yields for the given input data the correct inverse matrix
1 2 -4.5
0 -1 1.5
0 0 0.5
(already transposed and divided by the determinant of the original matrix).
Minor notes:
The *A = C; at the end of inverse() loses the original data pointer of *A.
The formatting function f() is wrong for negative values, since the fraction is also negative in this case. You could write if (fabs(f)<.00001).

Navigating through an array of structs and storing values not working properly

I have this code that's very messy and there are two structs, both defined and initialized the same. However for the tall struct I can store variables in the struct tall[radius] without any issues. But when I replicate the process for xx struct it doenst work prints wrong values.
I can't seem to figure out what's wrong with the struct usage.
I need an array of structs to store a different sized array dynamically every time, and other elements later for each struct.
I'm open to new approaches, too.
Why are the two structs performing differently?
#include <stdio.h>
#include <stdlib.h>
#define ar_length 18 //this is the radius max
#define WIDTH 10 //xx width
struct ttall
{
int *pnt;
};
struct xall
{
int *pnt;
};
int main()
{
int i;
int m;
int *x1;
int *x2;
int *x3;
int *ttThis;
int rad_size;
//beginning of the loop
int radius = 1;
int size = (2 * radius + 1);
int size_sqrd = size * size * size;
struct ttall **tall = malloc(sizeof(struct ttall *));
struct ttall *struct_pnt = malloc( 3*sizeof(struct ttall));
struct xall **xx = malloc(sizeof(struct xall *));
struct xall *xpnt = malloc( 3*sizeof(struct xall));
for (int radius = 0; radius < 3; radius++)
{
//need to increment the pointer to the struct everytime
xx[radius] = xpnt+radius;
tall[radius] = struct_pnt+radius;
int z = -1;
int t = -radius-2;
printf("****T*** %d\n",t);
int rad;
int meshCount;
meshCount = (2 * (radius+1) + 1);
rad_size = (2 * (radius+1) + 1) * (2 * (radius+1) + 1) * (2 * (radius+1) + 1);
printf("radius size : %d\n",rad_size);
printf("mesh size : %d\n",meshCount);
tall[radius]->pnt = malloc(rad_size * sizeof(int));
xx[radius]->pnt = malloc(rad_size * WIDTH * sizeof(int));
x1 = malloc(rad_size * sizeof(int));
x2 = malloc(rad_size * sizeof(int));
x3 = malloc(rad_size * sizeof(int));
for (i = 0; i < meshCount; i++)
{
t++;
z++;
for (m = 0; m < meshCount*meshCount; m++)
{
x1[z * (meshCount*meshCount) + m] = t;
}
}
//x2 computations
i = 0;
m = 0;
z = 0;
t = -radius-2;
int x;
for (x = 0; x < meshCount; x++)
{
t++;
for (m = 0; m < meshCount; m++)
{
for(int h = 0;h<meshCount;h++){
int index = meshCount*x+(meshCount*meshCount)*h +m ;
x2[index] = t;
}
}
}
//x3 computations
i = 0;
m = 0;
z = 0;
t = -radius-2;
for (x = 0; x < meshCount; x++)
{
// t++;
t = -radius-2;
for (m = 0; m < meshCount; m++)
{
t++;
for(int h = 0;h<meshCount;h++){
int index = meshCount*x+(meshCount*meshCount)*h +m ;
x3[index] = t;
}
}
}
// structure initializations and memalocation
//works fine with expanding radius
for (m = 0; m < rad_size; m++)
{
tall[radius]->pnt[m] = (x1[m] * x1[m]) + (x2[m] * x2[m]) + (x3[m] * x3[m]);
}
// doesnt work here
m = 0;
for (i = 0; i < rad_size; i++)
{
xx[radius]->pnt[i * WIDTH ] = 1;
xx[radius]->pnt[i * WIDTH + 1] = x1[i];
xx[radius]->pnt[i * WIDTH + 2] = x2[i];
xx[radius]->pnt[i * WIDTH + 3] = x3[i];
xx[radius]->pnt[i * WIDTH + 4] = x1[i] * x1[i];
xx[radius]->pnt[i * WIDTH + 5] = x1[i] * x2[i];
xx[radius]->pnt[i * WIDTH + 6] = x1[i] * x3[i];
xx[radius]->pnt[i * WIDTH + 7] = x2[i] * x2[i];
xx[radius]->pnt[i * WIDTH + 8] = x2[i] * x3[i];
xx[radius]->pnt[i * WIDTH + 9] = x3[i] * x3[i];
}
}
//free(x1);
//free(x2);
//free(x3);
//***testing sum***
// the sum when radius = 1 of xx should be 171
int k = 0;
int sum = 0;
//can replace 27 with rad_size
for (k = 0; k < 27*WIDTH; k++)
{
sum = sum + abs(xx[0]->pnt[k]);
//printf("%d\n",abs(xx[0]->pnt[k]));
}
printf(" sum xx : %d\n", sum);
//******Testing****
for (int c = 0; c < 27; c++)
{
//printf("X2 : %d\n",x3[c]);
printf("tall : %d\n", tall[1]->pnt[c]);
//printf("%d\n",xx[c]);
}
//free(ttThis);
//free(xx);
}
With struct ttall **tall = malloc(sizeof(struct ttall *)); you allocate room for one pointer.
But later you index it with tall[radius] but radius is more than 1.
You must allocate more memory:
struct ttall **tall = malloc(3 * sizeof(struct ttall *));`

Dynamic 2 array in C

I am learning C and I am trying to have a virtual grid in it, where the user can put new grid elements to "activate" into the console. So for example if I am starting with nothing and the user adds (40,50), then the size is at least 40x50 with element (40,50) initialised. If (20,30) follows, it just activates the element at 20,30. But if the user then enters (500,300), it will allocate some more memory and increases the size of the array. I would like to access them easily. I would like to work (I might have to anyway), because they are new for me.
My code (at the moment) is the following:
int width = 4, height = 5;
bool **arr = (bool **) malloc(height * sizeof(bool *));
for (int x = 0; x < height; x++) {
arr[x] = (bool *) malloc(width * sizeof(bool));
}
for (int x = 0; x < height; x++) {
for (int y = 0; y < width; y++) {
*(*(arr + x) + y) = false;
}
}
*(*(arr + 3) + 2) = true;
// user puts a value bigger than (4,5) inside
int newX=10, newY = 10;
//allocate more memory
So I am using a 2D pointer with booleans and I do "malloc" the height first and afterwards make an array of them for the width.
In the last line is just an example of entering the first element at (2,3). The scan method for the user doesn't matter here.
So is there a way of increasing the size of my array afterwards or do I need a totally different concept for it?
=====
The current code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main() {
int width = 4, height = 5;
bool **arr = (bool **) malloc(height * sizeof(bool *));
for (int x = 0; x < height; x++) {
arr[x] = (bool *) malloc(width * sizeof(bool));
}
for (int x = 0; x < height; x++) {
for (int y = 0; y < width; y++) {
*(*(arr + x) + y) = false;
}
}
*(*(arr + 3) + 2) = true;
int newWidth = 10, newHeight = 10;
bool **narr = realloc(arr, newHeight * sizeof(bool*));
if(narr) {
arr = narr;
for(size_t i = 0; i < newHeight; i++){
bool* p = realloc(arr[i] , newWidth * sizeof(bool));
if( !p ){
perror("realloc");
}
arr[i] = p;
}
// here resize the number of elements if needed
}
else {
perror("realloc failed");
exit(EXIT_FAILURE);
}
return 0;
}
Yes there is a mathod called realloc. And you can use that. You can resize the jagged array completely.
bool **narr = realloc(arr , newsize * sizeof(bool*));
if( narr ) {
arr = narr;
for(size_t i = 0; i < newsize; i++){
bool* p = realloc(arr[i] , newsize1 * sizeof(bool));
if( !p ){
perror("realloc");
}
arr[i] = p;
}
// here resize the number of elements if needed
}
else {
perror("realloc failed");
exit(EXIT_FAILURE);
}
Also simplify things write a[1][2] instead of *(*(a+1)+2). The check is needed as realloc may fail - in that case instead of letting your code go awry, take appropriate step as needed.
Also note that you need to set all the newly allocated bool* to NULL. So do this:-
for(size_t i = 0; i < newHeight; i++){
if( i >= height)
arr[i] = NULL;
bool* p = realloc(arr[i] , newWidth * sizeof(bool));
if( !p ){
perror("realloc");
}
arr[i] = p;
}
This is needed because realloc expects address of memory previously allocated with *alloc functions or NULL.
You can use realloc to increase the size of the array
in your situation you'd do
arr = (bool **)realloc(arr, sizeof(bool*) * new_height)
for(int i = 0; i < new_height; i++){
arr[i] = (bool *)realloc(arr, sizeof(bool) * new_width);
}
Note that you have to check whether the array needs to even get bigger, realloc can also "shrink" your array so tread carefully.
Initializing the newly created memory to false:
for(int i = old_height; i < new_height; i++){
for(int j = old_width; j < new_width; j++){
arr[i][j] = false;
}
}
arr[new_height - 1][new_width - 1] = true;

C - Returned array fills the array with the first value only

My problem is that when I create a local array in the function and return it, all the values in the array become assigned to the array's first value. So for an example, an array of size 4 with the values 3, 5, 11, 13 is returned. The array now only has the values 3, 3, 3, 3. I'm fairly new to coding, so any help would be appreciated.
uint8_t* half( const uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int size = (cols / 2) * (rows / 2);
uint8_t *newarray = malloc( size * sizeof(int) );
float average;
for (int i = 0; i < (rows / 2); i++) {
for (int k = 0; k < (cols / 2); k++) {
average = (array[ (2*k) + (2*i*cols) ] + array[ ((2*k) + 1) + (2*i*cols) ] + array[ ((2*k) + 1) + ((2*i*cols) + cols) ] + array[ (2*k) + ((2*i*cols) + cols) ]) / 4.0;
newarray[k + (i * (cols / 2))] = (int)( round(average) );
}
}
return newarray;
}
int main () {
uint8_t *halfarray;
halfarray = half(testarray, test_width, test_height);
for (int i = 0; i < 4; i++) {
printf("values in array %d\n", *halfarray);
}
return 0;
}
Try changing this line:
printf("values in array %d\n", *halfarray);
to this:
printf("values in array %d\n", *halfarray);
halfarray++
Use pointer arithmetic at each iteration to increment the position of the non-constant pointer, thusly:
for (int i = 0; i < 4; i++, halfarray++) {
printf("values in array %d\n", *halfarray);
}

Resources