How to set array B[0..n][0..k]. Help Please - c

I have to implement code to find the Binomial Coefficient using dynamic programming. But I don't know how to set the array B. Here the code I have:
#include <stdio.h>
int minimum(int a, int b) { return (a < b) ? a : b; }
main() {
int n = 50, k;
printf("Enter value for k:");
scanf("%d", &k);
printf("Value of coefficient %d, %d is: %d\n", n, k, bin2(n, k));
return 0;
}
// code to be implemented
int bin2(int n, int k) {
int i, j;
// array to initialize. Help
int B[0..n][0..k];
for (i = 0; i <= n; i++)
for (j = 0; j <= minimum(i, k); j++)
if (j == 0 || j == i)
B[i][j] = 1;
else
B[i][j] = B[i - 1][j - 1] + B[i - 1][j];
return B[n][k];
}

You can use the malloc() and calloc() functions from <stdlib.h> to allocate memory on the fly. Since you want to be able to store indices from 0 to n and 0 to k, you have to create a (n+1) × (k+1) array. This is done by allocating an array of n+1 int* pointers, then assigning an array of k+1 int values to each pointer:
/* Initialize array */
B = malloc((n+1) * sizeof(int*));
for (i=0; i<=n; i++) B[i] = calloc((k+1), sizeof(int));
When your function exits, this memory will still be allocated, but you will no longer be able to access it because the value of B is local to the bin2() function and will be lost forever. (This is called a memory leak.)
So you need to free up this memory before returning:
int result = B[n][k];
/* Dispose of array */
for (i=0; i<=n; i++) free(B[i]);
free(B);
return result;
}
(By the way, this isn't dynamic programming you're asking about, it's dynamic memory allocation. And there are better ways of calculating binomial coefficients.)

Related

Merge Sort in C using Recursion

This is my code for merge sort in C. I'm not able to understand what the issue is here. My knowledge of pointers is not that much. The merge function takes in 2 arrays and merges them. the sort function is a recursive function which is supposed to sort the array.
int * merge(int *fir, int n, int *sec, int m){
int res[m+n];
int x=0, y=0;
for(int i = 0; i < m+n; i++){
if(*(fir+x)<=*(sec+y)){
res[i] = *(fir+x);
x++;
}else{
res[i] = *(sec+y);
y++;
}
}
return res;
}
int * sort(int A[], int n){
if(n == 1){
return A;
}
int mid = n/2;
int AL[mid], AR[n-mid];
for(int i = 0; i < mid; i++){
AL[i] = A[i];
}
for(int i = 0; i < n-mid; i++){
AR[i] = A[i+mid];
}
int *BL, *BR;
BL = sort(AL, mid);
BR = sort(AR, n-mid);
return(merge(BL, mid, BR, n-mid));
}
int main(){
int n;
scanf("%d", &n);
int A[n];
for(int i = 0; i < n; i++){
scanf("%d", &A[i]);
}
int *sortedArray;
sortedArray = sort(A, n);
for(int i = 0; i < n; i++){
printf("%d ", *(sortedArray+i));
}
return 0;
}
And this is the output
q8.c:16:9: warning: address of stack memory associated with local variable 'res' returned [-Wreturn-stack-address]
return res;
^~~
1 warning generated.
7
23 12 56 67 11 99 97
97 32766 539779418 32767 -2002825496 32767 6 %```
There are two issues here: First, you merge your partial arrays into a temporary local array, which yoes out of bounds after you return from merge. The pointer you return points to invlid memory. That's what the warning about.
Second, you don't check whether you are reading beyond the limits of the partial arrays when you merge: The condition x < n must be true when you access fir, likewise for y < m and sec.
You are also causing confusion by returning a pointer to the first element of the sorted or merged arrays. That suggests that you create new sorted and merged arrays (and that's what you try to do in merge). This is okay for garbage-collected morern languages, but C doesn't work that way.
In C, if you need new memory, you must allocate it and later free it explicitly. In a recursive function like yours, this is tedious, because you are interested only in the final sorted array, not in the intermediate results. Therefore, C sorting algorithms usually work "in place": The same memory is used thoughout the sorting and elements are swapped. The original order of the elements is lost unless you make a copy before sorting.
For merge sort, you need auxiliary memory. In your case, you use the temporary arrays AL and AR, which are copies of the contents of the original array, A. Now when you merge, you can merge AL and AR back into A.
So istead of creating a ephemeral local array, pass in A so that it can be filled with the sorted elements:
void sort(int A[], int n)
{
if (n > 1) {
int mid = n / 2;
int AL[mid], AR[n - mid];
for (int i = 0; i < mid; i++) AL[i] = A[i];
for (int i = 0; i < n - mid; i++) AR[i] = A[i + mid];
sort(AL, mid);
sort(AR, n - mid);
merge(A, AL, mid, AR, n - mid);
}
}
Your merge function is now very similar to the one you has before, only that you have the result array as parameter and that you must catch the out-of-bound cases before accessing elements with [].
void merge(int *res, const int *fir, int n, const int *sec, int m)
{
int x = 0, y = 0;
for(int i = 0; i < m + n; i++) {
if (x == n) res[i] = sec[y++];
else if (y == m) res[i] = fir[x++];
else if (fir[x] <= sec[y]) res[i] = fir[x++];
else res[i] = sec[y++];
}
}

Allocating memory to 2D array using an array of NULL (c)

thanks for taking the time in reading this.
In my question a "vector" is defined as a 1D dimensional array of integers.
Therefore an array of vectors would be a 2D dimensional array in which every vector can be of a different length.
I'm asked to use:
int** vectors- the 2D array
int size -an integer that represents how many vectors exist inside **vectors
int* sizes-a 1D array of integers that represents the length of the vectors
for example,for:
vectors = {{4,3,4,3},{11,22,33,44,55,66},NULL,{5},{3,33,333,33,3}}.
size is 5 (there are 5 vectors inside vectors).
sizes is {4,6,0,1,5} (4 is the length of the first vector and so on).
size is inputted by the user at the beginning of main() and **vectors&*sizes are dynimacilly allocated with size's value.
I'm asked to write the function:
int init(int ***vectors, int **sizes, int size) which initializes **vectors to be an array of NULLs and *sizes to be an array of zeros.
I came up with this code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int init(int*** vectors, int** sizes, int size)
{
int i, k,j;
printf("check\n");
*vectors = (int**)malloc(size * sizeof(int*));
if (*vectors == NULL)
return 0;
for (i = 0; i < size; i++)
{
*(vectors + i) = NULL;
}
printf("check 2\n");
for (k = 0; k<size; k++)
{
if (*(vectors+k) != NULL)
printf("didn't work\n");
else
printf("current is null\n");
}
*sizes= (int*)malloc(size * sizeof(int));
if (*sizes == NULL)
return 0;
for (j= 0; j < size; j++)
{
*(sizes + j) = 0;
printf("%d ", *(sizes + j));
}
printf("\n");
return 1;
}
int main()
{
int size, i;
int** vectors = NULL;
int* sizes = NULL;
printf("\nPlease enter an amount of vectors:\n");
scanf("%d", &size);
printf("%d\n", init(&vectors, &sizes, size));
printf("size is %d now\n", size);
// for (i = 0; i < size; i++)
// printf("%d ", *(sizes+i));
printf("check 3\n");
free(sizes);
free(vectors);
printf("check 4\n");
printf("check 5\n");
return 0;
}
forgot to mention that init returns 0 if it fails to allocate memory and 1 otherwise.
printing the "checks" was so I could see where the program fails.
the problem is that no matter what,after printing the last check (check 5)
the program fails.(Run-Time Check Failure #2)
if anyone could help me understand what I'm doing wrong I would HIGHLY appreciate it.
thanks alot for reading and have an amazing day.
edit:
i also printed the array sizes/vectors inside init just to see if it prints zeros/nulls,i don't actually need to do it.
One problem of OP's code is in the pointer arithmetic. Given:
int ***vectors;
*vectors = malloc(size * sizeof(int*));
This loop:
for (i = 0; i < size; i++)
{
*(vectors + i) = NULL;
}
Would iterate over the next unallocated pointer to pointer to pointer to int, while what the OP needs is
for (i = 0; i < size; i++)
{
*(*vectors + i) = NULL; // or (*vectors)[i] = NULL;
}
The same holds in the following loops, where *(sizes + j) is used instead of *(*sizes + j) (or (*sizes)[j]).

C - free function crashes

I tried checking the addresses and its all looking normal, printing works and everything else works as well.
But when it tries to free the memory, the program crashes.
CODE:
#include <stdio.h>
#include <stdlib.h>
#define FIRST_DIM 2
#define SECOND_DIM 3
#define THIRD_DIM 2
#define TOTAL 12
void getNums(float* pArr);
void sortArr(float* pArr);
void printArr(float* pArr);
int main(void)
{
// Variable assignment
unsigned int i = 0, j = 0;
float*** pppArr = NULL;
// Memory assignment
pppArr = (float***)calloc(FIRST_DIM, sizeof(float**));
if (!(pppArr))
{
printf("Failed Allocating Memory..\n");
system("PAUSE");
return 1;
}
for (i = 0; i < FIRST_DIM; i++)
{
*(pppArr + i) = (float**)calloc(SECOND_DIM, sizeof(float*));
if (!(*(pppArr + i)))
{
printf("Failed Allocating Memory..\n");
system("PAUSE");
return 1;
}
}
for (i = 0; i < FIRST_DIM; i++)
{
for (j = 0; j < SECOND_DIM; j++)
{
*(*(pppArr + i) + j) = (float*)calloc(THIRD_DIM, sizeof(float));
if (!(*(*(pppArr + i) + j)))
{
printf("Failed Allocating Memory..\n");
system("PAUSE");
return 1;
}
printf("%p && %d\n", *(*(pppArr + i) + j), **(*(pppArr + i) + j));
}
}
printf("ASD");
// Getting numbers, sorting them and printing them out
getNums(**pppArr);
sortArr(**pppArr);
printArr(**pppArr);
// End of program
// Releasing memory
for (i = 0; i < FIRST_DIM; i++)
{
for (j = 0; j < SECOND_DIM; j++)
{
free(*(*(pppArr + i) + j));
}
}
for (i = 0; i < FIRST_DIM; i++)
{
printf("%p\n", *(pppArr + i));
free(*(pppArr + i));
}
free(pppArr);
system("pause");
return 0;
}
/* This function gets values for the given array from the user */
void getNums(float* pArr)
{
unsigned int i = 0;
for (i = 0; i < TOTAL; i++)
{
printf("Enter Number %d: ", i);
scanf("%f", pArr + i);
getchar();
}
}
/* This function prints out the given array */
void printArr(float* pArr)
{
unsigned int i = 0;
for (i = 0; i < TOTAL; i++)
{
printf("Number %d: %.2f\n", i, *(pArr + i));
}
}
/* This function sorts the given array from lowest to highest*/
void sortArr(float* pArr)
{
unsigned int i = 0, j = 0;
float temp = 0;
for (i = 0; i < TOTAL; i++)
{
for (j = 0; j < TOTAL - 1; j++)
{
if (*(pArr + j) > *(pArr + j + 1))
{
temp = *(pArr + j);
*(pArr + j) = *(pArr + j + 1);
*(pArr + j+ 1) = temp;
}
}
}
}
is there something im missing?
When you use pointers and dynamic allocation, the memory you allocate will most likely not be contiguous, but you will get separate memory areas for each allocation. That means when you treat it as one large contiguous memory area in your functions you exhibit undefined behavior.
An actual array, like
float arr[FIRST_DIM][SECOND_DIM][THIRD_DIM];
Now that will be contiguous.
See e.g. this old answer of mine for a more "graphical" explanation of the difference between arrays of arrays, and pointers to pointers.
This code is indexing out of bounds rather severely. The memory allocated for pppArr has three levels of indirection. It is not a multi-dimensional array (i.e., not an array of arrays), but is a pointer to an array of pointers to arrays of pointers. Three levels of indirection. There are 6 separate float slices, each with 2 contiguous elements (i.e., THIRD_DIM).
When calling getNums, sortArr, and printArr, you are passing **pppArr. This is equivalent to passing pppArr[0][0], which points to a single 2-element sequence of float values. The functions may only access THIRD_DIM elements, i.e. 2. Instead, they are trying to access 12 elements (i.e., TOTAL), which of course corrupts memory and leads to undefined behavior.
As far as I can tell, it looks like you're trying to access the storage as a single one-dimensional array. If so, then the easiest way to fix it is to eliminate 2 levels of indirection, and get rid of FIRST_DIM, SECOND_DIM, and THIRD_DIM, and just allocate a single, flat array with LENGTH elements. The pointer would have type float *.

Segmentation Fault while allocating memory at run time using malloc [duplicate]

This question already has answers here:
How do I work with dynamic multi-dimensional arrays in C?
(9 answers)
Closed 8 years ago.
This is my code. My purpose is to allocate memory to a 2D array at run time upto whatever size is given in input.
Why is segmentation fault occuring? Is it because array elements have to be stored consecutively and malloc(dynamic allocation) is not letting this happen?
OR I am doing some error in writing this code. Please guide me through.
Thanks in advance.
int main(){
// STEP 1
int size,**arr,i=0,j=0;
printf("Enter the size of matrix : ");
scanf("%d",&size);
// STEP 2
arr = (int**)malloc(size*size*sizeof(int));
printf("\n Enter the %d elements : \n",size*size);
for(i=0;i<size;i++){
for(j=0;j<size;j++){
// STEP 3
scanf("%d",&arr[i][j]);
}
}
/*
for(i=0;i<size;i++){
for(j=0;j<size;j++){
printf("%d\n",matrix[i][j]);
}
}
*/
return 0;
}
This is a classic mistake.
A pointer to pointers is actually not the same as a two-dimensional array.
Granted, you can access elements of both via the var[x][y] syntax, but the memory layout of
int foo[x][y]
is different from
int **bar
If you really want to have this dynamic, you will have to allocate space for your list of pointers, then allocate space for your elements to each pointer in turn.
bar = malloc( x * sizeof(int*) );
for ( int i = 0 ; i < x ; i++ )
bar[i] = malloc( y * sizeof(int) );
If at all possible, you should try and avoid this in favor of an actual two-dimensional array, which as of C99 you can declare on the stack even if its size is determined at runtime:
int main()
{
int n;
scanf("%d", &n);
int array[n][n];
// ...
return 0;
}
You should allocate like this:
arr = malloc(size * sizeof(int*));
for (int i = 0; i <size; i++)
arr[i] = malloc(size * sizeof(int));
And do not forget to free the memeory using free.
Side Note: Do not cast the return value of malloc.
One idea that would work and would also get you rid of the memory fragmentation induced by this double pointers level allocation is make your matrix linear:
arr = (int*) malloc (size*size*sizeof(int));
And then simply access your elements with arr[i*size + j] instead of arr[i][j]:
use it like this : a perfect example of dynamic memory Allocation
void mxmult()
{
int n,m,a,b,c,d, sum=0;
int x,y,z;
printf("Enter first order [n*n]\n");
scanf("%d", &n);
printf("Enter second order [m*m]\n");
scanf("%d", &m);
if (n!=m)
{
printf("Invalid orders");
}
else
{
//mem allocate for matrix 1
int **mat1 = (int**)malloc(n*sizeof(int));
for(x=0;x<n;x++)
{
mat1[x]=(int*)malloc(n*sizeof(int));
}
// input matrix 1
printf("Enter the first matrix entries\n");
for (a = 0; a <n; a++)
{
for (b = 0; b < n; b++)
{
scanf("%d", &mat1[a][b]);
}
}
// memory allocate matrix 2
int **mat2 = (int**)malloc(m*sizeof(int));
for(y=0;y<n;y++)
{
mat2[y]=(int*)malloc(m*sizeof(int));
}
//inpur matrix 2
printf("Enter the second matrix entries\n");
for (c = 0; c <n; c++)
{
for (d= 0; d < n; d++)
{
scanf("%d", &mat2[c][d]);
}
}
//Memory allocate matrix Mult
int **mult=(int**)malloc(m*sizeof(int));
for(z=0;z<m;z++)
mult[z]=(int*)malloc(m*sizeof(int));
for (a = 0; a < n; a++)
{
for (d = 0; d < m; d++)
{
for (c = 0; c < n; c++)
{
sum=sum + (mat1[a][c] *mat2[c][d]);
}
mult[a][d] = sum;
sum= 0;
}
}
printf("Product\n");
for ( a = 0 ; a < n ; a++ )
{
for ( d = 0 ; d < m ; d++)
printf("%d\t", mult[a][d]);
printf("\n");
}
}
}

Unknown error in the memory in C

I have a 2D dynamic array.
I enter a line of 0's after line which has a biggest number:
void InsertZero(int **a, int pos){
int i, j;
a = (int**)realloc(a, n * sizeof(*a));
a[n-1] = (int*)calloc(n, sizeof(**a));
d = 0;
for(i = n-1; i > pos; i--){
for(j = 0; j < n; j++){
a[i][j] = a[i-1][j];
printf("%d ", a[i][j]);
}
}
for(i = 0; i < n; i++){
a[pos][i] = 0;
}
}
If i make a size of array 3, 5, 7, 9, ... it works correctly. But if a number of lines is 2, 4, 6, ... , it is an access violation error, when i try to print my array:
void Print(void){
int i, j;
for(i = 0; i < (n-d); i++){
for(j = 0; j < n; j++){
printf("%d\t", arr[i][j]);
}
printf("\n");
}
}
code: http://codepad.org/JcUis6W4
Looking at this I cannot make sense of.... Look at comment 1, you have n set somewhere to realloc a block of memory which a is of type int ** - a double pointer, how are you calling this function? Secondly, comment 2, Why did you call calloc when the realloc on a double pointer was called previously...? Assume n has value of 5, then, realloc is called on double pointer a, meaning a[0][1]..a[4][1], now calloc is called thus a[4] has a new block of memory...
void InsertZero(int **a, int pos){
int i, j;
/* 1. */
a = (int**)realloc(a, n * sizeof(*a));
/* Bzzzzt....if realloc failed, a gets overwritten! */
/* 2. */
a[n-1] = (int*)calloc(n, sizeof(**a));
/* 3. */
d = 0;
/* 4. */
for(i = n-1; i > pos; i--){
for(j = 0; j < n; j++){
a[i][j] = a[i-1][j];
printf("%d ", a[i][j]);
}
}
for(i = 0; i < n; i++){
a[pos][i] = 0;
}
}
Comment 3, what is d used for - useless variable?
Comment 4, you are under the presumption that the block of memory has array subscripts [0][0] to [4][4] if n had a value of 5!
Can you clarify all this?
Edit: Looking at it again... it is likely that a got overwritten when the call to realloc failed! I recommend this section of code to counteract this
int **tmpA;
tmpA = (int**)realloc(a, n * sizeof(*a));
if (tmpA != NULL){
a = tmpA;
....
a[n-1] = (int*)calloc(n, sizeof(**a));
for(i = n-1; i > pos; i--){
....
}
for(i = 0; i < n; i++){
....
}
}
In your function InsertZero you have a local variable a. This local variable is initially set with a the address of a pointer to an integer (you probably wanted the address of a pointer to an array of integers, i.e. int ***a).
When you call realloc you are assigning your local copy of a a pointer to a block of memory, however once you have finished with your function it is entirely possible for your local copy of a to be pointing somewhere different to the rest of your program. You probably wanted to say *a = (int **)realloc(a, n * sizeof(int *));.
Dangerously, you're using n which isn't passed to your function. It appears you've made the assumption that n is going to be 1 bigger than the previous size of the array - otherwise your call to calloc is superfluous and you are just rotating the array letting the last element drop off as a memory leak.
Let's use a simpler example with no arrays, no dimensions. Let's say you wanted to create a function:
void make_me_a_pointer( int **mynumber ) {
*mynumber = (int *)malloc( sizeof(int) );
**mynumber = 7; /* assign the value 7 to my allocated memory */
}
int main( void ) {
int *demoint;
make_me_a_pointer( &demoint );
printf( "Magic num is %d\n", *demoint );
}
However in your case you merely assigned a = realloc... and thus never communicated the new address of a outside your function.

Resources