This is C and I have two programming include include include

This is C++. and I have two programming.

#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>

#include <cppunit/Test.h>
#include <cppunit/TestFailure.h>
#include <cppunit/portability/Stream.h>

class ProgressListener : public CPPUNIT_NS::TestListener {
public:

    ProgressListener()
            : m_lastTestFailed(false) {
    }

    ~ProgressListener() {
    }

    void startTest(CPPUNIT_NS::Test *test) {
        CPPUNIT_NS::stdCOut() << test->getName();
        CPPUNIT_NS::stdCOut() << \"\ \";
        CPPUNIT_NS::stdCOut().flush();

        m_lastTestFailed = false;
    }

    void addFailure(const CPPUNIT_NS::TestFailure &failure) {
        CPPUNIT_NS::stdCOut() << \" : \" << (failure.isError() ? \"error\" : \"assertion\");
        m_lastTestFailed = true;
    }

    void endTest(CPPUNIT_NS::Test *test) {
        if (!m_lastTestFailed)
            CPPUNIT_NS::stdCOut() << \" : OK\";
        CPPUNIT_NS::stdCOut() << \"\ \";
    }

private:
    /// Prevents the use of the copy constructor.
    ProgressListener(const ProgressListener &copy);

    /// Prevents the use of the copy operator.
    void operator=(const ProgressListener &copy);

private:
    bool m_lastTestFailed;
};

int main() {
    // Create the event manager and test controller
    CPPUNIT_NS::TestResult controller;

    // Add a listener that collects test result
    CPPUNIT_NS::TestResultCollector result;
    controller.addListener(&result);

    // Add a listener that print dots as test run.
    ProgressListener progress;
    controller.addListener(&progress);

    // Add the top suite to the test runner
    CPPUNIT_NS::TestRunner runner;
    runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
    runner.run(controller);

    // Print test in a compiler compatible format.
    CPPUNIT_NS::CompilerOutputter outputter(&result, CPPUNIT_NS::stdCOut());
    outputter.write();

    return result.wasSuccessful() ? 0 : 1;
}

One more,

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>

using array_index = int;
void printAddress(int value);
void fillArrayWithRandomData(int data[], int size, int min, int max);
int main(int argc, char **argv) {
    // Create and initialize an array of four integers. Since this is a local
    // variable, the memory allocated for these four integers resides in the
    // current stack frame.
    int intArray[] = {1, 2, 3, 4};

    // Create a pointer to an int. Since we\'re using new, this integer resides
    // in heap memory
    int *intPtr = new int;

    std::cout << \"Printing values and addresses of array elements...\"
              << std::endl;

    // Use a for-loop to iterate through the array
    for (array_index i = 0; i < 4; ++i) {
        printAddress(intArray[i]);
    }

    std::cout << std::endl;

    std::cout << \"Printing value and address of a pointer...\" << std::endl;
    // Notice how we dereference the pointer so that we\'re passing an int to
    // the function and not a pointer to an int
    printAddress(*intPtr);

    std::cout << std::endl;

    std::cout << \"Printing values and address of pointers...\" << std::endl;
    // Using pointer arithmetic, let\'s put new values into memory.
    // NOTE: Question 7a refers to this for-loop
    for (array_index i = 0; i < 4; ++i) {
        *(intPtr + i) = i + 1;
        printAddress(*(intPtr + i));
    }

    // Now that I\'m done with this pointer, let\'s free the memory it pointed to
    // NOTE: Question 7b refers to this delete statement.
    delete intPtr;

    // Demonstrate the concept of a dynamic array. Normally, an array\'s size
    // must be stated as a constant, i.e., it can\'t be done using an unknown
    // that is determined at runtime. Dynamic arrays allow their size to be
    // specified during runtime.
    std::cout << \"Enter a size between 1 and 10 followed by the [RETURN] key: \";
    int size;
    std::cin >> size;

    // NOTE: Question 8 refers to the following declaration
    int *dynamicArray = new int[size];

    fillArrayWithRandomData(dynamicArray, size, 0, 100);

    delete [] dynamicArray;

    return EXIT_SUCCESS;
}

void printAddress(int value) {
    std::cout << \"Value: \" << value
              << \" is stored at address: \" << &value
              << std::endl;
}

void fillArrayWithRandomData(int data[], int size, int min, int max) {
    // Seed with a real random value, if available
    std::random_device r;

    // Choose a random mean between min and max
    std::default_random_engine e1(r());
    std::uniform_int_distribution<int> uniform_dist(min, max);

    // Now let\'s fill this array with 0s...
    for (array_index i = 0; i < size; ++i) {
        // Notice I\'m using a pointer (dynamicArray) with array-like syntax
        data[i] = uniform_dist(e1);
        std::cout << \"dynamicArray[\" << i << \"] = \" << data[i]
                  << std::endl;
    }
}
Questions are here.

