I want to save the cartesian product of two arrays into one multidimentional array.
arr[number][0] - number from the first array
arr[number][1] - number from the second array
number = sizeof(array1)*sizeof(array2)
int main() {
int arr1[4] = {1, 4, 2, 3};
int arr2[4] = {4, 1, 3, 3};
int **outcomes = getProduct(arr1, 4, arr2, 4);
int i;
for(i = 0; i < 16; i++)
printf("%d", outcomes[i][0]);
getchar();
}
int* getProduct(int arr1[], int size1, int arr2[], int size2) {
int **outcomes = (int *)malloc(sizeof(int)*16), count = 0, i, j;
for(i = 0; i < size1; i++) {
for(j = 0; j < size2; j++) {
outcomes[count][0] = arr1[i];
outcomes[count][1] = arr2[j];
count++;
}
}
return outcomes;
};
However, this code strangly breaks while being in the nested loop inside getProduct() function. The logic is ok, I thing the problem might be with pointers.
Why do I need to initialize multidimentional arrays with two pointers?
Why isn't this code working?
Do u have any advices for me on how can I improve this code? ( I didn't code in C too much so I don't know which structures a "good" code uses).
The function getProduct would fail because you are assigning a pointer to an int( ie int*) to a pointer to an int pointer (ie int**).
Why do I need to initialize multidimentional arrays with two pointers?
Because
int** (pointer to int pointer) - with this, we can access an element of any row and any column which is not possible with a single pointer(or single dimensional array)
Similarly int*** can be used for three dimensional array or in other words pointer to pointer to an int pointer
To improve the code
I think your cartesian product function should look something like this
int** getProduct(int arr1[], int size1, int arr2[], int size2) {
int **outcome = malloc(sizeof(int*)*size1);
int i,j;
for(i = 0;i<size1;i++)
outcome[i] = malloc(sizeof(int)*size2);
for(i = 0; i < size1; i++) {
for(j = 0; j < size2; j++) {
outcome[i][j] = arr1[i] * arr2[j];
}
}
return outcome;
}
And your main() like this
int main() {
int arr1[4] = {1, 4, 2, 3};
int arr2[4] = {4, 1, 3, 3};
int **outcomes = getProduct(arr1, 4, arr2, 4);
int i,j;
for(i = 0; i < 4; i++){
for(j = 0;j <4; j++){
printf("%d ", outcomes[i][j]);
}
printf("\n");
}
getchar();
}
Related
I have a problem. I have to divide array into dynamic array and return pointer with parameter.
When I try to run this code, I get (interrupted by signal 11: SIGSEGV) this message. I think it is something with my pointers. But I don't even get any warnings, I don't know where to look else.
#include <stdio.h>
#include <stdlib.h>
int splitData(int data[], int size, int splitPlace, int **firstArray);
int main() {
int data[6] = {1, 2, 3, 4, 5, 6};
int size = 6;
int *ptr = NULL;
int n = splitData(data, size, 3, &ptr);
printf("%d", n);
for(int i = 0; i < 3; ++i)
{
printf("[%d]", ptr[i]);
}
return 0;
}
int splitData(int data[], int size, int splitPlace, int **firstArray)
{
*firstArray = (int *)malloc(splitPlace * sizeof(int));
for(int i = 0; i < splitPlace; ++i)
{
*firstArray[i] = data[i];
}
return 0;
}
You have the precedence wrong with *firstArray[i]. You need (*firstArray)[i].
Clearer might be to allocate
int *new_array = malloc(...);
*firstArray = new_array.
Then use new_array in your loop body.
For my studies, I have to write a C function GetEvenNumber:
parameters: array with n integers + array size;
returns tr array which contains even integers from td.
I don't know a priori the length of the array tr.
My below code returns errors:
#include <stdio.h> // define the header file
int *GetEvenNumber(int t[], int size)
{
int tr[];
int j = 0;
for (int i = 0; i < size; i++)
{
if (t[i] % 2 == 0)
{
printf("%d is even \n", t[i]);
tr[j] = t[i];
j++;
}
}
return tr;
}
int main() // define the main function
{
int *t; // = {4, 3, 1, 8, 6 };
int *tr = GetEvenNumber(t, 5);
for (int i = 0; i < 5; i++)
printf("%d \n", tr[i]);
}
I get error:
error: array size missing in 'tr'
int tr[];
warning: function returns address of local variable [-Wreturn-local-addr]
return tr;
How do I fix that? Thanks.
You mentioned that you could not use malloc() to dynamically create tr within GetEvenNumber() to address the two issues raised by your copmiler. This leaves making tr a global variable, or as here pass in the result array tr to be filled out:
#include <stdio.h>
#include <stdlib.h>
void GetEvenNumber(size_t size, const int *td, size_t *size2, int *tr) {
*size2 = 0;
for(size_t i=0; i<size; i++)
if(td[i] % 2 == 0)
tr[(*size2)++] = td[i];
}
int main() {
int td[] = {4, 3, 1, 8, 6 };
size_t size = sizeof(td) / sizeof(*td);
int tr[size];
size_t size2;
GetEvenNumber(size, td, &size2, tr);
for (size_t i=0; i < size2; i++)
printf("%d \n", tr[i]);
}
If the input array td contains uneven elements, then the result array tr have fewer valid elements than the input. I used size2 here to tell caller how many elements are valid in tr. Your code did not assign any values to, in this example, last 3 elements. You don't tell us what should happen with those last elements.
In modern C, if you specify the size before the array in the argument, then you can use the size in array specification which help document what is going on.
The error is due to
int tr[];
because you have to specify the size of your array during its creation.
I suggest trying to add a function that returns the number of even numbers in the array:
int getEvenNum(int t[], int lent){
int numEven = 0; // initialize counter to zero
for(int i = 0; i < lent; i++){ // for each element of the array
if ((t[i] % 2) == 0){ // if it's even,
numEven++; // add 1 to counter
}
}
return(numEven); // finaly returns the value of the counter
}
and then you replace the int tr[]; by int tr[getEvenNum(t, size)]; (maybe there's a ; after the getEvenNum(t, size) but I'm not sure)
Since the array tr can have AT MOST the same number of elements as the original integer array, it would be safe to declare the array with the same size as the array 't[]'.
I have made some changes to your code. Try the following:
#include<stdio.h> // define the header file
void GetEvenNumber(int *t, int* tr, int size, int *pCountEven)
{
int i, j=0;
for (i=0; i < size; i++)
{
if(t[i]%2==0)
{
printf("%d is even \n", t[i]);
tr[j++] = t[i];
}
}
*pCountEven = j;
}
int main() // define the main function
{
int t[] = {4, 3, 1, 8, 6 };
int tr[5], countEven = 0, i;
GetEvenNumber(t, tr, 5, &countEven);
for (i=0; i < countEven; i++)
printf("%d\n", tr[i]);
return 0;
}
Edit: As #chqrlie (who is an experienced coder) pointed out, we can simply return the length of the array instead of taking the address of a variable.
So alternatively, you can try this:
#include <stdio.h> // define the header file
int GetEvenNumber(int *t, int *tr, int size) {
int i, j = 0;
for (i = 0; i < size; i++) {
if (t[i] % 2 == 0) {
printf("%d is even \n", t[i]);
tr[j++] = t[i];
}
}
return j;
}
int main() // define the main function
{
int t[] = { 4, 3, 1, 8, 6 };
int tr[5], countEven = 0, i;
countEven = GetEvenNumber(t, tr, 5);
for (i = 0; i < countEven; i++)
printf("%d\n", tr[i]);
return 0;
}
I'm studying the C language and data structures. I wish to get some detailed explanation about why we cannot use dynamic arrays as parameters of functions that use static array parameters.
#include <stdio.h>
int sumAry2D_f1(int ary[][3], int rows, int cols);
void freeAry2D(int **ary, int rows);
int main(void)
{
int ary2D[][3] = { {1, 2, 3}, {4, 5, 6} }; // static array
int r, c;
int **ary = (int**)malloc(sizeof(int*) * 2); // dynamic array
for (r = 0; r < 2; r++)
ary[r] = (int*)malloc(sizeof(int)*3);
for (r = 0; r < 2; r++)
for (c = 0; c < 3; c++)
ary[r][c] = r + c; // 0, 1, 2, 1, 2, 3
printf("sumAry2D_f1() %d\n", sumAry2D_f1(ary2D, 2, 3));
// why we can`t function like this?
// printf("sumAry2D_f1~3() %d\n", sumAry2D_f1(ary, 2, 3));
freeAry2D(ary, 2); // free function of dynamic array
return 0;
}
int sumAry2D_f1(int ary[][3], int rows, int cols)
{
int i, j, sum = 0;
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
sum += ary[i][j];
return sum;
}
void freeAry2D(int **ary, int rows)
{
int i;
for (i = 0; i < rows; i++)
free(ary[i]);
free(ary);
}
// why we can`t function like this?
// printf("sumAry2D_f1~3() %d\n", sumAry2D_f1(ary, 2, 3));
Your function specifically expects a pointer to an integer pointer that points to a sequence of 3 integers, or an array of 3-element integer arrays, not just any pointer to integer pointer. ary is just a pointer to integer pointer. Although you made your int ** have the same structure as your int [][3], it could have been different and the compiler can't tell because the memory allocation occurs at run-time, hence if you uncomment the code it probably won't compile, and even if it does the behavior will be undefined.
Unfortunately, you would need 2 functions to do what you want:
int sumAry2D_f1(int ary[][3], int rows, int cols);
and
int sumAry2D_f12(int** ary, int rows, int cols);
The code inside each function would be the same.
I want max to have the content of tmp_max. max is dynamically allocated. tmp_max size is known.
Copying the values is working correctly when I hardcode it but it doesn't work when I create a function to copy the values. Why is that?
//This code works
int** max;
init2D(&max,3,4);
int tmp_max[3][4] = {{3,3,2,2}, {1,2,3,4}, {1,3,5,0}};
for(int i = 0 ; i < 3; i++)
for(int j = 0 ; j < 4; j++)
max[i][j] = tmp_max[i][j];
assert(max[0][1] == 3);
//This one crashes
void copyArray2D(int a, int b, int*** tab,int*** tab2){
for(int i = 0 ; i < a; i++)
for(int j = 0 ; j < b; j++)
tab2[i][j] = tab[i][j];
}
int** max;
init2D(&max,3,4);
int tmp_max[3][4] = {{3,3,2,2}, {1,2,3,4}, {1,3,5,0}};
copyArray2D(3,4,&tmp_max,&max); //crash
assert(max[0][1] == 3);
Note:
Using void copyArray2D(int a, int b, int** tab,int** tab2){and copyArray2D(3,4,max,tmp_max); isn't working either.
Using void copyArray2D(int a, int b, int** tab,int** tab2){and copyArray2D(3,4,&max,&tmp_max); isn't working either.
void init2D(int ***data_ptr, int x, int y) {
int **data = (int **) malloc(sizeof(int *) * x);
for (int k = 0; k < x; k++)
data[k] = (int *) malloc(sizeof(int) * y);
*data_ptr = data;
}
Your compiler should complain about copyArray2D(3,4,&tmp_max,&max).
You must fix any issues identified by your compiler before trying to run your code (running any such executable based on broken code is meaningless).
One problem is that copyArray2D says int*** when it should say int**.
After fixing that, the main issue here is that you have written copyArray2D so that it only works with an array of pointers. (You are simulating a 2-D array by allocating an array of pointers, and then making each pointer point to a separate allocation representing each row).
This works when you use init2D because the init2D function allocates an array of pointers etc.
However int tmp_max[3][4] is a block of 12 contiguous ints. There are no pointers. This is not compatible with copyArray2D.
Your options are:
Use int **tmp_max and use init2D to allocate it, instead of int tmp_max[3][4]
Make another version of copyArray2D which works on a contiguous 2-D array.
Use an ugly macro
Your second function has too many stars. You hadn't shown the init2D function when I first wrote an answer, so I had to guess what you'd done with that (but the code is now in the question and is close enough to what I produced that the difference is immaterial — except I do error check the allocations). Here's a (rewritten) version of the code (the first version hadn't been near a compiler, and I completely missed a crucial detail). Note that int ** is not the same as int arr[N][M] or variations on the theme — even if you use the same notation to access both.
#include <stdio.h>
#include <stdlib.h>
static void copyArray2D(int a, int b, int **dst, int src[a][b])
{
for (int i = 0; i < a; i++)
for (int j = 0; j < b; j++)
dst[i][j] = src[i][j];
}
static void oom(void)
{
fprintf(stderr, "Out of memory\n");
exit(1);
}
static void init2D(int ***arr, int a, int b)
{
(*arr) = malloc(a * sizeof((*arr)[0]));
if (*arr == 0)
oom();
for (int i = 0; i < a; i++)
{
(*arr)[i] = malloc(b * sizeof((*arr)[0][0]));
if ((*arr)[i] == 0)
oom();
}
}
static void dump_2d_array(int a, int b, int arr[a][b])
{
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
printf(" %2d", arr[i][j]);
putchar('\n');
}
}
static void dump_2d_pointers(int a, int b, int **arr)
{
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
printf(" %2d", arr[i][j]);
putchar('\n');
}
}
int main(void)
{
int **max;
init2D(&max, 3, 4);
int tmp_max[3][4] = { { 3, 3, 2, 2 }, { 1, 2, 3, 4 }, { 1, 3, 5, 0 } };
copyArray2D(3, 4, max, tmp_max);
printf("2D array:\n");
dump_2d_array(3, 4, tmp_max);
printf("List of pointers:\n");
dump_2d_pointers(3, 4, max);
return 0;
}
Output from running that code:
2D array:
3 3 2 2
1 2 3 4
1 3 5 0
List of pointers:
3 3 2 2
1 2 3 4
1 3 5 0
Note that I've not written a free function, so the memory is leaked.
Be wary of 3-Star Programming.
void copyArray2D(int a, int b, int tab[a][b],int*** tab2){
for(int i = 0 ; i < a; i++)
for(int j = 0 ; j < b; j++)
(*tab2)[i][j] = tab[i][j];
}
copyArray2D(3,4,tmp_max,&max);
I want to create a table with keys from an array. Is there an easy way to do it.
int array1[] = {1,5,3,8,9,11};
// table[1]
// table[5]
// table[3]
int count(int a)
{
//a is one of the values in array. array1[] = {1,5,3,8,9,11};
// for ex 3.
// I have to figure out how many times this function was called with what values 1/5/3/8/9/11
table[3]++;
}
A simple code
#include<stdio.h>
int n = 6; //number of elements in array1
int array1[] = {1,3,5,8,9,11};
int *funCount;//Count of elements in array1
int count(int a)
{
int i;
for(i = 0; i < n; i++)
if(a == array1[i])
break;
funCount[i]++;
}
int main()
{
funCount = (int*)calloc(n, sizeof(int));
int i;
count(1);
count(3);
count(5);
count(8);
count(9);
count(11);
for(i = 0; i < n; i++)
printf("%d ",funCount[i]);
return 0;
}
This approach is okay if your array1 is going to be small!
Otherwise I would recommend you to use hashing