Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help for Part 1 and Part 2 of this assignment about C++ Pointer Variables

ID: 3860302 • Letter: I

Question

I need help for Part 1 and Part 2 of this assignment about C++ Pointer Variables. I want to check if the code is right.

https://www.dropbox.com/s/s6bvy79p3m9yyiz/Assignment%2010.docx?dl=0

Code for Part 1:

#include<iostream>

using namespace std;

int main() {

double number1 = 1, number2 = 2, number3 = 3;

double *ptr1 = &number1, *ptr2 = &number2, *ptr3 = &number3;

cout << "Three variable content: " << number1 << " " << number2 << " " << number3 << endl << endl;

cout << "Three pointer content: " << ptr1 << " " << ptr2 << " " << ptr3 << endl << endl;

cout << "Values of pointer content: " << *ptr1 << " " << *ptr2 << " " << *ptr3 << endl << endl;

system("pause");

return 0;

}

Code for Part 2:

#include<iostream>

using namespace std;

const int SIZE = 10;

int main() {

int numbers[SIZE] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 };

char ch[SIZE] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T' };

int * iptr = numbers;

char *cptr = ch;

for (int i = 0; i < SIZE; i++)

{ //every element

cout << *(cptr + i) << " ";

}

cout << endl;

for (int i = 0; i < SIZE; i += 2)

{ //every other element

cout << *(iptr + i) << " " << endl;

}

system("pause");

return 0;

}

Thank you. I will apreciate if somebody can help me with the solution.

Explanation / Answer

1. Part 1 was correct. Just modified it so that the output is more readable and understandable.

2. Part 2 needed change. You had used the loop counter i while outputting the values. But the question says you should not use loop counter and rather use pointer arithmetic. So modified the code to do the same.

3. Implemented the part 3 as well.

Given below are the modified files. Please do rate if the the answer helped. Thank you

part1.cpp

#include<iostream>
using namespace std;
int main() {
double number1 = 1, number2 = 2, number3 = 3;
  
double *ptr1 = &number1, *ptr2 = &number2, *ptr3 = &number3;
  
cout << "Three variable contents: " << endl;
cout<< "number1: " << number1 << endl;
cout << "number2: " << number2 << endl;
cout << "number3: " << number3 << endl << endl;
  
cout << "contents of pointer: " << endl;
cout << "ptr1: " << ptr1 << endl;
cout << "ptr2: " << ptr2 << endl;
cout << "ptr3: " << ptr3 << endl << endl;
  
cout << "Values pointed by pointers: " << endl;
cout << "ptr1 points to " << *ptr1 << endl;
cout << "ptr2 points to " << *ptr2 << endl;
cout << "ptr3 points to " << *ptr3 << endl << endl;
system("pause");
return 0;
}

output

Three variable contents:
number1: 1
number2: 2
number3: 3

contents of pointer:
ptr1: 0x7fff5fbff610
ptr2: 0x7fff5fbff608
ptr3: 0x7fff5fbff600

Values pointed by pointers:
ptr1 points to 1
ptr2 points to 2
ptr3 points to 3

part2.cpp

#include<iostream>
using namespace std;
const int SIZE = 20;

int main() {
int numbers[SIZE] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 };
char ch[SIZE] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T' };
int * iptr = numbers;
char *cptr = ch;
  
cout << "printing EVERY element of char array using cptr " << endl;
for (int i = 0; i < SIZE; i++)
{ //every element
  
cout << *cptr << " ";
cptr++;
}
  
cout << endl;
  
cout << "printing EVERY OTHER element of int array using iptr" << endl;
for (int i = 0; i < SIZE; i += 2)
{ //every other element
cout << *iptr << " " ;
iptr += 2; //increment pointer by 2 , every other element
}
  
system("pause");
return 0;
}

output

printing EVERY element of char array using cptr
A B C D E F G H I J K L M N O P Q R S T
printing EVERY OTHER element of int array using iptr
1 3 5 7 9 11 13 15 17 19

part3.cpp

#include <iostream>
using namespace std;

void referenceSwap(int &n1, int &n2); //swap the numbers using reference
void pointerSwap(int *p1, int *p2); // swap the numbers in location pointed to by p1 and p2

int main()
{
int m = 5, n = 10;
  
cout << "m = " << m << ", n = " << n << endl;
  
cout << "using referenceSwap() to swap m and n " << endl;
referenceSwap(m, n);
cout << "m = " << m << ", n = " << n << endl;
  
cout << "using pointerSwap() to swap m and n " << endl;
pointerSwap(&m, &n);
cout << "m = " << m << ", n = " << n << endl;
  
}

void referenceSwap(int &n1, int &n2) //swap the numbers using reference
{
int temp = n1;
n1 = n2;
n2 = temp;
}
void pointerSwap(int *p1, int *p2) // swap the numbers in location pointed to by p1 and p2
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}

output

m = 5, n = 10
using referenceSwap() to swap m and n
m = 10, n = 5
using pointerSwap() to swap m and n
m = 5, n = 10

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote