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 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
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....
/************************************************************************************* 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...
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...
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.
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
C Programming 1. Provide an equivalent of the java class in C class Example { public...
C Programming 1. Provide an equivalent of the java class in C class Example { public static int[][] question4(int n) { int[][] result = new int [n][n]; for(int i=1; i<=n; i+=1) for (int j=1; j<=n; j+=1) result[i][j] = (i*n)+j; return result; } public static void main(String[] args) { int[][] my array = question4(4); } } 2. Rewrite the following function using no loops, and only tail call recursion int min (int n, int[] input) { int i; int result; for...