hello, need some help in C++. I asked before and someone did some of them which
ID: 3828686 • Letter: H
Question
hello, need some help in C++. I asked before and someone did some of them which i will include however you dont have to use it if you dont want. I need it to run
Write a program that performs the following tasks.
1)Declare an int variable room and assign an integer value 12 to it. Declare a pointer variable roomptr that points to room. Write statements using address-of operator and dereferencing operator to output the address and the value of room, the address and the value of roomptr , and the value pointed to by roomptr . Observe the output.
int room = 12;
int *roomptr = &room;
cout<<"Address: "<<roomptr<<", Value : "<<*roomptr<<endl;
2)Declare an int array arrayA with five elements, and initialize the elements to the odd integers from 1 to 9. Declare an int array arrayB with five elements, and initialize the elements to the even integers from 2 to 10.
int arrayA[5] = {1,3,5,7,9};
int arrayB[5] = {2,4,6,8,10};
3)Declare a pointer variable ptrA that points to arrayA. Declare a pointer ptrB variable that points to arrayB
int *ptrA = arrayA;
int *ptrB = arrayB;
4)Use a for statement to output the value of each element of arrayA using pointer offset notation combined with each element of arrayB using pointer offset notation to print out the contents of both arrays in ascending order.
for(int i=0; i<5; i++){
cout<<*(ptrA+i)<<endl;
cout<<*(ptrB+i)<<endl;
}
5)Use a for statement to output the value of each element of arrayA using array subscript notation combined with each element of arrayB using array subscript notation to print out the contents of both arrays in ascending order.
6)Write statements to increment each element of arrayA by 10 using pointer variable ptrA.
7)Use a function addTen to complete task 6 on arrayB. The function addTen uses a pointer variable as one of the formal parameters.
Explanation / Answer
Here re did all and put it in executable code as asked.
#include <iostream>
using namespace std;
void addTen(int *arr, int n)
{
// question 7
for(int i=0; i<n; i++)
{
*(arr + i) = *(arr + i) + 10;
}
}
int main()
{
int room = 12;
int *roomptr = &room;
cout<<"Address: "<< &room << ", Value : "<<*roomptr<<endl;
cout<<"Address: "<< &roomptr << ", Value : "<<roomptr<<endl;
int arrayA[] = {1,3,5,7,9};
int arrayB[] = {2,4,6,8,10};
int *ptrA = arrayA;
int *ptrB = arrayB;
cout << "using pointer"<< endl;
for(int i=0; i<5; i++)
{
cout<<*(ptrA+i)<<endl;
cout<<*(ptrB+i)<<endl;
}
// question 5
cout << "using subscript"<< endl;
for(int i=0; i<5; i++)
{
cout<<arrayA[i]<<endl;
cout<<arrayB[i]<<endl;
}
// question 6
for(int i=0; i<5; i++)
{
*(ptrA + i) = *(ptrA + i) + 10;
}
addTen(arrayB, 5);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.