Question

In: Computer Science

Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...

Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below into your main function first, and then fill in your statements.

// 1. Create two integer variables named x and y
// 2. Create an int pointer named p1
// 3. Store the address of x in p1
// 4. Use p1 to set the value of x to 99
// 5. Using cout and x, display the value of x
// 6. Using cout and the pointer p1, display the value of x
// 7. Store the address of y into p1
// 8. Use p1 to set the value of y to -300
// 9. Create two new variables: an int named temp, and an int pointer named p2
// 10. Use temp, p1, and p2 to swap the values in x and y (this will take a few statements)
// 11. Write a function with the following signature: void noNegatives(int *x). The function should accept the address of an int variable. If the value of this integer is negative then it should set it to zero
// 12. Call the function twice: once with the address of x as the argument, and once with the address of y
// 13. Use p2 to display the values in x and y (this will require both assignment statements and cout statements)
// 14. Create an int array with two elements. The array should be named ‘a’
// 15. Use p2 to initialize the first element of a with the value of x
// 16. Use p2 to initialize the second element of a with the value of y
// 17. Using cout, display the address of the first element in a
// 18. Using cout, display the address of the second element in a
// 19. Use p1, p2, and temp to swap the values in the two elements of array ‘a’. (first point p1 at a[0], then point p2 at a[1]. After this the swapping steps should look very similar to step 10.)
// 20. Display the values of the two elements. (The first element should be 99, the second 0).
// 21. Write a function named ‘swap’ that accepts two integer pointers as arguments, and then swaps the two integers that the pointers point to.  This function must be pass by pointer, i.e. int *, not pass by reference, i.e. int &.
// 22. Call your swap function with the addresses of x and y, then print their values. (x should be 99, y should be 0).
// 23. Call your swap function with the address of the two elements in array ‘a’, then print their values. (a[0] should be 0, a[1] should be 99)

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU



#include <iostream>

using namespace std;
int x, y;               // 1. Create two integer variables named x and y
int noNegatives(int *x) // 11. Write a function with the following signature: void noNegatives(int *x). The function should accept the address of an int variable. If the value of this integer is negative then it should set it to zero {
{
        if (*x < 0)
        {
                *x = 0;
        }
        return *x;
}
void swap(int *a, int *b)
{
        int temp;

        temp = *a;
        *a = *b;
        *b = temp;

        cout << *a <<" "<< *b << endl;
}

int main()
{
        int *p1;             // 2. Create an int pointer named p1
        p1 = &x;             // 3. Store the address of x in p1
        *p1 = 99;            // 4. Use p1 to set the value of x to 99
        cout << x << endl;   // 5. Using cout and x, display the value of x
        cout << *p1 << endl; // 6. Using cout and the pointer p1, display the value of x
        p1 = &y;             // 7. Store the address of y into p1
        *p1 = -300;          // 8. Use p1 to set the value of y to -300
        int temp, *p2;       // 9. Create two new variables: an int named temp, and an int pointer named p2
        p2 = &x;             // 10. Use temp, p1, and p2 to swap the values in x and y (this will take a few statements)
        temp = *p1;
        *p1 = *p2;
        *p2 = temp;

        noNegatives(&x);
        noNegatives(&y); // 12. Call the function twice: once with the address of x as the argument, and once with the address of y

        p2 = &x; // 13. Use p2 to display the values in x and y (this will require both assignment statements and cout statements)
        cout << *p2 << endl;
        p2 = &y;
        cout << *p2 << endl;

        int a[1]; // 14. Create an int array with two elements. The array should be named ‘a’
        p2 = &x;
        a[0] = *p2; // 15. Use p2 to initialize the first element of a with the value of x
        p2 = &y;
        a[1] = *p2;            // 16. Use p2 to initialize the second element of a with the value of y
        cout << &a[0] << endl; // 17. Using cout, display the address of the first element in a
        cout << &a[1] << endl; // 18. Using cout, display the address of the second element in a

        p1 = &a[0];
        p2 = &a[1];
        cout << "test" << *p1 << *p2 << endl;

        temp = *p1; // 19. Use p1, p2, and temp to swap the values in the two elements of array ‘a’. (first point p1 at a[0], then point p2 at a[1]. After this the swapping steps should look very similar to step 10.)
        *p1 = *p2;
        *p2 = temp;

        cout << a[0] << endl;
        cout << a[1] << endl; // 20. Display the values of the two elements. (The first element should be 99, the second 0).
        // 21. Write a function named ‘swap’ that accepts two integer pointers as arguments, and then swaps the two integers that the pointers point to. This function must be pass by pointer, i.e. int *, not pass by reference, i.e. int &.
        swap(&x, &y);     // 22. Call your swap function with the addresses of x and y, then print their values. (x should be 99, y should be 0).
        swap(&a[0],&a[1]); // 23. Call your swap function with the address of the two elements in array ‘a’, then print their values. (a[0] should be 0, a[1] should be 99)
        return 0;
}

