Question

C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like...

C PROGRAM

the functions create and free a dynamically allocated 2D array. It should function like a statically allocated array (use malloc())

int **createArray(int rows, int cols)

parameters to this function are the number of rows and cols in the array. The return value is the array that you have dynamically allocated

void freeArray (int **array, int rows, int cols)

the parameters to this function are:

-the array created by createArray()

-the number of rows in the array

-the number of cols in the array

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

#include <stdio.h>

#include <stdlib.h>

int **createArray(int rows, int cols);

void freeArray (int **array, int rows, int cols);

int main()

{

int i,j;

int **arr;//declare the array pointer

arr=createArray(3,4);//callong createArray function

for (i = 0; i < 3; i++) //print the array

{

for (j = 0; j <4; j++)

printf("%d ", arr[i][j]);

printf(" ");

}

freeArray(arr,3,4);//calling the free array function

}

int **createArray(int rows,int cols)

{

int i,j,count;

int **arr = (int **)malloc(rows * sizeof(int *));

for (i=0; i<rows; i++)

arr[i] = (int *)malloc(cols * sizeof(int));

  

  

count = 0;

for (i = 0; i < rows; i++)

for (j = 0; j < cols; j++)

arr[i][j] = ++count;

return arr;

}

void freeArray (int **array, int rows, int cols)

{

int i;

for(i = 0; i <rows ; i++)

free(array[i]);

free(array);

}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like...

    C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like a statically allocated array (use malloc()) int **createArray(int rows, int cols) parameters to this function are the number of rows and cols in the array. The return value is the array that you have dynamically allocated void freeArray (int **array, int rows, int cols) the parameters to this function are: -the array created by createArray() -the number of rows in the array -the number...

  • Write a C program that uses mallloc to create a 2D array. of c columns and...

    Write a C program that uses mallloc to create a 2D array. of c columns and r rows. Elements of array must be initialized to 0 and you must use malloc and free allocated memory fo array.

  • In Java Please Create A Program For The Following; Please Note: This program should be able...

    In Java Please Create A Program For The Following; Please Note: This program should be able accept changes to the values of constants ROWS and COLS when testing the codes. Switch2DRows Given a 2D array with ROWS rows and COLS columns of random numbers 0-9, re-order the rows such that the row with the highest row sum is switched with the first row. You can assume that 2D arrau represents a rectangular matrix (i.e. it is not ragged). Sample run:...

  • Purpose The purpose of the lab is to learn how to pass pointers in C. Process Create the function...

    Programming in C Purpose The purpose of the lab is to learn how to pass pointers in C. Process Create the functions as defined in the following table. Each function will add the last two integer parameters together Function PrototypeReturr int add2a(int,int); Returns the sum as a return value void add2b(int* int,int); Returns the sum in the first parameter int add2c(int*,int,int); Returns the sum as a return value and in the first parameter Returns the sum as a pointer to...

  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...

  • USING C++: Write a function that given a 2D dynamic array of integers, will return the...

    USING C++: Write a function that given a 2D dynamic array of integers, will return the number of elements whose absolute value is smaller than a given value x. The function should take as arguments (in this order): a pointer to the array (i.e., 2D dynamic array) the number of rows the number of columns the given value The function should return the number of elements smaller in absolute value than epsilon E.g. if A={{0.2, -0.1, 3.4},{4.4, 0, -2}} and...

  • In this assignment, you are asked to: 1. create a Matrix class that stores and operate...

    In this assignment, you are asked to: 1. create a Matrix class that stores and operate on a dynamic two-dimensional array. The class has the following structure: Private member variables: - int ** mat; - int rows; - int cols; Public member functions: +Default constructor: sets both rows and cols to 3 and creates 3x3 matrix dynamically +Parameterized constructor: sets the rows and cols to the values passed to the constructor and create a matrix with the given dimensions. +Destructor:...

  • Use C: 3. In function main, declare a two-dimensional integer array with 5 rows and 4...

    Use C: 3. In function main, declare a two-dimensional integer array with 5 rows and 4 columns. Call the following functions from function main. Call a function createArray to seed the random number generator and to fill the two-dimensional array with random integers between -20 and +20. Parameters for this function should be the array and the number of rows and columns). Call a function signs to count the number of positive values, number of negative values and number of...

  • -Create a function output() in C that takes the pointer to the array and its size...

    -Create a function output() in C that takes the pointer to the array and its size and prints the arrays' contests. (malloc format) (function prototype : void output(int *arrayPtr, int size); -Create a function check() in C that takes the pointer to the array and a number and checks if the number is in the array. If the number is in the array, returns the index of the element of the array holding the number. Otherwise, returns -1.

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT