Computer science help
why the c++defination has bug?
float *arrayFt;
arrayFt = new float(10); // allocate an array of float
for(int i = 0; i <= 10; i++)
arrayFt[i] = 15.0; // initialize dynamic array
delete arrayFt; // delete the allocated memory
You have allocated only 10 elements for *arrayFt. But inside the for loop you have assigned 0 to 10, i.e/ 11 times assigned. So logically the array elements will be arrayFt[0] to arrayFt[9]. But you have assigned to arrayFt[10] also.
The correct form will be
for(int i = 0; i <10; i++)
arrayFt[i] = 15.0; // initialize dynamic array
PROGRAM EXECUTION
#include<iostream>
using namespace std;
int main()
{
float *arrayFt;
arrayFt=new float(10);// allocate an array of
float
for(int i=0;i<10;i++)
arrayFt[i]=15.0;// initialize dynamic
array
for(int i=0;i<10;i++)
cout<<endl<<"arrayFt["<<i<<"] =
"<<arrayFt[i]; //DISPLAY THE ARRAY ELEMENTS
delete arrayFt;// delete the allocated
memory
}
output
![arrayFt [0] arrayFt [11 = 15 arrayFt [21 15 arrayFt [31 arrayFt [4] arrayFt [51 arrayFt [61 arrayFt [71 arrayFt [81 arrayFt [](http://img.homeworklib.com/questions/ec46e940-cd52-11ea-baad-a51a97e90c12.png?x-oss-process=image/resize,w_560)
Computer science help why the c++defination has bug? float *arrayFt; arrayFt = new float(10); // allocate...
What is the purpose of the new operator? Select one: a. It is used to allocate memory for variables at runtime. b. It is used to declare a new function. c. It is used to initialize class member variables. d. It is used to start a new program. e. It allows new operators to be created. Question 2 Not yet answered Scored out of 1.00 Flag question Question text Under what circumstances can you successfully return a pointer from a...
Computer Science Midterm Exam Review Questions 21. How do you dynamically allocate variables in C++? 22. How do you dynamically allocate an array in C++? 23. How do you deallocate variables in C++? 24. How do you deallocate arrays in C++? 25. How do you dynamically allocate memory in C? 26. How do you deallocate memory in C?
C++ please, thank you! Design a class named ArrayClass that has an array of floating-point numbers. The constructor should accept an integer argument and allocate the array on the heap to hold that many numbers. The default constructor should allocate an array that can hold one number. When the user attempts to store items, you must make a copy constructor that correctly produces a new copy of an ArrayClass object from an old one. If the user attempts to store...
WRITE THE NECESSARY C++ STATEMENTS 1. (10 points) Allocate memory dynamically for an int array with 100 elements. Next, assign the value of 1 to all the elements of the array and, finally, delete the array.
C++ computer science Given the partial class HardDrive implementation below, implement all of the following: 1: default constructor that initialized the attributes with proper default data of your choice. 2: destructor() method, that releases all the dynamically allocated memory. 3: copy constructor() method: That properly manages the dynamic array when the object is passed by value to a function as a parameter. 4: overload the assignment operator: To properly handle copying the dynamic array when one object is assigned to...
Which of the following function calls are syntactically correct given the variables and function header below? Select all that apply. int x=0, *ptr1; ptr1 = &x; void makeNegative(int *val) makeNegative(*ptr1); makeNegative(ptr1); makeNegative(x); makeNegative(&x); Which of the following statements will dynamically allocate memory for an array with 10 elements? int *ptr = int[10]; int *ptr[10]; int *ptr[10] = new int[10]; Identify all of the smart pointer types. Select all that apply. dynamic weak unique shared Which of the following statements will free...
C ++ 1. Write down the missing code according to the comments. #include <cstdlib> #include <iostream> using namespace std; void fun1(int radius) { /* dynamic memory management */ double * dmptr; double circumference; /* add the code below to: - allocate a memory location for a double and nameless variable in heap and assign its address to the pointer - assign 3.14 to the variable in heap via the pointer....
CSCI 3000 Homework 4In this assignment, you will implement a simple version of Computer Lab administration system in C++. Your program will monitor computers in 4 computer labs and will allow users to log in and log out. Each computer lab has different number of computers.· Lab 1 has 10 computers· Lab 2 has 6 computers· Lab 3 has 3 computers· Lab 4 has 12 computersHere is a sample state of the system:Lab ArraySome of the computers are free (no...
Please help with this coding 1.You need to dynamically allocate memory to read into a number of elements. At first, you need to determine how many numbers (here, ‘n’) to be read. Next, you need to dynamically allocate memory for ‘n’ integers. As you see,‘x’ is an integer pointer which needs to dynamically allocate memory to read ‘n’integers into it fromthekeyboard. 2 Read nintegers into the allocated block using scanf. 3. Complete the printArrayfunction to display ‘n’ integers. void printArray(int...
C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...