Question

Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory...

Create C program that takes 1 parameter: a number.

Using that number, dynamically allocate a memory so you store that number of integers.

Write integers in order starting from 1 until you fill all that memory.

Print the addresses and values of the first and the last integer stored in the memory.

Homework Answers

Answer #1

HERE IS THE CODE AS PER YOUR REQUIREMENT

CODE:

#include<stdio.h>
#include<stdlib.h>

int main() {
int n,i;
// pointer to hold base address of the block to be created
int* ptr;
// user input for a number parameter
printf("Enter the number of elements: ");
scanf("%d",&n);
// malloc method to dynamically allocate the memory
ptr = (int*)malloc(n * sizeof(int));
if(ptr != NULL) {
// Entering the elements from 1 to n into the memory locations
printf("Memory allocated successfully using malloc() method");
for (int i = 0; i < n; i++) {
ptr[i] = i+1;
}
// printing the values and assigned addresses of 1st and last integer stored
printf("\n%d %p",ptr[0],&ptr[0]);
printf("\n%d %p",ptr[n-1],&ptr[n-1]);
}
else {
printf("Memory not allocated");
  
}
}

SCREENSHOTS:

OUTPUT:

IF YOU HAVE ANY QUERIES FEEL FREE TO ASK AT COMMENTS

PLEASE DO LIKE :)

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
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory...
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory so you store that number of integers. Write integers in order starting from 1 until you fill all that memory. Print the addresses and values of the first and the last integer stored in the memory.
Dynamically allocate memory for two integers. Swap their values. write your statement in C
Dynamically allocate memory for two integers. Swap their values. write your statement in C
In c++ Write a program that creates a dynamically allocated array to store students’ grades. The...
In c++ Write a program that creates a dynamically allocated array to store students’ grades. The user should be able to decide the size of the array during run time. Write code to fill this array from the keyboard and then print out the values of your array,
• First, create a function called addNumber, which has a formal parameter for an array of...
• First, create a function called addNumber, which has a formal parameter for an array of integers and increase the value of each array element by a random integer number between 1 to 10. o Add any other formal parameters that are needed. • Second, create another function called printReverse that prints this array in reverse order. • Then, you need to write a C++ program to test the use of these two functions.
In Assembly x86 please Write an assembly program(call it lab6_file1.asm)that contains 10 double words in memory...
In Assembly x86 please Write an assembly program(call it lab6_file1.asm)that contains 10 double words in memory in the .data section where the first five initialized to 0 and the last five are initialized to 1. In addition, reserve ten uninitialized double words in .bss section. Using a loop, write an assembly program that copies the ten initialized values into the ten reserved double words, starting at the last position, moving to the first(reverse order the data in your uninitialized array)....
Write a program on C++ to calculate and print the factorial of a number using a...
Write a program on C++ to calculate and print the factorial of a number using a for loop. The factorial of a number is the product of all integers up to and including that number, so the factorial of 4 is 4*3*2*1= 24.
Write some C++ program segments that solves the indicated tasks (you do not have to write...
Write some C++ program segments that solves the indicated tasks (you do not have to write a complete program, nor be concerned about "good" output; a small code segment will be enough). a) A program that gets a double number from the user, decides whether that number is positive, negative, or zero and display its decision on the screen. a) A function isPositive that takes as input a double number and returns the integer 1 if the number is positive,...
10. Number Array Class Design a class that has an array of floating-point numbers. The constructor...
10. Number Array Class Design a class that has an array of floating-point numbers. The constructor should accept an integer argument and dynamically allocate the array to hold that many numbers. The private data members of the class should include the integer argument in a variable to hold the size of the array and a pointer to float type to hold the address of the first element in the array. The destructor should free the memory held by the array....
In C++ Using recursion write a program that takes a positive integer number and returns whether...
In C++ Using recursion write a program that takes a positive integer number and returns whether there is 6. For example, if the input number is 7068, the function returns true
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT