COMSATS University Islamabad, Lahore Campus: Time 3:00 Hour Maximum Marks: 50
COMSATS University Islamabad, Lahore Campus: Time 3:00 Hour Maximum Marks: 50
Question-1: Write a program which should handle books information for a book shop. It should be
able to handle at least 50 records. Your program should store following information against a book.
(Note: use structure in array).
(A Part):Book Id (you may use index number as book ID)
Book Name
Author Name
Publish?
Ans : code
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
struct book {
char name[100];
char author_name[50];
int price;
int id;
int publish_date;
};
int main()
{
book b;
// storing information
for(int i = 0; i < 10; ++i)
{
cout << "\nEnter information of book";
cout << "\nEnter name of book\n";
cin.getline(b.name, 100);
cout << "Enter name of author\n";
cin.getline(b.author_name, 100);
cout << "Enter price of employee\n";
cin >> b.price;
cout << "Enter book ID\n";
cin >> b.id;
cout << "Enter publish date\n";
cin >> b.publish_date;
}
// Displaying information
for(int i = 0; i < 10; ++i)
{
cout << "Name : " << b.name << endl;
cout << "Price : " << b.price << endl;
cout << "ID : " << b.id;
cout << "Author : " << b.author_name << endl;
cout << "Publish Date : " << b.publish_date<<endl;
}
return 0;
}
Write a function to input 10 books data from user and insert 10 books data in array
Ans : Code
#include <iostream>
using namespace std;
struct student
{
char name[50];
int roll;
float marks;
} s[10];
int main()
{
cout << "Enter information of students: " << endl;
// storing information
for(int i = 0; i < 10; ++i)
{
s[i].roll = i+1;
cout << "For roll number" << s[i].roll << "," << endl;
// Displaying information
for(int i = 0; i < 10; ++i)
{
cout << "\nRoll number: " << i+1 << endl;
cout << "Name: " << s[i].name << endl;
cout << "Marks: " << s[i].marks << endl;
}
return 0;
}
Part:3
Write a function to display all information of books entered by user [5 Marks]
Ans: codes
#include <iostream>
using namespace std;
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
student s;
cout << "Enter information," << endl;
cout << "Enter name: ";
cin >> s.name;
cout << "Enter roll number: ";
cin >> s.roll;
cout << "Enter marks: ";
cin >> s.marks;
Question-2:
Initialize an integer array of size 10 with default values 1 and perform
following tasks:
a) Write a function to input all values of array from user [5 Marks]
b) Write a function to display all values in array [5 Marks]
c) Write a function to sum all even values in array and display even sum in main function [5
Marks]
d) Write a function to multiply all odd values in array and display result in main function [5 Marks]
e) Write a function to delete value entered by user [5 Marks]
f) Write a function to reverse array and return reversed array in main function [5 Marks]
Ans: code
#include <iostream>
using namespace std;
int arr[10]={1,1,1,1,1,1,1,1,1,1};
void input_value(){
cout << "\nEnter values for array\n";
for(int i=0;i<10;i++){
cin >> arr[i];
}
}
void display(){
cout << "\nArray elements";
for(int i=0;i<10;i++){
cout << arr[i] << endl;
}
}
void Delete(){
int tot,elem,found=0;
cout<<"\nEnter Element to Delete: ";
cin>>elem;
for(int i=0;i<tot;i++)
{
if(arr[i]==elem)
{
for(int j=i; j<(tot-1); j++)
arr[j] = arr[j+1];
found++;
i--;
tot--;
}
}
if(found==0)
cout<<"\nElement doesn't found in the Array!";
else
cout<<"\nElement Deleted Successfully!";
cout<<endl;
}
int Display_sum(){
int sum;
for(int i=0;i<=10;i++){
if(arr[i]%2==0){
sum+=arr[i];
}
}
return sum;
}
int Display_pro(){
int pro;
for(int i=0;i<=10;i++){
if(arr[i]%2!=0){
pro*=arr[i];
}
}
return pro;
}
int reverse_arr(){
for(int i=(10-1);i>=0;i--){
cout<<arr[i]<<" ";
cout<<endl;
}
}