Roshene has two integer variables, x, and y. She wants to perform the following operations using pointers:Increment the value of x by 3.Decrement the value of y by 2.Multiply the values of x and y and store the result in x.Write a program to help Roshene achieve this and display the updated values of x and y.Input format :The input consists of two integer values x and y in separate lines.Output format :The first line of output displays "x = " followed by the updated value of x after multiplication.The second line displays "y = " followed by the decremented value of y.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ x ≤ 5001 ≤ y ≤ 450Sample test cases :Input 1 :59Output 1 :x = 56y = 7Input 2 :4512Output 2 :x = 480y = 10Input 3 :478356Output 3 :x = 170274y = 354
Question
Roshene has two integer variables, x, and y. She wants to perform the following operations using pointers:Increment the value of x by 3.Decrement the value of y by 2.Multiply the values of x and y and store the result in x.Write a program to help Roshene achieve this and display the updated values of x and y.Input format :The input consists of two integer values x and y in separate lines.Output format :The first line of output displays "x = " followed by the updated value of x after multiplication.The second line displays "y = " followed by the decremented value of y.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ x ≤ 5001 ≤ y ≤ 450Sample test cases :Input 1 :59Output 1 :x = 56y = 7Input 2 :4512Output 2 :x = 480y = 10Input 3 :478356Output 3 :x = 170274y = 354
Solution
Here is a C++ program that can help Roshene achieve her goal:
#include<iostream>
using namespace std;
int main() {
int x, y;
cin >> x >> y;
// Create pointers to x and y
int* ptrX = &x;
int* ptrY = &y;
// Increment the value of x by 3
*ptrX += 3;
// Decrement the value of y by 2
*ptrY -= 2;
// Multiply the values of x and y and store the result in x
*ptrX = *ptrX * *ptrY;
// Display the updated values of x and y
cout << "x = " << *ptrX << endl;
cout << "y = " << *ptrY << endl;
return 0;
}
This program first reads the values of x and y. Then it creates pointers to these variables. It increments the value of x by 3 and decrements the value of y by 2 using these pointers. Then it multiplies the values of x and y and stores the result in x. Finally, it prints the updated values of x and y.
Similar Questions
Write a program to add two numbers using pointers
Arun is developing a program that demonstrates the use of a constant pointer to a constant integer (const int *). He should take an integer input from the user, use a constant pointer to store its address, and then print both the original value of the integer and the value accessed through the constant pointer.Guide Arun to accomplish the task.Input format :The input consists of an integer.Output format :The first line of output prints "Original value of number: ", followed by the input integer.The second line prints "Value accessed through const pointer: ", followed by the value accessed through the pointer.Refer to the sample output for formatting specifications.Sample test cases :Input 1 :10Output 1 :Original value of number: 10Value accessed through const pointer: 10Input 2 :89Output 2 :Original value of number: 89Value accessed through const pointer: 89Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10Negative Marks : 0WhitelistSet 1:constSet 2:*Fill your code hereC (17)12// You are using GCCProvide Custom InputClear
Write a program that does basic arithmetic operations (addition, subtraction, multiplication,and division). The inputs to the program are two numbers (in double format) and theoperation required. Provide a function for each operation and the identifiers for addition,subtraction, multiplication, and division are ‘+’, ‘-‘, ‘*’, and ‘/’ respectively
Single File Programming QuestionProblem StatementArun is developing a program that demonstrates the use of a constant pointer to a constant integer (const int *). He should take an integer input from the user, use a constant pointer to store its address, and then print both the original value of the integer and the value accessed through the constant pointer.Guide Arun to accomplish the task.Input format :The input consists of an integer.Output format :The first line of output prints "Original value of number: ", followed by the input integer.The second line prints "Value accessed through const pointer: ", followed by the value accessed through the pointer.Refer to the sample output for formatting specifications.Sample test cases :Input 1 :10Output 1 :Original value of number: 10Value accessed through const pointer: 10Input 2 :89Output 2 :Original value of number: 89Value accessed through const
Create an array of integers, then create an array of pointers pointing to each element in the integer array. Print the values using both arrays. Write the code in c
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.