I have the following array and index
0 1 2 3 4 5 6 7 8 <-indexes -- n values
7 7 8 8 6 9 8 7 9 <-values -- array x
I have another array with the indices
0 1 2 3 4 5 6 7 8 <-indexes -- m values
7 8 9 9 8 7 7 8 9 <-values -- array y
what I want is to obtain all the index(in reverse order of array x):
6: 4
7: 7 1 0
8: 6 3 2
9: 8 5
And then, for each element in the y array, I want to make another array index[ ] have the values. So, index[6] = 4, index[7] = 7, 1, 0 etc.
I have done:
for (i=n-1; i>=0; i--) {
index[y[i]] = index[y[i]]+i;
}
for (i=0; i<m; i++) {
strings[i] = strings[i] + index[x[i]];
}
for (i=0; i<m; i++) {
printf("%d",strings[i]);
}
The desired output is to print a sequence like 710632858563271071063285 but this is not printing the desired output.
int i;
int j;
// Assuming 9 is the highest integer possible
for(j=0;j<=9;j++){
int k=0;
for(i=n-1;i>=0;i--){
if(x[i]==j){
index[j][k]=i;
k++;
}
}
// Adding a -1 to denote that no more indices were found
index[j][k]=-1;
}
PS: Make sure that index is big enough...
#include <stdio.h>
#include <string.h>
int main(void) {
int x[] = {7, 7, 8, 8, 6, 9, 8, 7, 9};
int n = sizeof(x)/sizeof(*x);
int y[] = {7, 8, 9, 9, 8, 7, 7, 8, 9};
int m = sizeof(y)/sizeof(*y);
int index[10][10];
memset(index, -1, sizeof(index));
for(int i = n -1; i >= 0; --i){
int j = 0;
while(index[x[i]][j] >= 0)
++j;
index[x[i]][j] = i;
}
for(int i = 0; i < m; ++i){
for(int j = 0; j < 10 && index[y[i]][j] >= 0; ++j){
printf("%d", index[y[i]][j]);
}
}
return 0;
}
I'd say that array is not the best for this purpose as you have to iterate multiple times over it every time you want to know what numbers are at what indexes. If memory isn't an issue you will do much better with combination of data structures to efficiently perform this task.
An array is fine for matching index with value. What I suggest is to keep numbers in linked list. Every node would also keep linked list of indexes that number appears on. This way you'd do much better because of search time.
Adding value would mean that you:
1) check if there is already value at given index in an array. If there is - you remove this index from linked list of indexes for given number.
2) Then you put new value at given index and if this is first appearance of this value, else you add new index to list of indexes this value appears.
If you implement adding to the linked list of indexes in descending order to get all the numbers and indexes you just need to iterate over list of values and print list of indexes.
This might look like an overkill but I don't know a thing about the actual problem at hand. Your array might be thousands indexes long. That would set you up for handling these more efficiently.
try this one:
#include <stdio.h>
int main()
{
int A[]={7,7,8,8,6,9,8,7,9};
for(int i=0;i<=9;i++)
{
int flag=0;
for(int j=8;j>=0;j--)
{
if(A[j]==i)
{
if(flag==0)
printf("%d:",i);
flag=1;
printf(" %d",j);
}
}
if(flag==1)
printf("\n");
}
return 0;
}
This should work as expected. Note, that the main loop is simpler than the output loop.
#include <stdio.h>
int idx[9][9] = {{-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1}};
int x[9] = {7, 7, 8, 8, 6, 9, 8, 7, 9};
void buildIndex(int maxval, int idx_len, int x_len) {
assert(maxval >= x_len);
for(int i = 0; i < x_len; i++)
for(int j = idx_len - 1; j >= 0; j--)
if(idx[x[i]-1][j] == -1) {
idx[x[i]-1][j] = i;
break;
}
}
int main() {
buildIndex(9, 9, 9);
/* output the idx */
for(int k = 0; k < 9; k++)
if(idx[k][8] != -1) { /* ignore empty idx lines */
printf("%d: ", k+1);
for(int l = 0; l < 9; l++)
if(idx[k][l] != -1) /* write all valid numbers */
printf("%d ", idx[k][l]);
printf("\n");
}
}
This program outputs:
6: 4
7: 7 1 0
8: 6 3 2
9: 8 5
I am sure, you can adapt the output to your needs yourself. The "big" idea in this code is to fill the arrays in idx backwards and in the output just ignore the unset values at the beginning.
Related
I'm trying to jump from an element to another element with a specified number for jumping and how many times it jumps, for example, k=4, and if it reaches the end it goes back from where it started. For example, as in the code, the array for a[Max] will be like {1,4,7,1}
#define Max 100
int main() {
int i=0,n,k,counter,j=0;
char v[Max]={1,2,3,4,5,6,7,8};
int a[Max];
k=4;
counter=k+1;
int size=strlen(v);
while(counter!=0) {
for(i=0;i<size;i=i+k-1){
a[j]=(int)v[i];
j++;
counter--;
}
}
}
You shouldn't be using a string or strlen() for this. Use an int array and you can get the size of your int array by using sizeof. Here, sizeof(v) will tell you the number of bytes allocated for your array, which in this case is 36 (Assuming ints are 4 bytes). Then you can divide by the number of bytes of an integer with sizeof(int) to get the number of elements in your array, 9.
You're segfaulting because you're writing outside the bounds of your array. You don't need that outer loop and should remove it entirely.
To get the wrapping around of your array, use the modulus operation (%) with the size of your array. Understanding The Modulus Operator %
#include <stdio.h>
#define MAX 100
int main() {
int i = 0, ii = 0, k = 4, counter = k - 1, j = 0;
int v[]= {1, 2, 3, 4, 5, 6, 7, 8, 9};
int a[MAX];
int size = sizeof(v) / sizeof(int);
for (i=0; counter >= 0; i += k - 1) {
a[ii++] = v[j];
counter--;
j = (j += 3) % size;
}
for (int i = 0; i < k; i++) {
printf("%d\n", a[i]);
}
}
Output:
1
4
7
1
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 1 year ago.
Improve this question
if my program needs an int array of size of at most 10 and my user is given the option to "terminate" the data set when entering -1, what are some ways i can acheive this?
By "terminating", I am referring to if my user enters -1 while inputting the values in the array, the array stopps scanning user input values immediately and returns the values that were entered previously, discarding -1.
The array size can be at most 10, but i do not know how to process the array when my user enters less than 10 values.
Example:
Enter data -> 1 2 3 4 5 -1
data[10] contains {1, 2, 3, 4, 5}
Enter data -> 1 2 3 4 5 6 7 8 9 10
data[10] contains {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Enter data -> 1 2 3 4 5 6 7 -1
data[10] = {1, 2, 3, 4, 5, 6, 7}
I'm not allowed to use break statements. Right now I can only think of searching for -1 and turning it and the reset of the values into 0. This is what i have so far:
int getValues(int arr[])
{
int loop; // used to iterate through for loop
int terminate; // used to iterate through for loop when user terminates data set
loop = 0;
terminate = 0;
printf("Enter 10 integer values -> ");
for (loop = 0; loop < 10; loop++)
{
scanf("%d", &arr[loop]);
if (arr[loop] == -1)
{
arr[loop] = 0;
for (terminate = loop; terminate < 10; terminate++)
{
arr[terminate] = 0;
}
}
}
printf("\n");
return 0;
}
Maybe you want this: We stop the loop as soon as the user has entered -1 but we simply store that -1 in the array and we use that -1 one as terminator.
#include <stdio.h>
int getValues(int arr[])
{
int loop; // used to iterate through for loop
printf("Enter 10 integer values -> ");
for (loop = 0; loop < 10; loop++)
{
scanf("%d", &arr[loop]);
if (arr[loop] == -1)
{
loop = 10 - 1; // simulate a break
}
}
printf("\n");
return 0;
}
int main()
{
int arr[10];
getValues(arr);
// print array values and stop at the -1 terminator
for (int i = 0; i < 10 && arr[i] != -1; i++)
printf("arr[%d] = %d\n", i, arr[i]);
}
Another possibility is to store the size of the array:
#include <stdio.h>
int getValues(int arr[], int *size)
{
int loop; // used to iterate through for loop
printf("Enter 10 integer values -> ");
for (loop = 0; loop < 10; loop++)
{
scanf("%d", &arr[loop]);
if (arr[loop] == -1)
{
*size = loop; // store the size
loop = 10 - 1; // simulate a break
}
}
printf("\n");
return 0;
}
int main()
{
int arr[10];
int size;
getValues(arr, &size);
// now size contains the number of elements of the array
for (int i = 0; i < size; i++)
printf("arr[%d] = %d\n", i, arr[i]);
}
There are still issues with this code:
the return value of scanf is ignored
there is no check for out of bounds array access, In other words, the user can enter more than 10 numbers and thus write beyond the array bounds which will lead to undefined behaviour (google that term).
the hard coded value 10 is all over the place
I think I'm close to being able to complete this program, but I'm not entirely sure how to continue.
Basically, I have three arrays. Array 3 is empty, and Array 1 and Array 2 have values that are up to the users discretion. I want to merge Array 1 and Array 2 into Array 3 in such a way that they're alternating between even and odd positions in Array 3.
So, for example:
Array 1 = [1,2,3,4,5]
Array 2 = [10,20,30,40,50]
And Array 3 = [0,0,0,0,0,0,0,0,0,0]
I want the end result to look like so:
Array 3 = [1,10,2,20,3,30,4,40,5,50]
That's what I mean when I say I want Array 1 to fill odd values, and Array 2 to fill even values.
This is what the relevant piece of code I have so far looks like:
void EvenOdd(int n, int *ap1, int *ap2, int *ap3) {
// "n" is set to 5.
ap1 = arr1;
ap2 = arr2;
ap3 = arr3;
int i;
int j;
int k;
for (i = 0; i < n * 2; i++) {
for (j = 0; j < n; j++) {
if ((i + 1) % 2 != 0)
ap3[i] = ap1[j];
}
for (k = 0; k < n; k++) {
if ((i + 1) % 2 == 0)
ap3[i] = ap2[k];
}
}
}
arr1, arr2, and arr3 are all global arrays.
Every time I run the program, it only assigns the last values of Array 1 and Array 2 to the positions in Array 3, like so:
Array 1 = [1,2,3,4,5]
Array 2 = [6,7,8,9,10]
And Array 3 = [5,10,5,10,5,10,5,10,5,10]
This all suggests to me that the two for loops inside of the first one keep running for "i" until they reach the end of their array every time, which is why the last values are consistently assigned, but I'm not sure how to fix this to have the intended result.
I would go for something like this
void EvenOdd(int n, int*ap1, int*ap2, int*ap3){
// "n" is set to 5.
ap1=arr1;
ap2=arr2;
ap3=arr3;
int i;
for(int i = 0; i < 2 * n; i++)
{
if(i%2)
{
*ap3= *ap2;
ap1++;
}
else
{
*ap3= *ap1;
ap2++;
}
ap3++;
}
}
Problem:
That's what I mean when I say I want Array 1 to fill odd values, and
Array 2 to fill even values
Actually, in the case of indices, array1 fills the even indices and array2 fills the odd indices. Luckily, your code works in this regard because you check the parity of i + 1, and not i.
Every time I run the program, it only assigns the last values of Array
1 and Array 2 to the positions in Array 3, like so:
The i-loop is responsible to fill the destination array. But you have two other loops nested inside it, which rewrite ap[i] over and over with the value in even and odd indices of ap1 and ap2. The last value that remains in ap[i] is when the j-loop and k-loop ends. That is why, you see repeated values filled inside ap3 from the end of ap1 and ap2.
Solution:
You simply need a single i-loop that fills the destination array after checking the parity of i.
for (int i = 0; i < 2 * n; ++i)
ap3[i] = i % 2 ? ap2[i / 2] : ap1[i / 2];
An other way would be
#include <stdio.h>
void EvenOdd(int n, int *ap1, int *ap2, int *ap3) {
for (int i = 0; i < 2 * n; i++)
if(i % 2 == 0)
ap3[i] = ap1[i / 2];
else
ap3[i] = ap2[i / 2];
}
int main() {
int arr1[5] = {1, 2, 3, 4, 5};
int arr2[5] = {10, 20, 30, 40, 50};
int arr[10];
EvenOdd(5, arr1, arr2, arr);
for (int i = 0; i < 10; i++)
printf("%d ", arr[i]);
return 0;
}
Output
1 10 2 20 3 30 4 40 5 50
You could do something like this:
int ap1[5] = {1, 2, 3, 4, 5};
int ap2[5] = {10, 20, 30, 40, 50};
int ap3[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int j = 0;
int k = 0;
for (int i = 0; i < 10; i++) {
if (i % 2 != 0) { // if i is odd
ap3[i] = ap2[j++]; // set ap3 to jth value in ap2; j becomes j+1
} else { // if i is even
ap3[i] = ap1[k++]; // likewise, but ap1 and k becomes k+1
}
}
for (int i = 0; i < 10; i++) {
printf("%d ", ap3[i]);
}
Output is 1 10 2 20 3 30 4 40 5 50.
Your issue is that the nested loops are unnecessary and infact rewriting values when you don't want them to.
I am trying to find the duplicate values in an array. When a number is duplicated once like (25,25) program correctly prints 25 once but when a number duplicated twice like (12,12,12) program prints 12 three times while it should print it once.
#include<stdio.h>
int main()
{
int numbers[15]={1,1,2,2,2,3,4,5,6,6,7,7,7,8,9},i,j;
for(i=0;i<15;i++)
{
for(j=i;j<14;j++)
{
if(numbers[i]==numbers[j+1])
{
printf("Number %d has duplicate values\n",numbers[i]);
}
}
}
return 0;
}
Output:
Number 1 has duplicate values
Number 2 has duplicate values
Number 2 has duplicate values
Number 2 has duplicate values
Number 6 has duplicate values
Number 7 has duplicate values
Number 7 has duplicate values
Number 7 has duplicate values
The arrays cannot be assumed to be ordered so the program should work even with {1,2,1,2,3,4,2,5,6,6,7,7,7,8,9}.
The problem at hand is essentially simple. There is no need for any complicated code fragments. All it takes is just a little modification to the limits of the for loops and how you check for duplicates.
The solution:
Just introduce another array which is going to store the elements which are repeated in the array. Start filling this array from the 0th index as and when you find a NEWLY REPEATED element. This can easily be done iterating through this new array and checking if the currently encountered repeated element is already present or not. If it is not present there, then insert into the new array, which in the code I have given below is arr2[].
Hence whatever elements will be held by this new array are the unique repeated elements. I have attached the code and corresponding output below.
CODE:
#include<stdio.h>
int main()
{
int numbers[15] = {1,1,2,2,2,3,4,5,6,6,7,7,7,8,9}, i, j;
int arr2[15], k = 0, k1 = 0;
int flag = 0;
for(i = 0; i < 15; i++)
{
for(j = 0; j < 15; j++)
{
flag = 0;
if(i != j && numbers[i] == numbers[j])
{
for(k1 = 0; k1 < k; k1++)
if(arr2[k1] == numbers[j])
flag = 1;
if(flag != 1)
arr2[k++] = numbers[j];
}
}
}
for(i = 0; i < k; i++)
printf("Number %d has duplicate values\n",arr2[i]);
return 0;
}
OUTPUT:
Number 1 has duplicate values
Number 2 has duplicate values
Number 6 has duplicate values
Number 7 has duplicate values
Hope this helps.
A solution which doesn't introduce additional memory requirements and finds unique duplicates in a worst case of O(N²):
#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
// macro that evaluates to the number of elements in an array:
#define SIZEOF_ARRAY(arr) sizeof(arr) / sizeof(*arr)
// contains() returns true if [values, values + size) contains value
bool contains(int *values, size_t size, int value)
{
for (size_t i = 0; i < size; ++i)
if (values[i] == value)
return true;
return false;
}
int main(void)
{
int numbers[] = { 1, 1, 2, 2, 2, 3, 4, 5, 6, 6, 7, 7, 7, 8, 9 };
for (size_t i = 0; i < SIZEOF_ARRAY(numbers) - 1; ++i)
{
if (contains(numbers, i, numbers[i])) // if we already encountered numbers[i]
continue; // it was already identified as dup.
if(contains(numbers + i + 1, SIZEOF_ARRAY(numbers) - i , numbers[i]))
printf("Number %d has duplicate values\n", numbers[i]);
}
}
Output:
Number 1 has duplicate values
Number 2 has duplicate values
Number 6 has duplicate values
Number 7 has duplicate values
If you deal with low numbers, you can always go via index table
#include <stdio.h>
int main() {
int index[256] = {0};
int table[15] = {1, 2, 3, 5, 5, 6, 1, 2, 9, 10, 11, 2, 3, 4, 5 };
for(int i=0; i<15; i++) {
index[table[i]] ++;
}
for(int i=0; i<256; i++) {
index[i] > 1 ? printf("%d\n",i) : 0;
}
}
In your example consider the indexes of the 7: they are 10, 11, 12.
The first print happens when i=10 and j=10 because your are indexing numbers[10] and numbers[11].
At the next iteration of the inner loop you have i=10 and j=11 and the condition is verified again by the elements numbers[10] and numbers[12].
Finally the third condition happens when i=11 and j=11 because you are referring to numbers[11] and numbers[12].
All this becomes clear if you print your indexes when the condition happens. For example you can substitute your printf with this:
printf("Number %d has duplicate values with indexes i=%d, j=%d\n",numbers[i], i,j);
which prints:
Number 1 has duplicate values with indexes i=0, j=0
Number 2 has duplicate values with indexes i=2, j=2
Number 2 has duplicate values with indexes i=2, j=3
Number 2 has duplicate values with indexes i=3, j=3
Number 6 has duplicate values with indexes i=8, j=8
Number 7 has duplicate values with indexes i=10, j=10
Number 7 has duplicate values with indexes i=10, j=11
Number 7 has duplicate values with indexes i=11, j=11
If your values are ordered you can do something like this:
#include<stdio.h>
int main()
{
int numbers[15]={1,1,2,2,2,3,4,5,6,6,7,7,7,8,9},i,j;
int last_dup = -1;
for(i=0;i<15;i++)
{
for(j=i+1;j<14;j++)
{
if(numbers[i]==numbers[j] && numbers[i]!=last_dup)
{
printf("Number %d has duplicate values with indexes i=%d, j=%d\n",numbers[i], i,j);
last_dup = numbers[i];
break;
}
}
}
return 0;
}
I initialized to -1 assuming your numbers are all positive. If not you can save the index of the last duplicate and reference using in the check numbers[last_dup_idx] (be careful to initialize the value properly and not go out the length of the array). I also added a break: since you already found a duplicate there is no need to continue the second loop.
If your array is not ordered, you can create an additional array saving the duplicates elements every time you find one and then iterate through this array to check.
FOR NOT ORDERED ARRAYS
#include<stdio.h>
int main()
{
int numbers[15]={1,1,2,2,2,3,4,5,6,6,7,7,7,8,9},i,j;
int dups [7];//at most 15/2 duplicates
char dup_idx=0;
char found_new_duplicate; //initialized later
for(i=0;i<15;i++)
{
for(j=i+1;j<14;j++)
{
if(numbers[i]==numbers[j])
{
found_new_duplicate = 1;
//check in the duplicates array
for (int kk=0; kk<=dup_idx; kk++)
{
if (dups[kk]==numbers[i] && !(i==0 && j==1))
{
found_new_duplicate = 0;
break;
}
}
if (found_new_duplicate)
{
dups[dup_idx] = numbers[i]; //insert the new duplicate in the duplicates array
printf("Number %d has duplicate values with indexes i=%d, j=%d\n",numbers[i], i,j);
printf("New duplicate %d\n", dups[dup_idx]);
dup_idx++;//increase the index
break;
}
}
}
}
for (int kk=0; kk< dup_idx; kk++)
printf("%d\n", dups[kk]);
return 0;
}
Given:
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
I want to split the 2d array (struct MATRIX) into the an array of struct MATRIX
given a chunksize CS:
assume cs to be 2,
the answer would be
Seg[0]:
1 2
1 2
1 2
Seg[1]:
3 4
3 4
3 4
....
Seg[3]:
7 8
7 8
7 8
Here is my Matrix Struct:
typedef struct MATRIX {
int nrow;
int ncol;
int **element;
} MATRIX;
and here is the function the seperates them:
void SegmentMatrix(MATRIX input,MATRIX* segs,int Chunksize, int p) {
int i,j,r;
//Allocate segs
for (i = 0; i<p;i++)
{
CreateMatrix(&(segs[i]),input.nrow ,Chunksize,0);
}
//Now Copy the elements from input to the segs
//where seg0 takes from 0 to cs cols of a, and all their rows, and seg1 takes from cs to 2cs ...
printf("Stats:\n\t P: %d\t CS: %d\n",p,Chunksize);
for (r = 0; r<p; r++) {
for (i = 0; i<input.nrow;i++) {
for (j = r*Chunksize; j<r*Chunksize+Chunksize-1; j++) {
//I tried (&(segs[r]))->element... Doesn't work, produces wrong data
segs[r].element[i][j] = input.element[i][j];
}
}
PRINTM(segs[r]);
}
}
Note that PRINTM basically prints the matrix, it knows the limits by checking segs[r].nrow and ncol
and CreateMatrix takes the following inputs (&matrix, number of rows, number of colums, filltype) and mallocs from within.
filltype:
0- generates zeroth matrix
1- generates identity
else A[i][j] = j; for simplicity
The problem is that the if i print the matrices Segs[i], they all come down with their default value given by CreateMatrix, and not the newly added values.
CLARIFICATION:
Okay, so if you guys check that last PRINTM in SegmentMatrix function, it outputs the matrices as if the for loops didn't happen, aka, i can delete the for loops and would get the same output..
did i do something wrong in this line (taken from the SegmentMatrix)
Segs[r].element[i][j] = input.element[i][j];
I don't see why and what you are manipulating with multiplication by ChunkSize and r (which is uninitialized anyway), I'd suggest simplifying the code (rule of thumb: if it seems messy, it's too complex). All you need is a 3-dimensional array to store the array of chunks, and modulo arithmetic plus integer division to insert into the appropriate column of the appropriate chunk:
/* the variable-sized dimension of the `chunks' argument is w / chsz elements big
* (it's the number of chunks)
*/
void split(int h, int w, int mat[h][w], int chsz, int chunks[][h][chsz])
{
/* go through each row */
for (int i = 0; i < h; i++) {
/* and in each row, go through each column */
for (int j = 0; j < w; j++) {
/* and for each column, find which chunk it goes in
* (that's j / chsz), and put it into the proper row
* (which is j % chsz)
*/
chunks[j / chsz][i][j % chsz] = mat[i][j];
}
}
}
Demonstration, a. k. a. how to call it:
int main(int agrc, char *argv[])
{
const size_t w = 8;
const size_t h = 3;
const size_t c = 2;
int mat[h][w] = {
{ 1, 2, 3, 4, 5, 6, 7, 8 },
{ 1, 2, 3, 4, 5, 6, 7, 8 },
{ 1, 2, 3, 4, 5, 6, 7, 8 }
};
int chunks[w / c][h][c];
split(h, w, mat, c, chunks);
for (int i = 0; i < w / c; i++) {
for (int j = 0; j < h; j++) {
for (int k = 0; k < c; k++) {
printf("%3d ", chunks[i][j][k]);
}
printf("\n");
}
printf("\n\n");
}
return 0;
}
Question was unclear . so i thought he wanted just to know how to achieve this.
So i wrote this simple Pseudo code . Otherwise accept my apologize :
matrix[i] matrix
//matrixes total column size should be bigger big 2d array column size
first condition check: sum(matrix[i].colsize)>=big2d.colsize
//in this simple code raw sizes must be equal
second condition: for all i matrix[i].rawsize=big2d.rawsize
//if columns sizes will be equal the algorithm could be simplified , does not mean optimized
//splitting big2d into matrixes
for (int br=0;br<big2d.rawsize;br++){
i=0;//store matrix index
int previndex=0;//store offset for next matrix
for(int bc=0;bc<big2d.colsize;bc++){
matrix[i].val[bc-previndex][br]=big2d.val[bc][br]; //assign (bc,br)
if(bc-previndex==matrix[i].colsize-1){
i++; //move to next matrix;//if we not have next matrix then break;
previndex=bc+1;
}
/*if it be for equal chunks matrixes offset can be calculated this way too
matrix[bc/chunk].val[bc%chunk][br]=big2d.val[bc][br];
*/
}//loop columns
}//loop raws