Uploaded by hassanrajabtengegeya

Lab 10.1 solution@kijazi

1. Create a structure called employee that contains two members: an employee
number (type int) and the employee‘s compensation (in Tshs; type float). Ask
the user to fill in this data for three employees, store it in three variables of
type struct employee, and then display the information for each employee.
#include <iostream>
using namespace std;
struct employee{
int employee_number;
float compesatiom;
};
int main(){
employee employee[3];
for (int i=0; i<3; i++){
cout<<"enter employee number of employee"<<(i+1)<<endl;
cin>>employee[i]. employee_number;
cout<<"enter compesation of employee"<<(i+1)<<endl;
cin>>employee[i].compesatiom;
}
for (int i=0; i<3; i++){
cout<<"_______________________________"<<endl;
cout<<"employee employee number for employee"<<(i+1)<<"is"<<endl;
cout<<employee[i]. employee_number <<endl;
cout<<"employee compesation for employee"<<(i+1)<<"is"<<endl;
cout<<employee[i].compesatiom<<endl;
}
}
2. Using the structure formed in question 1:
a. Write C++ code to implement a linked list of 15 nodes. Display all data from the linked list
#include <iostream>
using namespace std;
struct employee{
int employee_number;
float compesation;
employee*next;
};
int main(){
employee*head=NULL;
employee*mynode;
employee*trav;
for (int i=0; i<3; i++){
mynode=new employee();
cout<<"enter value of employee number for node"<<(i+1)<<endl;
cin>>mynode->employee_number;
cout<<"enter value of compesation for node"<<(i+1)<<endl;
cin>>mynode->compesation;
mynode->next=NULL;
if(head==NULL){
head=mynode;
}
else{
trav=head;
while(trav->next!=NULL){
trav =trav->next;
}
trav->next=mynode;
}
}
int i=0;
employee*display=head;
while(display!=NULL){
cout<<"employee number for node"<<(i+1)<<endl;
cout<<display->employee_number<<endl;
cout<<"compesation for node"<<(i+1)<<endl;
cout<<display->compesation<<endl;
display=display->next;
i++;
}
}
b. b. Write a code segment to delete 6th to 10th nodes. Display all remained
data from the linked list.
#include <iostream>
using namespace std;
struct employee{
int employee_number;
float compesation;
employee*next;
};
int main(){
employee*head=NULL;
employee*mynode;
employee*trav;
for (int i=0; i<15; i++){
mynode=new employee();
cout<<"enter value of employee number for node"<<(i+1)<<endl;
cin>>mynode->employee_number;
cout<<"enter value of compesation for node"<<(i+1)<<endl;
cin>>mynode->compesation;
mynode->next=NULL;
if(head==NULL){
head=mynode;
}
else{
trav=head;
while(trav->next!=NULL){
trav =trav->next;
}
trav->next=mynode;
}
}
employee*del1;
employee*del2;
for(int i=0; i<5; i++){
del1=head;
del2=head->next;
for (int i=1; i<5; i++){
del1=del1->next;
del2=del2->next;
}
del1->next=del2->next;
del2->next=NULL;
delete del2;
}
int i=0;
employee*display=head;
while(display!=NULL){
cout<<"employee number for node"<<(i+1)<<endl;
cout<<display->employee_number<<endl;
cout<<"compesation for node"<<(i+1)<<endl;
cout<<display->compesation<<endl;
display=display->next;
i++;
}
}
3. Create a structure of type date that contains three members: the month, the day of the month, and
the year, all of type int. (Or use day-month-year order if you prefer). Have the user enter a date in the
format 12/31/2001, store it in a variable of type struct date, then retrieve the values from the variable
and print them out in the same format.
#include <iostream>
using namespace std;
struct date {
int month;
int day;
int year;
};
int main() {
date d;
cout << "Enter date in the format mm/dd/yyyy: "<<endl;
cout<<"enter month: ";
cin>>d.month;
cout<<"enter day: ";
cin>>d.day;
cout<<"enter year: ";
cin>>d.year;
cout<<"The date is: " <<d.month<<"/"<<d.day<<"/"<<d.year;
}
4. Using the structure formed in question 2:
a. Write C++ code to implement a linked list of 15 nodes. Display all data
from the linked list
#include <iostream>
using namespace std;
struct employee{
int employee_number;
float compesation;
employee*next;
};
int main(){
employee*head=NULL;
employee*mynode;
employee*trav;
for (int i=0; i<3; i++){
mynode=new employee();
cout<<"enter value of employee number for node"<<(i+1)<<endl;
cin>>mynode->employee_number;
cout<<"enter value of compesation for node"<<(i+1)<<endl;
cin>>mynode->compesation;
mynode->next=NULL;
if(head==NULL){
head=mynode;
}
else{
trav=head;
while(trav->next!=NULL){
trav =trav->next;
}
trav->next=mynode;
}
}
int i=0;
employee*display=head;
while(display!=NULL){
cout<<"employee number for node"<<(i+1)<<endl;
cout<<display->employee_number<<endl;
cout<<"compesation for node"<<(i+1)<<endl;
cout<<display->compesation<<endl;
display=display->next;
i++;
}
}
b. Write a code segment to delete the last 5 nodes. Display all remained
data from the linked list.
#include <iostream>
using namespace std;
struct employee{
int employee_number;
float compesation;
employee*next;
};
int main(){
employee*head=NULL;
employee*mynode;
employee*trav;
for (int i=0; i<15; i++){
mynode=new employee();
cout<<"enter value of employee number for node"<<(i+1)<<endl;
cin>>mynode->employee_number;
cout<<"enter value of compesation for node"<<(i+1)<<endl;
cin>>mynode->compesation;
mynode->next=NULL;
if(head==NULL){
head=mynode;
}
else{
trav=head;
while(trav->next!=NULL){
trav =trav->next;
}
trav->next=mynode;
}
}
employee*del1;
employee*del2;
for(int i=0; i<5;i++){
del1=head;
del2=head->next;
while(del2->next!=NULL){
del1=del1->next;
del2=del2->next;
}
del1->next=del2->next;
del2->next=NULL;
delete del2;
}
int i=0;
employee*display=head;
while(display!=NULL){
cout<<"employee number for node"<<(i+1)<<endl;
cout<<display->employee_number<<endl;
cout<<"compesation for node"<<(i+1)<<endl;
cout<<display->compesation<<endl;
display=display->next;
i++;
}
}
5. What is sorting Algorithms? Discuss.
Sorting algorithms are a set of instructions that take an unordered collection of data, such as an array
or a list, and arrange the data in a specific order. The most common orders are ascending and
descending. There are many different sorting algorithms, each with their own strengths and
weaknesses. Some of the most popular sorting algorithms include bubble sort, insertion sort,
selection sort, quicksort, and merge sort.
a. Write a C++ code to implement bubble sort algorithms
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
b. Show how arr[] = {5, 3, 43, 8, 17, 20, 1, 35, 60, 51} is sorted by the
algorithm in 5(a)
b. Here is an example of how the array {5, 3, 43, 8, 17, 20, 1, 35, 60, 51} would be sorted using the
bubble sort algorithm:
1st pass: {3, 5, 8, 17, 20, 1, 35, 43, 51, 60}
2nd pass: {3, 5, 8, 17, 1, 20, 35, 43, 51, 60}
3rd pass: {3, 5, 8, 1, 17, 20, 35, 43, 51, 60}
4th pass: {3, 5, 1, 8, 17, 20, 35, 43, 51, 60}
5th pass: {3, 1, 5, 8, 17, 20, 35, 43, 51, 60}
6th pass: {1, 3, 5, 8, 17, 20, 35, 43, 51, 60}
The final sorted array is {1, 3, 5, 8, 17, 20, 35, 43, 51, 60}