Question

Build a dynamic array of 10 ints on the heap. Assign a value of 10* index...

Build a dynamic array of 10 ints on the heap. Assign a value of 10* index number to each element.

Display the array.

Wrap the following in a 5 count loop:

Generate a random number. Even – add 1 element to the array. Odd –subject 1 element from the array.

Print what happened.

Print the entire loop again.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdlib.h>
#include<iostream>

//main function
int main()
{   
//declaring dynamic array
int *a = new int[10];
int index = 9;
// assigning value to array
for(int i=0; i<=index; i++){
a[i] = i*10;
}
for(int i=0; i<=index; i++){
std::cout<<a[i]<<"\t";
}
std::cout<<std::endl;
  
//looping 5 times
for(int i=0; i<5; i++)
{
//generating random number
int num = rand();
  
//if random number is even
if(num%2 == 0)
{
index++;
a[index] = index*10;

for(int i=0; i<=index; i++)
std::cout<<a[i]<<"\t";
std::cout<<"\n 1 Element is added to array"<<std::endl;
}
else
{
//if random number is add


--index;

  for(int i=0; i<=index; i++)
std::cout<<a[i]<<"\t";
std::cout<<"\n 1 Element is Subtracted from array "<<std::endl;
}
}
std::cout<<std::endl;

//printing value of array
std::cout<<" After 5 iteration array is: "<<std::endl;
for(int i=0; i<=index; i++){
std::cout<<a[i]<<"\t";
}
}

Add a comment
Know the answer?
Add Answer to:
Build a dynamic array of 10 ints on the heap. Assign a value of 10* index...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • Using Java: Create an array with the size of 10 and assign student details (Name, ID,...

    Using Java: Create an array with the size of 10 and assign student details (Name, ID, age and TotalMarks) to the array. Perform the Heap Sort to sort the students in ascending order based on their total marks. Steps: • Create the student list (use Random class in java to generate the age (15- 25) and total (0-100)) • Print the Student List in a table format • Perform Heap sort based on the total marks of the students •...

  • Write a C program to do the following 1) request user to enter 10 integer into...

    Write a C program to do the following 1) request user to enter 10 integer into an array 2) sort the array in ascending order 3) display the sorted array 4) count the number of odd and even number in the array and print out the result 5) add the value of the odd number and even number and calculate the average of the odd and even number. display the result 6) write function addNumber() to add all the number...

  • Having issues using binary search on a pointer array. 1. Write 1000 random ints to file...

    Having issues using binary search on a pointer array. 1. Write 1000 random ints to file 2. Binary Search to find if number exists in element from file. int main() { ArrayActions action;   int count; int userInput; int num = 1000; int array[num]; ofstream myFile ("/Users/chan/Desktop/LANEY_CIS27/Assignemtn2_CIS27/Assignemtn2_CIS27/File.txt");   //Set srand with time to generate unique random numbers srand((unsigned)time(0));   //Format random num up to 999 for(count = 0; count < num; count++) { array[count] = rand() % 1000; }          //Condition...

  • Write a program in either C++ or Java meeting these requirements Description: In this program, we...

    Write a program in either C++ or Java meeting these requirements Description: In this program, we assume that our data comes in dynamically. We need to maintain our data in a heap, after every insertion and deletion. We also need to handle the underlying array dynamically. whenever we detect an overflow in our array, we will create a new array with size doubling the previous size. Requirements: 1. (Dynamic Data) when we generate the data, we simulate dynamic data. We...

  • In C language 1. Write a program to declare and initialize an array of size 5...

    In C language 1. Write a program to declare and initialize an array of size 5 and place odd numbers 3, 5, 7,9 and 11 in it. Declare and initialize another array of size 5 and place even numbers 4, 6, 8, 10 and 12 in it. Write a for loop to add each element and place it in a third array of the same dimensions. Display each array.

  • C languge please ! 1. Using the random number program, fill up an array with 100...

    C languge please ! 1. Using the random number program, fill up an array with 100 random numbers between -30 and 30 and display it. Find out how many of those numbers in the array are positive, negative, even and odd. Display the results. Make sure you cover the special case. Your entire code should have only 1 FOR loop. You will not receive any credit if you use more than 1 loop. (50 points - 5pts for commenting and...

  • Write a Java program, In this project, you are going to build a max-heap using array...

    Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...

  • please do everything in matlab with commets next to each line of code thank you! %%...

    please do everything in matlab with commets next to each line of code thank you! %% Problem 1 - 1 point % Create a 100-element array, A, which is a random array of integers % Use the randi function for this, 'help randi' %% Problem 2 - 1 point % Use a 'for' loop and an 'if' statement to count how many odd numbers there are in the % array you made in #1 above %% Problem 3 - 1...

  • 1. In Lab 4, you developed a program to build a Max Heap, and then Heap...

    1. In Lab 4, you developed a program to build a Max Heap, and then Heap Sort. Update the program by adding two additional functions: (a) AddData(A, N, V) where V is the new value added. (b) Delete a data Delete by giving the index of the data position Make sure to display the array after calling each of the function. 2. Write a program to implement Binary Search Algorithm, which will return the index of the data searched (V)....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT