Question

In: Computer Science

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.

Help ASAP!!!

Solutions

Expert Solution

Hi,

Hope you are doing fine. I have coded the above question in C keeping all the conditions in mind. Since a sample output of the code is not provided, I have taken the liberty to write it as per my convenience. You may modify it as per your requirement. The code has been clearly explained using comments that have been highlighted in bold.

Program:

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

int main()
{
   //declaring variables
   //num is the input parameter, i is used for indexing

   int num,i=0;
   //promptiong user for input
   printf("Enter a number:\n");
   scanf("%d",&num);
   //declaring pointer that will hold the base address of the dynamic memory block
   int *arr;
   //dynamically allocating memory of size num to hold a list of integers. arr points to the base address of the memory
   arr=(int*)malloc(num * sizeof(int));
   //filling the dynamically allocated memory
   for(i=0;i<num;i++)
   {
       arr[i]=i+1;
   }
   //printing the value of first integer
   printf("Value of first integer: %d\t",arr[0]);
   //printing the address of first integer
   printf("Address: %d\n",&arr[0]);
   //printing the value of last integer
   printf("Value of last integer: %d\t",arr[num-1]);
   //printing the address of last integer
   printf("Address: %d\n",&arr[num-1]);
  
}

Executable code snippet:

Output:


Related Solutions

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.
Create a c program that takes 1 parameter, a number using that number, dynamically allocate a...
Create a 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 address and values of the first and the last integer stored in the memory
Write a modified version of the program below. In this version, you will dynamically allocate memory...
Write a modified version of the program below. In this version, you will dynamically allocate memory for the new C-string and old C-string, using the new keyword. Your program should ask the user for the size of the C-string to be entered, and ask the user for the C-string, then use new to create the two pointers (C-strings).   Then, call Reverse, as before. Don’t forget to use delete at the end of your program to free the memory! #include <iostream>...
Prime Sum C program !! Dynamically allocated memory Let P(n) denote the sum of the first...
Prime Sum C program !! Dynamically allocated memory Let P(n) denote the sum of the first n prime numbers. For example, P(1) = 2 and P(3) = 10, since the first three prime numbers are 2, 3 and 5, respectively. Write a program to determine the value of the function P(n) for different values of n. The first few prime sums are 2, 5, 10, 17, 28, 41, 58 and 77. Input The first line of the input file contains...
Create a memory diagram for the following C program when the program reaches point 1. void...
Create a memory diagram for the following C program when the program reaches point 1. void foo (int *a); int bar(int *x); int search(int *c); int main(void) { int q, p=10; q= search(&p); return 0; } void foo(int *a) { *a += 2; //point 1 *f *= 10; } int bar(int *x) { return 2* *x; } int search(int *c) { int z= *c + bar(c); foo(c); return z; }
Create a C++ program that generates a random number from 1 to 100 using the rand()...
Create a C++ program that generates a random number from 1 to 100 using the rand() function. Give the user a total 5 chances to guess the number. Stop the program and indicate the user guessed the correct number and should be applauded. Also, please find out which guess was the closest to the actual number. For example, if the rand() produces 57 and the user guessed 10, 80, 52, 33 and 44 in their respective turns then print out...
Can someone do this python program? Write a function that takes a number as a parameter...
Can someone do this python program? Write a function that takes a number as a parameter and then prints out all of the factors for that number.
Using loop statements, write a C++ program which takes the number of items that a shopper...
Using loop statements, write a C++ program which takes the number of items that a shopper wants to buy, and then takes the price of each item, and at the end tells the shopper how much she must pay. This is a sample of the output: How many items do you have in your basket? 3 Enter the price in dollar? 10.25 Enter the price in dollar? 20.75 Enter the price in dollar? 67.5 You must pay 98.5 $ today.
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your program does not need to do anything else.  Indicate via comments where memory for at least one variable in each memory area is allocated. a) Write code that allocates static memory (only include one declaration): b) Write code that allocates stack memory (only include one declaration): c) Write code that allocates heap memory (only include one declaration): 2) Edit the C++ program below to include...
1. [100] Create a C program for a number guessing game. Here are the requirements: a....
1. [100] Create a C program for a number guessing game. Here are the requirements: a. Your program generates a random number between -100 and 100 and keeps asking the user to guess the number until the user guesses it correctly. Your random number generator should be implemented as a C function which takes min and max values as input parameters and returns a random number between those values including the min and the max. b. If the user guesses...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT