﻿ C++ exercises solutions: C++ pointers

C++ exercises solutions: C++ pointers

C++ pointer exercises

1. Write a C++ program to accept five integer values from keyword.

The five values will be stored in an array using a pointer. Then print the elements of the array on the screen.

Solution:

#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
int arr,i;
int *p=arr;
cout<<"Enter five numbers separated by space:";
cin>>*p>>*(p+1)>>*(p+2)>>*(p+3)>>*(p+4);
for(i=0;i<5;i++)
cout<<arr[i]<<endl;

getch();
return 0;

}

2. Modify the solution of exercise 1 in order to print the elements of the array in reverse order using a pointer.

Solution:

#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
int arr,i;
int *p=arr;
cout<<"Enter five numbers separated by space:";
cin>>*p>>*(p+1)>>*(p+2)>>*(p+3)>>*(p+4);
for(i=4;i>=0;i--)
cout<<*(p+i)<<endl;

getch();
return 0;

} 2018-6-13 struct Medicine{ int id,quantity; double price; char medicneName; char Mfg_Date; char Exp_Date; char info; } declaretoin of structure for medicine store 2018-06-13 lexshard#include using namespace std; void display (int*, int); void assigValue (int*, int); const int MAX = 5; int main () { int array[MAX]; int* ptr = array; assigValue (ptr, MAX); display (ptr, MAX); cin.get(); cin.get(); return 0; } void assigValue (int* ptr, int size) { cout <<"Enter the five values\n"; for (int i=0; i> ptr[i]; } } void display (int* ptr, int size) { for (int i=0; i using namespace std; void display (int*, int); void assigValue (int*, int); const int MAX = 5; int main () { int array[MAX]; int* ptr = array; assigValue (ptr, MAX); display (ptr, MAX); cin.get(); cin.get(); return 0; } void assigValue (int* ptr, int size) { cout <<"Enter the five values\n"; for (int i=0; i> ptr[i]; } } void display (int* ptr, int size) { for (int i=0; i