Question

Memory was allocated initially for 5 elements, later on two more elements added to list. How...

Memory was allocated initially for 5 elements, later on two more elements added to list. How can
space be managed in such case. Implement the scenario in C.
IN C PROGRAMING

Homework Answers

Answer #1

CODE:

#include <stdio.h>

#include <stdlib.h>

int main(void) {

  int *ptr;

  // a dynamic array of size 5 is allocated

  ptr = (int*) malloc(5 * sizeof(int));

  for(int i = 0; i < 5; ++i){

    // storing index+1 in the array

    *(ptr+i)=i+1;

  }

  printf("%d\n",*(ptr+4));

  // now for example assume 5 is a bit short for us

  // so lets extend the size of ptr array

  ptr = realloc(ptr, 7 * sizeof(int));

  // now we can store value till index 7

  for(int i = 0; i < 7; ++i){

    // storing index+1 in the array

    *(ptr+i)=i+1;

  }

  printf("%d\n",*(ptr+6));

return 0;

}

OUTPUT:

Please upvote if you like my answer and comment below if you have any queries or need any further explanation.

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
12. In a study of memory recall, 5 people were given 10 minutes to memorize a...
12. In a study of memory recall, 5 people were given 10 minutes to memorize a list of 20 words. Each was asked to list as many of the words as he or she could remember both 1 hour and 24 hours later. The result is shown in the following table. Assume we want to use a 0.05 significance level in your analysis. The research question is: Do students recall significantly more words after 1 hour than after 24 hours...
Answer question for C# 1 of 10 When items are added or removed, a list resizes...
Answer question for C# 1 of 10 When items are added or removed, a list resizes itself. True False Question 2 of 10 What happens when you assign one list to another using the assignment operator? An error message generates. Elements copy to new memory locations. Both lists become value-type data elements. Both lists reference the same memory locations. Question 3 of 10 Which of the following is a valid example of calling a method and sending it an array...
1. The memory units that follow are specified by the number of words times the number...
1. The memory units that follow are specified by the number of words times the number of bits per word. How many address lines and input/output data lines are needed in each case? (a) 8K X 16 (b) 2G X 8 (c) 16M X 32 (d) 256K X 64 2. Give the number of bytes stored in each memory unit in question 1. 3. Word number 563 decimal in the memory shown in Fig. 7.3 (see Mano-Ch7.pdf) contains the binary...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n == 0)    return 0; else    return n * f(n - 1); } A. 120 B. 60 C. 1 D. 0 10 points    QUESTION 2 Which of the following statements could describe the general (recursive) case of a recursive algorithm? In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt(int n ) // line 1 { // line 2...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
How to use C++ figure this question? Collection and Sorted Collection An array is great for...
How to use C++ figure this question? Collection and Sorted Collection An array is great for storing a list of values. However, one challenge when working with arrays is they have a fixed size. If you declare an array of size 10 and later need to actually store 11 elements you have to create a new array and copy everything over. C++ (and other langauges) create classes that handle this and other operations behind the scenes. In this assignment, we...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
5) Two heavenly bodies both with radii of 6×103 miles and masses of 9×1024 kg are...
5) Two heavenly bodies both with radii of 6×103 miles and masses of 9×1024 kg are separated by 4×107 miles. They are initially at rest. How fast are they moving just before their surfaces collide. Assume that you can ignore any effects having to do with the existence of atmospheres and that nothing significant exists in the space between the planets. Correct, computer gets: 1.25E+04 mi/hr Hint: Consider the initial "scene" before the planets gain any speed and the final...
1. Dynamics and artificial gravity. The space station has two thrusters pointed in opposite directions. They...
1. Dynamics and artificial gravity. The space station has two thrusters pointed in opposite directions. They operate by expelling propellant at high speed. The effect is that there is a force of magnitude F0 on the two ends of the space station in opposite directions, which causes the entire object to start rotating. The thrusters will stop firing when the artificial gravity created on the space station (described below) reaches the required value. Upward force, magnitude F0 Downward force, magnitude...
Please linked both files. For this assignment you need to create a ToDo list using Javascript,...
Please linked both files. For this assignment you need to create a ToDo list using Javascript, along with HTML and CSS. Begin by creating a HTML page called todo.html. Then create a Javascript file called todo.js and link it in to the HTML page using a script tag. All Javascript for the assignment must be in the separate file. (For CSS, feel free to include styles in a style block at the top of the HTML page, or to link...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT