Question
Asked Mar 18, 2020
1 views

 

4. Is the following program correct? If not, please correct it (4)

#include<iostream>

using namespace std;

 

int main ()

{

​int size;

​int * ptr;

​cin>>size;

​ptr=new int [size];

 

​for ( int count=0; count<size; count++)

​{​

​​ptr[count]=count*count;

​​cout<<ptr[count]<<" ";

​}

 

​return 0;

 

}

check_circle

Expert Answer

Step 1

The given C++ program is working properly.

The program (with comments), explanation and the sample output is given below: -

Program: -

//header file

#include <iostream>

//using namespace for standard I/O

using namespace std;

//main method

int main ()

{

    //declaring the varibale

    int size;

    //declaring the pointer ptr

    int *ptr;

    //taking the value of "size" varibale

    cin>>size;

    //dynamic array of pointer

    ptr = new int[size];

    //filling the elements in the pointer array

    for(int count=0; count<size; count++)

    {

        //storing the square of count varibale in the pointer array

        ptr[count] = count*count;

        //displaying the elements stored in the pointer array

        cout << ptr[count] <<" ";

    }

    //return statement

    return 0;

}

Step 2

Explanation: - The given C++ program is used to store the square of the numbers in the dynamic array of pointers. Below is the working of the given program: -

  • Including the header file and the namespace.
  • Defining the main function.
  • Declaring the variable “size” to give the size of the pointer array. (The value of size is given by the user).
  • Declaring the pointer “*ptr”.
  • Using the new keyword declaring the dynamic array of pointer (to integers) type and the variable “size” is passed to give the size of the array at run-time.
  • Using a for-loop, to fill ...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: In C code, Given an int variable count that has already been declared, write a for loop that prints ...

A: The C program and its approach is given below: - Approach: - Declaring the necessary headers. Defin...

question_answer

Q: a. Draw the hierarchy chart and design the logic for a program needed by the manager of the Stengel ...

A: Note: We are authorized to answer only one question at a time. Since you have asked more than one qu...

question_answer

Q: What is software engineering?

A: Software engineering Software engineering deals with building and maintaining software systems. It ...

question_answer

Q: In C++, what is the benefit of using typedefs in a program? Here is an exmaple line of code: typedef...

A: Benefits of using typedef:   In C++, typedef allows to define the new data type explicitly. i.e., i...

question_answer

Q: Decode the following secret ASCII message (reading across):100 1101 110 0101 110 0101 111 0100 010 0...

A: The related ASCII characters for the given binary digits: Convert the given binary digit into decima...

question_answer

Q: Create a python program for the following: Ask the user to create a username and password. Authentic...

A: Programming instructions : Prompt user to set a value for username Prompt user to set a value for p...

question_answer

Q: Which UML symbol represents aggregation? a filled triangle an unfilled diamond an unfilled tri...

A: UML stands for Unified Modelling Language. It is popular for its diagrammatic notation. It is used ...

question_answer

Q: Convert ABCD in hexadecimal to a decimal number

A: ABCD is in hexadecimal. The decimal equivalent of ABCD will be: 43981

question_answer

Q: 47) Convert the following numbers from decimal to binary:a) 12.28125b) 0.0234375c) 0.7

A: Approach to convert from decimal fraction  to binary number-   1) Procedure for the binary equivalen...