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[5],i;
     int *p=arr;
     cout<<"Enter five numbers separated by space:";
     cin>>*p>>*(p+1)>>*(p+2)>>*(p+3)>>*(p+4);
     cout<<"Your numbers are:\n";
     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[5],i;
     int *p=arr;
     cout<<"Enter five numbers separated by space:";
     cin>>*p>>*(p+1)>>*(p+2)>>*(p+3)>>*(p+4);
     cout<<"Your numbers are:\n";
     for(i=4;i>=0;i--)
        cout<<*(p+i)<<endl;
    
     getch();
     return 0;
 
    }


Comments

2018-6-13 comment

 2018-6-13

struct Medicine{
int id,quantity;
double price;
char medicneName[100];
char Mfg_Date[11];
char Exp_Date[11];
char info[5000];
}
declaretoin of structure for medicine store


2018-06-13
lexshard comment

 lexshard

#include <iostream>
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<size; i++)
{
cin >> ptr[i];
}
}
void display (int* ptr, int size)
{
for (int i=0; i<size; i++)
{
cout <<"the values entered: "<<ptr[i]<<endl;
}

}


2018-03-07
lexshard comment

 lexshard

#include <iostream>
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<size; i++)
{
cin >> ptr[i];
}
}
void display (int* ptr, int size)
{
for (int i=0; i<size; i++)
{
cout <<"the values entered: "<<ptr[i]<<endl;
}

}


2018-03-07
Nouman comment

 Nouman

http://courses.cs.vt.edu/cs2604/SummerI_2006/OQ/3/Q3.C++s.pdf
Here is the Link for the s exercise


2017-07-02
Nouman comment

 Nouman

http://courses.cs.vt.edu/cs2604/SummerI_2006/OQ/3/Q3.C++Pointers.pdf
Here is the Link for the pointers exercise


2017-07-02
Your name comment

 Your name

Type comment


2017-04-03
Bazib Abdullah comment

 Bazib Abdullah

I need more pointer related questions for practice. Please tell me the questions.


2017-03-01
Jazib Abdullah (mjazibabdullah comment

 Jazib Abdullah (mjazibabdullah

I need more pointer related questions for practice. Please tell me the questions.


2016-12-28
Jazib Abdullah comment

 Jazib Abdullah

I need more pointer related questions for practice. Please tell me the questions.


2016-12-28
kashif Raza rind comment

 kashif Raza rind

good nice job <3


2016-08-25



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.