Question

In: Computer Science

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 comment in the program that indicates which step is being completed in the following statements. Easiest way to do this is by copying and pasting below in the main function first, and then fill in the 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

I've declared and defined functions namely noNegatives(int* x) and swap(int*,int*) above main function and called them inside the main(). Please find below the complete code for first task. Please note that the comments are as according to what was provided in problem, additional comments are added to make the program more understandable. Copy the code below and save the file as pointerTasks.cpp and run the code to generate .exe file as output.

#include <iostream>

using namespace std;
//Start debugging the code from the main()function

// 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;

// 6. Using cout and the pointer p1, display the value of x
cout<<"\nValue of x using *p1 : "<<*p1;

// 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=*p2;
*p2=*p1;
*p1=temp;

// 11. Write a function with the following signature: void noNegatives(int *x)
//The function definition is provided above the main() function at line #7

// 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)
p2=&x;
cout<<"\nValue of x using *p2 : "<<*p2;
p2=&y;
cout<<"\nValue of y using *p2 : "<<*p2;

// 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<<"\nAddress of first element in \'a\' : "<<&a[0];

// 18. Using cout, display the address of the second element in a
cout<<"\nAddress of first element in \'a\' : "<<&a[1];

// 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=*p2;
*p2=*p1;
*p1=temp;

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

// 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 &.
//function swa(int*,int*) is defined at line #13 of this program above main() function

// 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);  //Function call for swap(int*,int*) defined at line #13
cout<<"\nValue of x : "<<x;
cout<<"\nValue of y : "<<y;

// 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<<"\nFirst element of array \'a\' : "<<a[0];
cout<<"\nSecond element of array \'a\' : "<<a[1];

return 0;  //main() ends here
}
//program ends

Output:-

Value of x : 99
Value of x using *p1 : 99
Value of x using *p2 : 0
Value of y using *p2 : 99
Address of first element in 'a' : 0x61ff04
Address of first element in 'a' : 0x61ff08
First element of array 'a' : 99
Second element of array 'a' : 0
Value of x : 99
Value of y : 0
First element of array 'a' : 0
Second element of array 'a' : 99

Note:- Address of variables may be allocated differently on your device

Happy Learning.....

Give it a thumps up if you got the answer....


Related Solutions

The League with DMA Rewrite your League program from Assignment 8 so that it uses Dynamic...
The League with DMA Rewrite your League program from Assignment 8 so that it uses Dynamic Memory Allocation (DMA) to create the team names and scores arrays. This is a good test of the modularity of your program. You will only need to make slight modifications to your main() function if you wrote your original program using functions similar to the following: void initializeData(string names[], int wins[], int size) void sort(string names[], int wins[], int size) void display(string names[], int...
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...
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...
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....
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....
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...
Write C++ programs to perform the following tasks. In the program descriptions below, example input and...
Write C++ programs to perform the following tasks. In the program descriptions below, example input and output is provided. NOTE: You don’t need arrays to solve any of these problems. You should NOT use arrays to solve any of these problems. • stat.cpp: Let the user input a one or more integers, space separated, on a single line (as seen below), then work out and display the sum, average, sum of squares and population variance of the numbers. Remember, you...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
The main tasks of this C++ programming assignment include implementing a transformation matrix and a view/projection...
The main tasks of this C++ programming assignment include implementing a transformation matrix and a view/projection matrix. Giving three 3D points v0(2.0, 0.0, −2.0), v1(0.0, 2.0, −2.0), v2(−2.0, 0.0, −2.0),you are required to transform these points to the camera/view/monitor coordinates system, and draw a lined triangle based on them get_projection_matrix(float eye_fov, float aspect_ratio, float zNear, float zFar): using the giving parameter, build a projection matrix, and return it. Here is what I have so far. Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,...
Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT