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 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...
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; }
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.
with C# create a console application project that outputs the number of bytes in memory that...
with C# create a console application project that outputs the number of bytes in memory that each of the following number types use, and the minimum and maximum values they can have: sbyte, byte, short, ushort, int, uint, long, ulong, float, double, and decimal. Try formatting the values into a nice-looking table! More Information: You can always read the documentation, available at https://docs.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting for Composite Formatting to learn how to align text in a console application. Your output should look...
Write a program in C or C++ that takes a number series of size n (n...
Write a program in C or C++ that takes a number series of size n (n integers) as input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note that this is not simply popping and printing the numbers, but the program should manipulate the stack to have the numbers stored in reverse order. In addition to the provided header file, the students can use the following function to print the content...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT