Question

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.

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

ANSWER:-

#include <stdio.h>

int main(void) {

int r, c;

printf("enter the row : ");

scanf("%d",&r);

printf("enter the column : ");

scanf("%d",&c);

int *arr = (int *)malloc(r * c * sizeof(int));

int i, j, count = 0;

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

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

*(arr + i*c + j) =count;

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

{

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

{

printf("%d ", *(arr + i*c + j));

}

printf("\n");

}

free(arr);

return 0;

}

// OUTPUT

Success #stdin #stdout 0s 4216KB enter the row:3 enter the column3 0 e e 0 e e

// please vote

Add a comment
Know the answer?
Add Answer to:
Write a C program that uses mallloc to create a 2D array. of c columns and...
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...

  • 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 Java program that does the following. a. Declare an integer 2D array with 5...

    Write a Java program that does the following. a. Declare an integer 2D array with 5 rows and 5 columns. b. Initialize the array's elements to random integers between 1 and 10 (inclusive). c. Display all the elements in the 2D array as a table of rows and columns. d. Display the row index and column index of all the even integers in the 2D array. e. Display the sum of first row's elements.

  • Create a program that uses a Jagged Array. The program will create an array with 50...

    Create a program that uses a Jagged Array. The program will create an array with 50 rows. Each of the values for each element of the array will be randomly generated. The random values will be between 1 and 20. Depending on the value generated for each row, you will create an array with that number of columns for that row. Each column created will contain the value to the left plus 1. After you create and populate the entire...

  • In C++ for 2D Array: Write a program that uses a function that manipulates a 2D...

    In C++ for 2D Array: Write a program that uses a function that manipulates a 2D array of integers as follows: 1. Fill the first row and column in the array with random bits 2. Every subsequent cell should be filled as follows: 1. If the cells that are to the left and top of it are equal, then you should store their sum in it b. Otherwise, your program should store the highest among them. Test your function using...

  • Write a program in C++ that uses a class template to create a set of items....

    Write a program in C++ that uses a class template to create a set of items. . . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of...

  • *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create...

    *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create an array of doubles named “hummus” with 2 rows and 300 columns. Initialize all array elements to zero. Write a loop that will repeatedly ask the user to enter a value and store it in the first row of the array (do not overwrite previously entered values) until the whole first row is populated with the user input. (hint the loop should have the...

  • create a program with files, 1d array, 2d array, and vectors in c++ a single program

    create a program with files, 1d array, 2d array, and vectors in c++ a single program

  • 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...

  • Can anyone help me with my C hw? Exercise 3 You will write a new program...

    Can anyone help me with my C hw? Exercise 3 You will write a new program that combines dynamically allocating an array and saving that array to a file. These are the tasks your program must perform Open an output file named "data.txt" and prepare it for writing in text mode o If the file handle is NULL, quit the program o By default, it is created and stored in the same directory as your source code file Prompt the...

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