Related Solutions

Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below...
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt...
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt and determines the following: The number of uppercase letters in the file The number of lowercase letters in the file The number of digits in the file Prompts And Output Labels. There are no prompts-- nothing is read from standard in, just from the file text.txt. Each of the numbers calculated is displayed on a separate line on standard output, preceded by the following...
Write a C program that, given a file named Program_2.dat as input, determines and prints the...
Write a C program that, given a file named Program_2.dat as input, determines and prints the following information: The number of characters in the file. The number of uppercase letters in the file. The number of lowercase letters in the file. The number of words in the file. The number of lines in the file. Your program should assume that the input file, Program_2.dat, may contain any text whatsoever, and that text might be, or might not be, the excerpt...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100 random integers between -50 and +50 (inclusive) to the file. Be sure to handle any file IO exceptions. Remember to close the file. Write a program that opens rand_nums.txt for input. Create two output files pos.txt and neg.txt. Read through the input file, one line at a time, converting each line into an integer (no exception handling, yet). If the number is positive, write...
Submit a c++ file: 2. Write a program that defines the named constant PI, const double...
Submit a c++ file: 2. Write a program that defines the named constant PI, const double PI = 3.14159;, which stores the value of p. The program should use PI and the functions listed in Table 6-1 to accomplish the following: a. Output the value of Pi Output the value of Pi. b. Prompt the user to input the value of a double variable r, which stores the radius of a sphere. The program then outputs the following: i.   The...
For this assignment, I need to write a c program named stick which plays a matchstick-picking...
For this assignment, I need to write a c program named stick which plays a matchstick-picking game. Given an initial number of sticks, players take turns picking either 1, 2, 3 or 4 sticks from a pile. Whoever picks the last stick wins. Usage The user can run stick with or without command line arguments. i.e. somebody can enter the number of sticks to begin play when the program is launched or they can be prompted after the program begins...
For this assignment, I need to write a c program named stick which plays a matchstick-picking...
For this assignment, I need to write a c program named stick which plays a matchstick-picking game. Given an initial number of sticks, players take turns picking either 1, 2, 3 or 4 sticks from a pile. Whoever picks the last stick wins. Usage You run stick with or without command line arguments. i.e. somebody can enter the number of sticks to begin play when the program is launched or they can be prompted after the program begins running. Print...
For this assignment, I need to write a c program named stick which plays a matchstick-picking...
For this assignment, I need to write a c program named stick which plays a matchstick-picking game. Given an initial number of sticks, players take turns picking either 1, 2, 3 or 4 sticks from a pile. Whoever picks the last stick wins. Usage You run stick with or without command line arguments. i.e. somebody can enter the number of sticks to begin play when the program is launched or they can be prompted after the program begins running. Print...
Write a program that reads a file (provided as attachment to this assignment) and write the...
Write a program that reads a file (provided as attachment to this assignment) and write the file to a different file with line numbers inserted at the beginning of each line. Such as Example File Input: This is a test Example File Output 1. This is a test. (Please comment and document your code and take your time no rush).
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT