0% found this document useful (0 votes)
41 views

COMSATS University Islamabad, Lahore Campus: Time 3:00 Hour Maximum Marks: 50

The document contains instructions for a programming assignment involving C/C++ code. It includes two questions. Question 1 asks to write a program to handle book information for a bookshop using a structure in an array. It should store book ID, name, author name, price and publish date for at least 50 books. Functions are provided to input data, display all books, and display a single book. Question 2 asks to initialize an integer array of size 10 with default values of 1. Functions are then to be written to input and display array values, calculate the sum of even and product of odd values, delete a value, and reverse the array. Code is provided implementing these functions to work with the array.

Uploaded by

Atif Javed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

COMSATS University Islamabad, Lahore Campus: Time 3:00 Hour Maximum Marks: 50

The document contains instructions for a programming assignment involving C/C++ code. It includes two questions. Question 1 asks to write a program to handle book information for a bookshop using a structure in an array. It should store book ID, name, author name, price and publish date for at least 50 books. Functions are provided to input data, display all books, and display a single book. Question 2 asks to initialize an integer array of size 10 with default values of 1. Functions are then to be written to input and display array values, calculate the sum of even and product of odd values, delete a value, and reverse the array. Code is provided implementing these functions to work with the array.

Uploaded by

Atif Javed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

COMSATS University Islamabad, Lahore Campus

Course Programming Fundamentals Course Code: CSC141 Credit Hours: 4


Title:
Course Shaina Ashraf Programme Name: BS Stats (
Instruct
Semeste 2nd Batch: FA20-BST Section: Date
r:
Time 3:00 Hour Maximum Marks: 50
Student Sharjeel ahmed Reg. No. Fa20-bst-011
Important Instructions / Guidelines:

 Assume only ‘C/C++’ programming language is considered wherever applicable


 All questions are compulsory
 Copy paste code and screen shots of output in this document (Mandatory)

Sessional-III Examination–Spring 2021

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

cout << "Displaying Information: " << endl;

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

cout << "Enter name: ";


cin >> s[i].name;

cout << "Enter marks: ";


cin >> s[i].marks;

cout << endl;


}

cout << "Displaying Information: " << 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;

cout << "\nDisplaying Information," << endl;


cout << "Name: " << s.name << endl;
cout << "Roll: " << s.roll << endl;
cout << "Marks: " << s.marks << endl;
return 0;
}

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

int main(int argc, char** argv) {


input_value();
Delete();
cout << "\nSum of even elements = " << Display_sum();
cout << "\nSum of even elements = " << Display_pro();
reverse_arr();
display();
return 0;
}

You might also like