Question

In: Computer Science

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

Solutions

Expert Solution

C Program:

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

//Function prototypes
void createArray(int n);

//Main function
int main()
{
int n;

//Reading a number from user
printf("Enter a number: ");
scanf("%d", &n);

//Calling function
createArray(n);

return 0;
}

//Function that allocates and writes numbers
void createArray(int n)
{
int i; //Looping Variable

//Dynamically allocating memory
int *arr;
arr = (int *)malloc(sizeof(int)*n);

//Filling array with numbers
for(i=0; i<n; i++)
{
//Storing values
*(arr+i) = i+1;
}

//Printing address and values of the first and the last integer stored in the memory
printf("\nFirst Integer: %d, stored at %u\n", *(arr+0), (arr+0));
printf("\nLast Integer: %d, stored at %u\n", *(arr+n-1), (arr+n-1));
}

________________________________________________________________________________________

Sample Run:


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 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!!!
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...
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>...
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.
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...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.
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...
Using Python, create a program that will act as a number convertor. This program will convert...
Using Python, create a program that will act as a number convertor. This program will convert an input decimal integer into binary and hexadecimal formats. a) Define a main function named numberconvertor(). Inside the main function, write all the logic of your code. [5% marks] b) Looping indefinitely (Hint: use infinite while loop), the program should give the user the 3 options given below and allow the user to select one among them. [15% marks] 1. converting a decimal number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT