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

I need help with this C++ Pointers Variables code. For part 2 I do not know why

ID: 3860442 • Letter: I

Question

I need help with this C++ Pointers Variables code. For part 2 I do not know why it shows the half of integers numbers located in the array. NOTE:  part 2 should not use loop counter and rather use pointer arithmetic. It will be grateful if somebody can help me with the code, Thanks.

The steps are here:

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

And the code is here:

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

Explanation / Answer

#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;
}