Question

c++ only. Please follow directions. What does the following program print and why? Comment each line...

c++ only. Please follow directions.

What does the following program print and why? Comment each line of code to explain what it is doing.  

#include <iostream>
using namespace std;

int main()
{
int track[ ] = { 10, 20, 30, 40 };
int * ptr;

ptr = track;
track[1] += 30;
cout << * ptr << " ";
*ptr -= 10;
ptr++;
cout << * ptr << " ";
ptr += 2;
cout << * ptr << " ";
cout << track[ 0 ] << endl;

return( 0 );
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;

int main()
{
int track[ ] = { 10, 20, 30, 40 };
int * ptr;

ptr = track;
// this changes the value of 20 to 20+30 = 50
track[1] += 30;

// As ptr points to start of the array track
// *ptr prints 10
cout << * ptr << " ";

// this changes track[0]=track[0]-10 = 0
*ptr -= 10;

// ptr points to track[1] 
ptr++;

// prints the value of track[1]
// prints 50
cout << * ptr << " ";

// ptr points to track[3]
ptr += 2;

// this prints value of track[3]
// i.e 40
cout << * ptr << " ";

// track[0] is 0
// so it prints 0
cout << track[ 0 ] << endl;

return( 0 );
}

10 50 40 0

Note: Please follow comments in code

Add a comment
Know the answer?
Add Answer to:
c++ only. Please follow directions. What does the following program print and why? Comment each line...
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
  • c++ only. Please read directions. I'm looking to have each line re-written in a way that...

    c++ only. Please read directions. I'm looking to have each line re-written in a way that shows we're using the walk through method that the pointer is shifting by each value to solve this. Example far below shows what not to do.   Rewrite this program and remove the variable declared at line A below. Make your revised program generate the exact same output as the original without using the variable declared at line A. #include <iostream> using namespace std; int...

  • What does the following program print? I // Exercise 3.12 Solution: ex03_12.cpp // What does this...

    What does the following program print? I // Exercise 3.12 Solution: ex03_12.cpp // What does this program print? #include <iostream> using namespace std; 3 4 7 int main() { int y; // declare y int x = 1; // initialize x int total = 0; // initialize total while ( 10 ) // loop 10 times y = X* X; // perform calculation cout << y << endl; // output result total += y; // add y to total ++x;...

  • // Write a program that determines how many of each type of vowel are in an...

    // Write a program that determines how many of each type of vowel are in an entered string of 50 characters or less. // The program should prompt the user for a string. // The program should then sequence through the string character by character (till it gets to the NULL character) and count how many of each type of vowel are in the string. // Vowels: a, e, i, o, u. // Output the entered string, how many of...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • Consider the following C++code snippet and what is the output of this program? # include<iostream> using...

    Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • 1. What is wrong with the following C++ program? #include <iostream> int main() { a =...

    1. What is wrong with the following C++ program? #include <iostream> int main() { a = 4; b = 6; cout << a << "+" << b << "=" << a+b; return 0; 2. What is wrong with the following C++ program? What was its intended output? #include <iostream> using namespace std; int main() { cout << "What is larger? e pi or pi e?" << endl; double ans1 = exp(pi); double ans2 = pi exp(1.); cout << "epi is...

  • C++ 1. What is the final output? 2. Why is 2 the ouput for (int)*c? Please...

    C++ 1. What is the final output? 2. Why is 2 the ouput for (int)*c? Please explain what happens during the line c = c + *p +1; #include <iostream> using namespace std; int main() {    int arr[5] = { 5,2,3,1,4 };    int* p = arr;    cout << (int)*p << endl;    unsigned char* c = (unsigned char*)arr;    p = p + 2;    c = c + *p + 1;       cout << (int)*p...

  • Can someone explain how this C++ program runs? A line by line explanation/commentation would be great,...

    Can someone explain how this C++ program runs? A line by line explanation/commentation would be great, as well as the purpose of the program and functions/classes involved. #include <iostream> #include <vector> using namespace std; // template function vector<int> removeEvenIndexedVals(vector<int> vec); // main int main() {        static const int arr[] = { 2,5,7,9,1,3,6 };        vector<int> vec(arr, arr + sizeof(arr) / sizeof(arr[0]));        // call function        vec = removeEvenIndexedVals(vec);        // print        cout << "Displaying the Vector Elements:"...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

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