1) Write a program that declares a structure to store the record of a book. It defines a structure variable, input the values and displays them using pointer.
2) Write a program that input two integers and passes them to a function using pointers. The function exchange the values and the program finally display the values.
1)
#include <iostream>
using namespace std;
struct Book
{
string book_name;
string book_no;
string book_author;
};
int main()
{
Book *ptr, b;
ptr = &b;
cout<<"\nEnter the name of the Book: ";
cin>>(*ptr).book_name;
cout<<"\nEnter the number of the Book: ";
cin>>(*ptr).book_no;
cout<<"\nEnter the author of the Book: ";
cin>>(*ptr).book_author;
cout<<"\nBook name: "<<(*ptr).book_name<<endl;
cout<<"\nBook number: "<<(*ptr).book_no<<endl;
cout<<"\nBook author: "<<(*ptr).book_author<<endl;
return 0;
}
2)
#include <iostream>
using namespace std;
void swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x,y;
cout<<"\nEnter a number: ";
cin>>x;
cout<<"\nEnter another number: ";
cin>>y;
cout<<"\nBefore swaping: ";
cout<<"\nx= "<<x<<"\ty= "<<y<<endl;
swap(&x,&y);
cout<<"\nAfter swaping: ";
cout<<"\nx= "<<x<<"\ty= "<<y<<endl;
return 0;
}
Comments
Leave a comment