1. Why does the function printAddress(int value); always print the same address for every element in the array?
2. After changing the function\'s signature, why does the function printAddress(const int& value); now print different addresses for every
3. From the given addresses now printed out using this new function signature, deduce how many bytes are occupied by integers.
4. How could you modify this program to deduce how many bytes are occupied by long values?
5. What are some similarities between pointers and arrays?
6. What are some differences between pointers and arrays?
7a. In the for loop that puts new values into memory using pointer arithmetic, why might this be a \"dangerous\" thing to do?
7b. Why do you suppose that I can\'t delete (intPtr + 1)?

The answers to these last two questions (8a + 8b) is really beyond the scope of this class, but I\'m curious to see what you think... you will not be penalized for your answers to these last two questions.


8. What error message do you get when you remove the \"new\" keyword used in declaring the dynamic array?

Solution

1.
output :

Printing values and addresses of array elements...

Value: 1 is stored at address: 0xbff501c0

Value: 2 is stored at address: 0xbff501c0

Value: 3 is stored at address: 0xbff501c0

Value: 4 is stored at address: 0xbff501c0


Printing value and address of a pointer...

Value: 0 is stored at address: 0xbff501c0

Printing values and address of pointers...

Value: 1 is stored at address: 0xbff501c0

Value: 2 is stored at address: 0xbff501c0

Value: 3 is stored at address: 0xbff501c0

Value: 4 is stored at address: 0xbff501c0

It is function call by value so only local value will consider here.
Beacause we are printing the address of called function argument which in value So basically it is printing the address of value which is argument of called function.
That\'s why it giving the same.

2. This case is function call by address so we are pasing address of each element in our array so it is printing the different address corespondent to value passed in
function argument.

output:

Printing values and addresses of array elements...

Value: 1 is stored at address: 0xbfa3cee0

Value: 2 is stored at address: 0xbfa3cee4

Value: 3 is stored at address: 0xbfa3cee8

Value: 4 is stored at address: 0xbfa3ceec


Printing value and address of a pointer...

Value: 0 is stored at address: 0x9274008

Printing values and address of pointers...

Value: 1 is stored at address: 0x9274008

Value: 2 is stored at address: 0x927400c

Value: 3 is stored at address: 0x9274010

Value: 4 is stored at address: 0x9274014

3.

Byte occupied by integers = address(intArray[1]) - address(intArray[1])
           = 0xbfa3cee4 - 0xbfa3cee0
           = 4 bytes

4.change the data type of intArray to long and then by subtracting address(intArray[1]) - address(intArray[1]) we will get size of long.

5.
similarities :
   if we declare
       int array[5];
       then we can access element by array[0],array[1]....arra[i] where i is int between 0 to n-1.
   similarlly
   int *arrayP = &a[0];
       we can refer to *ip, *(ip+1), *(ip+2), etc., or to *(ip+i) where i is an int.
6.

differences :

   There are also differences, of course. You cannot assign two arrays; the code

   int a[10], b[10];
   a = b;               /* WRONG */
   is illegal. As we\'ve seen, though, you can assign two pointer variables:
   int *ip1, *ip2;
   ip1 = &a[0];
   ip2 = ip1;

7a.
   Beacause we create is intPtr integer using new keyword that is why it reside in heap area and we are incresing the heap address So it may be situation when our
   heap address cross all heap area and we are accessing some address which is outside of heap area.It is dangerous to access invalid memory area and can cause
   segmentation fault.

7b.
   intPtr is a integer which we create in our heap using area using dynamic memory allocation so can only delete this only.Not the value which may be adjacent to
   it (intPtr + 1 ot intPtr+2).

8.
   When we remove new keyword then we will get below compile time error.
   Error :
       prog.cpp: In function \'int main(int, char**)\':
prog.cpp:58:26: error: expected primary-expression before \'int\'

       int *dynamicArray = int[size];
^

This is C++. and I have two programming. #include <cppunit/BriefTestProgressListener.h> #include <cppunit/CompilerOutputter.h> #include <cppunit/
This is C++. and I have two programming. #include <cppunit/BriefTestProgressListener.h> #include <cppunit/CompilerOutputter.h> #include <cppunit/
This is C++. and I have two programming. #include <cppunit/BriefTestProgressListener.h> #include <cppunit/CompilerOutputter.h> #include <cppunit/
This is C++. and I have two programming. #include <cppunit/BriefTestProgressListener.h> #include <cppunit/CompilerOutputter.h> #include <cppunit/
This is C++. and I have two programming. #include <cppunit/BriefTestProgressListener.h> #include <cppunit/CompilerOutputter.h> #include <cppunit/

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site