Question

In: Computer Science

Pointer Tasks This part of the assignment will give you a chance to perform some simple...

Pointer Tasks

This part of the assignment will give you a chance to perform some simple tasks with pointers. The instructions below are a sequence of tasks that are only loosely related to each other. 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

//pointerTasks.cpp

#include<iostream>
using namespace std;

// 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
void noNegatives(int *x){
    if(*x < 0){
        *x = 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 &.
void swap(int* x, int* y) 
{ 
    int z = *x; 
    *x = *y; 
    *y = z; 
} 
  
int main(){

    // 1. Create two integer variables named x and y
    int x,y;

    // 2. Create an int pointer named p1
    int* p1;

    // 3. Store the address of x in p1
    p1 = &x;

    // 4. Use p1 to set the value of x to 99
    *p1 = 99;

    // 5. Using cout and x, display the value of x
    cout<<"value of x "<<x<<endl;

    // 6. Using cout and the pointer p1, display the value of x
    cout<<"value of x "<<*p1<<endl;
    
    // 7. Store the address of y into p1
    p1 = &y;

    // 8. Use p1 to set the value of y to -300
    *p1 = -300;

    // 9. Create two new variables: an int named temp, and an int pointer named p2
    int temp,*p2;

    // 10. Use temp, p1, and p2 to swap the values in x and y (this will take a few statements)
    p2 = &x;
    temp = *p1;
    *p1 = *p2;
    *p2 = temp;
    cout<<"value of x and y after swapping x="<<x<<" y="<<y<<endl;
    
    // 12. Call the function twice: once with the address of x as the argument, and once with the address of y
    noNegatives(&x);
    noNegatives(&y);

    // 13. Use p2 to display the values in x and y (this will require both assignment statements and cout statements)
    cout<<"now value of x "<<*p2<<endl;
    p2 = &y;
    cout<<"now value of y "<<*p2<<endl;
    
    // 14. Create an int array with two elements. The array should be named ‘a’
    int a[2];

    // 15. Use p2 to initialize the first element of a with the value of x
    p2 = &x;
    a[0] = *p2;

    // 16. Use p2 to initialize the second element of a with the value of y
    p2 = &y;
    a[1] = *p2;

    // 17. Using cout, display the address of the first element in a
    cout<<"Address of the first element "<<&a[0]<<endl;

    // 18. Using cout, display the address of the second element in a
    cout<<"Address of the second element "<<&a[1]<<endl;

    // 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 = &a[0];
    p2 = &a[1];
    temp = *p1;
    *p1 = *p2;
    *p2 = temp;

    // 20. Display the values of the two elements. (The first element should be 99, the second 0).
    cout<<"the values of the two elements "<<a[0]<<" "<<a[1]<<endl;

    // 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(&x,&y);
    cout<<"value of x and y are "<<x<<" "<<y<<endl;

    // 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)
    swap(&a[0],&a[1]);
    cout<<"the values of the two elements "<<a[0]<<" "<<a[1]<<endl;

    


}

OUTPUT:

value of x 99
value of x 99
value of x and y after swapping x=-300 y=99
now value of x 0
now value of y 99
Address of the first element 0x72fde0
Address of the second element 0x72fde4
the values of the two elements 99 0
value of x and y are 99 0
the values of the two elements 0 99

***Have doubt ask in comment***


Related Solutions

The C++ question: This part of the assignment will give you a chance to perform some...
The C++ question: This part of the assignment will give you a chance to perform some simple tasks with pointers. The instructions below are a sequence of tasks that are only loosely related to each other. 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....
1.This C++ assignment regards pointers and the league with DMA. First part asks to perform tasks...
1.This C++ assignment regards pointers and the league with DMA. First part asks to perform tasks using pointers. The instructions below are a sequence of tasks that are loosely related to each other. Start the assignment by creating a file names pointerTasks.cpp with an empty main function , then add the statements in main() to complete each of the tasks listed below. Some tasks only require a single C++ statement while others will require more. For each step, include a...
The goal of this part is to give you a chance to practice working through the...
The goal of this part is to give you a chance to practice working through the steps of calculating variance. Use the table of scores (X values) below to complete the following calculations. Assume the data in the table represents a sample, not a population. Use the extra columns in the table to show your work. X 20 22 18 15 21 Question 2.1 (0.5 points) – Calculate the deviation score for each x value Question 2.2 (0.5 points) –...
Assignment Details: Perform the following tasks: Complete the reading assignment and the interactive lesson before attempting...
Assignment Details: Perform the following tasks: Complete the reading assignment and the interactive lesson before attempting this assignment. Select a recent news article about a life event of an individual. It can be health related, accident related, educational, or even achievement-oriented. It will be one "slice in the lifespan of that person." For example, you might select a story of someone who has achieved a major goal in life after experiencing a debilitating accident. An example would be Nick Vujicic....
For this case study assignment, perform the following tasks: 1. Select an American company with a...
For this case study assignment, perform the following tasks: 1. Select an American company with a worldwide presence (examples: Starbucks, McDonalds, Walmart). 2. Do research on the worldwide economic crisis of 2008 and in particular, focus on the company selected. 3. Discuss how your chosen company faired in the economic crisis of 2008. 4. Discuss the microeconomic implications of the crisis on your company. 5. Discuss whether your company was immune or not immune to the crisis 6. Discuss the...
Assignment Overview IN C++ This assignment will give you practice with numerical calculations, simple input/output, and...
Assignment Overview IN C++ This assignment will give you practice with numerical calculations, simple input/output, and if-else statements. Candy Calculator [50 points] The Harris-Benedict equation estimates the number of calories your body needs to maintain your weight if you do no exercise. This is called your basal metabolic rate or BMR. The calories needed for a woman to maintain her weight is: BMR = 655 + (4.3 * weight in pounds) + (4.7 * height in inches) - (4.7 *...
Assignment Overview This assignment will give you practice with interactive programs and if/else statements. Part 1:...
Assignment Overview This assignment will give you practice with interactive programs and if/else statements. Part 1: User name Generator Write a program that prompts for and reads the user’s first and last name (separately). Then print a string composed of the first letter of the user’s first name, followed by the first five characters of the user’s last name, followed by a random number in the range 10 to 99. Assume that the last name is at least five letters...
Database Application Development Project/Assignment Milestone 1 (part 1) Objective: In this assignment, you create a simple...
Database Application Development Project/Assignment Milestone 1 (part 1) Objective: In this assignment, you create a simple HR application using the C++ programming language and Oracle server. This assignment helps students learn a basic understanding of application development using C++ programming and an Oracle database Submission: This Milestone is a new project that simply uses what was learned in the SETUP. Your submission will be a single text-based .cpp file including your C++ program for the Database Application project/assignment. The file...
Assignment Tasks This assignment consists of three parts as follows (100 marks): Part 1(30 marks): Write...
Assignment Tasks This assignment consists of three parts as follows : Part 1: Write a report of 300 words that covers the following:  Fixed Cost  Variable Cost  Profit Part 2 : Solve the following questions by completing the required calculations:  A factory has fixed cost of RO 10,000 and produces 500 units of a product at a variable cost of RO 75 per unit. Calculate Total Cost of the factory  Calculate variable cost per unit...
Discuss what we mean by pointer arithmetic and give some examples. Discuss the relationship between passing...
Discuss what we mean by pointer arithmetic and give some examples. Discuss the relationship between passing arrays to a function and using pointers to pass an array to a function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT