Question

C Programming 1. Write a void function that takes in a 2D array of character and...

C Programming

1. Write a void function that takes in a 2D array of character and have it print out each array on a new numbered line on the console.

2. Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to.

int main()
{
int myarray[3];
myfunction (myarray);
}

int myfunction(int* in)
{
int i;
for (i = 0; i<3; i+= 1)
{
in[i] = i;
}
// illustrate the stack and the heap at this point in the code
return 0;

}

Homework Answers

Answer #1

Hi,

1. Input 2D array of char and getting 1D array as row:

#include<stdio.h>
int main(){
   /* 2D array declaration*/
   //char disp[2][3];
  /* char disp1[2][4] = {
    {10, 11, 12, 13},
    {14, 15, 16, 17}
   };
   */
   char disp[2][4] = {
    {'a', 'b', 'c', 'd'},
    {'f', 'g', 'h', 'i'}
   };

   //Counter variables for the loop
   int i, j;
   
   //Displaying array elements
   printf("Two Dimensional array elements:\n");
   for(i=0; i<2; i++) {
      for(j=0;j<3;j++) {
         printf("%c ", disp[i][j]);
         if(j==2){
            printf("\n");
         }
      }
   }
   return 0;
}

2)

Stack Allocation :

Example :below program is showing stack allocation ..stack allocation get only by local variables.The allocation happens on contiguous blocks of memory. We call it stack memory allocation because the allocation happens in function call stack.

Stack memory allocated by compiler.

int main() 
{ 
   // All these variables get memory 
   // allocated on stack 
   int a; 
   int b[10]; 
   int n = 20; 
   int c[n]; 
} 

Heap Allocation:

Example: This example shows heap allocation which is on global variables and expression.The memory is allocated during execution of instructions written by programmers.

Heap memory allocation should be handle by programmers himself.he should deallocate memory after use.

int main() 
{ 
   // This memory for 10 integers 
   // is allocated on heap. 
   int *ptr  = new int[10]; 
} 

Your given program heap and stack allocation , commented the program below

int main()
{
int myarray[3]; // allocated on stack
myfunction (myarray);
}

int myfunction(int* in)
{
int i; //created on stack..unintialized
for (i = 0; i<3; i+= 1)
{
in[i] = i;//allocated on heap
}
// Stack (Method myfunction and variables in/i)
// Heap (Values of in/i will be in heap)
return 0;

}

Know the answer?
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
Similar Questions
***C Programming*** Given a 2D array. Write a function that will take the 2D array as...
***C Programming*** Given a 2D array. Write a function that will take the 2D array as argument and then print the maximum number in each row of that 2D array.
C programming Write a function that takes in a 2D char array (string array) and return...
C programming Write a function that takes in a 2D char array (string array) and return a 1D char array with all the elements connected together Hint: strlen(-char*-) //returns the length of a string strcat(-char* accum-, something to add) //works like string+= in java
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void Ge
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void GenerateFromArray(void); /************************************************************************************/ /************************************************************************************/ int main(void) { /* This is the main() program. It should call the functions ScanArray() and GenerateFromArray() */ GenerateFromArray(); system("pause"); return 0; } /*************************************************************************************** Define this function to scan the elements of an array from the given data file "ArrayInp.dat". If the input file is not found, or contains invalid data, the function should return a 0 and print an error message. If the input...
DESCRIPTION: You will be given a 2D array, called matrix, of Strings. The array has an...
DESCRIPTION: You will be given a 2D array, called matrix, of Strings. The array has an unknown number of cells filled with data. Your goal is to iterate through the 2D array and keep a count of how many cells are full. You will be given the dimensions of the array. INPUT: All input has been handled for you: Two integers denoting the dimensions of the array. These are the integer variables rows and cols. A partially filled 2-D array....
(C++) Write a function that takes as input parameters (using call by pointer) 3 integers. It...
(C++) Write a function that takes as input parameters (using call by pointer) 3 integers. It generates a random number between 25 and 50 (not including 50). It then creates an array on the memory heap of that length. It generates a random high number between 5 and 10 and a random low number between -5 and -10 and fills in the array iteratively with random numbers between the high and the low numbers*, and it returns that array. The...
(C++) Write a function that is almost exactly the same as the function above, only it...
(C++) Write a function that is almost exactly the same as the function above, only it takes an input parameter an integer (pass in a number between 25 and 50). Inside the function create an array on the stack instead of the heap. Fill it with random numbers as above. Return the address of the first value of the array, and then in the main use function 1 to print it out. This should NOT work. In comments explain why....
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
(In Java) 1. Create a 6X6 2D Array called numCourses 2. Sum the rows and print...
(In Java) 1. Create a 6X6 2D Array called numCourses 2. Sum the rows and print out the results with text identifying what you are outputting. 3. Sum the columns and print out the results with text identifying what you are outputting. 4. Compare the rows output and calculate and output the minimum value and maximum value. */ public class TwoDArrayExamplesHW{ public static void main(String[] args){ int[][] numCourses = {{2, 3, 2, 0, 0},    {2, 3, 2, 3, 1},...
Explain your code with comments. Solve in C++. Write a function named myFunc3() that takes a...
Explain your code with comments. Solve in C++. Write a function named myFunc3() that takes a 2D integer array NUMBERS[][50], and it size n and m. Then the function will print the sum of each row in one line.