C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter8: Arrays And Strings
Section: Chapter Questions
Problem 1TF: Mark the following statements as true or false. A double type is an example of a simple data type....
icon
Related questions
icon
Concept explainers
Question

1. Examine the following code segment. What will be printed?
If there are any errors indicate the nature of each error.
int a[7] = { 31, 12, 32, 14, 25, 16, 27 };
int *ptr ;
ptr = & a[ 2 ] ;
for( i = 1; i <= 5; i = i + 2)
cout << *( ptr + i ) ;
cout << a[0] << *ptr << *ptr - 2 << *(ptr - 1)) << endl;

Expert Solution
Step 1

Error:

The variable i is not declared.

Nature:

The variable i is used in for loop but never been declared.

 

Declare variable i before its use:

int i;

 

Error:

Double closing parenthesis in the last cout statement.

Nature:

The parenthesis is used to point the value of pointer but there is one opening parenthesis and two closing parentheses.

 

Eliminate extra parenthesis from the code:

cout << a[0] << *ptr << *ptr - 2 << *(ptr - 1) << endl;

Step 2

Enbaded given code segement within the program:

#include <iostream>

using namespace std;

int main ()

{

    //declare integer i

    int i;

    int a[7] = { 31, 12, 32, 14, 25, 16, 27 };

    int *ptr ;

    ptr = & a[ 2 ] ;

    for( i = 1; i <= 5; i = i + 2)

    cout << *( ptr + i ) ;

    //remove extra closing parenthesis

    cout << a[0] << *ptr << *ptr - 2 << *(ptr - 1) << endl;

    return 0;

}

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Control Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning