Answer to Question #267974 in C++ for malika

Question #267974
This program is supposed to write 100 99 98 3 2 1, but it probably does not. What is the program doing that is incorrect? (We're not asking you explain why the incorrect action leads to the particular outcome it does, and we're not asking you to propose a fix to the problem.)
    #include <iostream>
    using namespace std;
    int* nochange(int* p)
    {return p;
    }
    int* getPtrToArray(int& m)
    {
        int anArray[100];
        for (int j = 0; j < 100; j++)
            anArray[j] = 100-j;
        m = 100;
        return nochange(anArray); }
    void f()
    {int junk[100];
        for (int k = 0; k < 100; k++)
            junk[k] = 123400000 + k;
        junk[50]++;
    }
    int main()
    { int n;
        int* ptr = getPtrToArray(n);
        f();
        for (int i = 0; i < 3; i++)
            cout << ptr[i] << ' ';
        for (int i = n-3; i < n; i++)
            cout << ptr[i] << ' ';
        cout << endl;}





1
Expert's answer
2021-11-20T06:39:57-0500
 /*This program is supposed to write 100 99 98 3 2 1,
 but it probably does not.
 What is the program doing that is incorrect?
(We're not asking you explain why the incorrect action leads to the particular outcome it does,
and we're not asking you to propose a fix to the problem.)*/
 #include <iostream>
    using namespace std;
    int* nochange(int* p)
    {return p;
    }
    int* getPtrToArray(int& m)
    {
        int anArray[100];
        for (int j = 1; j < 100; j++)
            anArray[j] = 100-j;
        m = 100;
        return nochange(anArray); }
    void f()
    {int junk[100];
        for (int k =0;k < 100; k++)


            junk[k] = 100-k;
        junk[50]++;
    }
    int main()
    { int n;
        int *ptr = getPtrToArray(n);
      f();
        for (int i = 0; i <3; i++)
            cout << ptr[i] << ' ';
        for (int i = n-3; i < n; i++)
            cout << ptr[i] << ' ';
        cout << endl;}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS