Answer to Question #208912 in C++ for Ahmed Ali

Question #208912

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
Expert's answer
2021-06-20T08:11:19-0400

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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS