Question

Computer science help why the c++defination has bug? float *arrayFt; arrayFt = new float(10); // allocate...

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

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

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 [

Add a comment
Know the answer?
Add Answer to:
Computer science help why the c++defination has bug? float *arrayFt; arrayFt = new float(10); // allocate...
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
  • Computer Science Midterm Exam Review Questions 21. How do you dynamically allocate variables in C++? 22....

    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?

  • WRITE THE NECESSARY C++ STATEMENTS 1. (10 points) Allocate memory dynamically for an int array with...

    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:...

    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...

  • computer science

    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...

  • C++ problem with dynamic arrays is that once the array is created using the new operator...

    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...

  • How to solve this Problem in C++ . The Problem Write program that uses a class...

    How to solve this Problem in C++ . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the set...

  • Need helps on C++ attach. Thanks Pointers with classes a) A user-defined class named Timer has...

    Need helps on C++ attach. Thanks Pointers with classes a) A user-defined class named Timer has a constructor that takes two integer parameters to initialize hour and minute data members. Write a single C++ statement to create an object of the Timer class using dynamic memory allocation and assign it to a pointer variable named timePt r. It should call the constructor with two parameters. Use values of 10 and 20 for hour and minute respectively. b) Write the definition...

  • Homework Question Write a void function called transformArray that takes two parameters - a reference to...

    Homework Question Write a void function called transformArray that takes two parameters - a reference to a pointer to a dynamically allocated array of ints, and the size of that array.  The pointer is passed by referencebecause you want to change the value of the pointer. The function should dynamically allocate an array that is twice as long, filled with the values from the original array followed by each of those values times 2. For example, if the array that was...

  • Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array...

    Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray "Computer Science"; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println("int"); return n; public static long xMethod(long n) System.out.,println("long"); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

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
ADVERTISEMENT