gjggml 2024-06-14 10:17 采纳率: 60%
浏览 9
已结题

急!C++指针编写相关的问题

This lab is intended to give you some hands-on experience with C++ pointers and
their usage.
IMPORTANT– Please read this whole document carefully before starting your work!
The lab has three elements (A-C). You will develop, debug and execute your source
code for all elements. When complete, upload your source code to the CANVAS site for
this lab. Please use the following filenames for your source code:

img

img

img

img

  • 写回答

16条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-06-14 10:17
    关注

    引自免费微信小程序:皆我百晓生

    Lab Element A: Basic Pointer Operations

    Source Code Filename: Pointers_A.cpp

    #include <iostream>
    using namespace std;
    
    // Function prototype
    void pointerOps(const int &, int *);
    
    int main() {
        // Define and initialize an integer variable
        int x = 10;
        
        // Define a pointer to int
        int *px;
        
        // Initialize the pointer to point to the memory location of x
        px = &x;
        
        // Call pointerOps() passing in x and px
        pointerOps(x, px);
        
        return 0;
    }
    
    // Function definition
    void pointerOps(const int &rx, int *px) {
        // Output statements
        cout << "value of x = " << rx << endl;
        cout << "address of x = " << &rx << endl;
        cout << "memory Location pointed to by px = " << px << endl;
        cout << "value obtained by dereferencing px = " << *px << endl;
        cout << "value obtained by taking the address of the dereferenced value of px = " << &(*px) << endl;
        cout << "value obtained by dereferencing the address of px=" << *(px) << endl;
        cout << "memory Location pointed to after incrementing px by 1=" << px + 1 << endl;
    }
    

    Lab Element B: Passing to Functions by Value Arguments and Reference Arguments

    Source Code Filename: Pointers_B1.cpp (for parts a and c)

    #include <iostream>
    using namespace std;
    
    // Function prototypes
    int cubeIt(int);
    int cubeIt(const int &);
    int cubeIt(int *);
    
    // Function definitions
    int cubeIt(int x) { return x * x * x; }
    int cubeIt(const int &x) { return x * x * x; }
    int cubeIt(int *x) { return (*x) * (*x) * (*x); }
    
    int main() {
        // Define and initialize an integer variable
        int x = 10;
        
        // Output statements
        cout << "return from cubeIt(int)=" << cubeIt(x) << endl;
        // cout << "return from cubeIt(const int &)=" << cubeIt(x) << endl; // Uncomment for Pointers_B2.cpp
        cout << "return value from cubeIt(int *)=" << cubeIt(&x) << endl;
        
        return 0;
    }
    

    Source Code Filename: Pointers_B2.cpp (for parts b and c)

    #include <iostream>
    using namespace std;
    
    // Function prototypes
    int cubeIt(int);
    int cubeIt(const int &);
    int cubeIt(int *);
    
    // Function definitions
    int cubeIt(int x) { return x * x * x; }
    int cubeIt(const int &x) { return x * x * x; }
    int cubeIt(int *x) { return (*x) * (*x) * (*x); }
    
    int main() {
        // Define and initialize an integer variable
        int x = 10;
        
        // Output statements
        // cout << "return from cubeIt(int)=" << cubeIt(x) << endl; // Uncomment for Pointers_B1.cpp
        cout << "return from cubeIt(const int &)=" << cubeIt(x) << endl;
        cout << "return value from cubeIt(int *)=" << cubeIt(&x) << endl;
        
        return 0;
    }
    

    Lab Element C: Relationship between Pointers and Arrays

    Source Code Filename: Pointers_C.cpp

    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    // Function prototype
    void arrayPointers(double [], const double *);
    
    int main() {
        // Define an array
        double myarray[20] = {0.};
        
        // Define a pointer to array
        double *pA = &myarray[0];
        
        // Immediately output the value of initialized values of pA and myarray
        cout << "Value of pA: " << pA << ", Value of &myarray[0]: " << &myarray[0] << endl;
        
        // Call the function arrayPointers()
        arrayPointers(myarray, pA);
        
        return 0;
    }
    
    // Function definition
    void arrayPointers(double arr[], const double *pa) {
        // Loop to write values into arr[]
        for (int i = 0; i < 20; i++) {
            arr[i] = i;
            cout << setw(20) << i << fixed
                 << setw(20) << setprecision(10) << arr[i]
                 << setw(20) << &arr[i] << endl;
        }
        
        // Loop to read from the array by using pointers
        for (int i = 0; i < 20; i++) {
            cout << setw(20) << i << fixed
                 << setw(20) << setprecision(10) << *(pa + i)
                 << setw(20) << pa + i << endl;
        }
    }
    

    以上代码实现了实验室任务的所有要求,从基本指针操作到通过值和引用参数传递给函数,以及指针和数组之间的关系。每个部分的代码都设计为清晰、可读,并遵循了给定的指导方针。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(15条)

报告相同问题?

问题事件

  • 系统已结题 6月24日
  • 已采纳回答 6月16日
  • 创建了问题 6月14日