Task: Make three different files specification file, implementation file and dri
ID: 3599634 • Letter: T
Question
Task: Make three different files specification file, implementation file and driver program to compile a program in project.
Create the Fahrinacci sequence in C++.
This Fahrinacci sequence is made by multiplying the first number by 3 and subtracting the second number.
Example: num1 = 4, num2 = 2
Sequence: 4, 2, 10, -4, 38, etc.
Processing: Calculate the Fahrinacci sequence and store the numbers in a doubly linked list.
Menu:
1. Ask for the first 2 numbers of the sequence (assume they don’t match) and how many terms there should be in the sequence. Then create the sequence.
2. Print the sequence of numbers.
3. Check to see if a number is in the sequence of numbers and how many times it occurs.
4. Display the two numbers that were added to make all occurrence of a number in the sequence.
5. Display the two numbers in the sequence that will use a number to create them. Ask the user for the number. Only need to check for the first occurrence.
6. Delete all occurrences of a number from the sequence. (Some of the other options in the menu will no longer make sense.)
7. Exit.
Program is to be well documented both internally and externally
Explanation / Answer
dlist.hpp
#include<iostream>
using namespace std;
//Node declaration
typedef struct node
{
int info;
struct node* prev;
struct node* next;
}Node;
//Class specification
class dlist
{
Node* head;
public:
dlist();
dlist(Node* head);
Node* getHead();
void insert(int value);
void print(void);
int findOccurence(int value);
void findParents(int value);
void findSuccessor(int value);
void remove(int);
};
dlist.cpp
#include<iostream>
#include "dlist.hpp"
using namespace std;
dlist::dlist(){
head = NULL;
}
dlist::dlist(Node* head){
this->head = head;
}
Node* dlist::getHead(){
return head;
}
void dlist::insert(int value){
Node* ptr = new Node;
int i = 0;
Node* temp = NULL;
if(ptr == NULL)
return;
ptr->info = value;
ptr->next = ptr->prev = NULL;
if(head == NULL){
head = ptr;
}
else{
temp = head;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = ptr;
ptr->prev = temp;
}
}
void dlist::print(void){
if(head == NULL){
return;
}
Node* ptr = head;
cout << "Sequence : ";
while(ptr != NULL){
cout<< ptr->info << " ";
ptr = ptr->next;
}
cout<< endl;
}
int dlist::findOccurence(int value){
if(head == NULL)
return 0;
int occurence = 0;
Node* ptr = head;
while(ptr != NULL){
if(ptr->info == value){
occurence++;
}
ptr = ptr->next;
}
return occurence;
}
void dlist::findParents(int value){
if(head == NULL){
cout<< "No such value found in the list" <<endl;
return;
}
Node* ptr = head;
int found = 0;
while(ptr != NULL){
if(ptr->info == value){
found = 1;
if(!((ptr == head) || (ptr == head->next))){
cout<< ptr->prev->prev->info << " and " << ptr->prev->info << " added to get " << value <<endl;
}
}
ptr = ptr->next;
}
if(found == 0)
cout<< "Value not found in the list" <<endl;
}
void dlist::findSuccessor(int value){
if(head == NULL){
cout<< "List empty" <<endl;
return;
}
int found = 0;
Node* ptr = head;
while(ptr != NULL){
if(ptr->info == value){
found = 1;
if(ptr == head){
if( (ptr->next) && (ptr->next->next) )
cout << ptr->next->next->info << " created using " << value <<endl;
}
else{
if( ptr->next){
cout<< ptr->next->info << " created using " <<value <<endl;
if(ptr->next->next){
cout<< ptr->next->next->info << " created using " << value <<endl;
}
}
}
}
ptr = ptr->next;
}
}
void dlist::remove(int value){
if(head == NULL){
cout<<"Sequence is empty!" <<endl;
return;
}
Node* ptr = head;
while(ptr != NULL){
if(ptr->info == value){
if(ptr == head){
if(head->next){
head->next->prev = NULL;
head = head->next;
ptr->next = NULL;
delete ptr;
}
}
else{
ptr->prev->next = ptr->next;
if(ptr->next){
ptr->next->prev = ptr->prev;
}
ptr->prev = NULL;
delete ptr;
}
}
ptr = ptr->next;
}
}
driver.cpp
/****************************************************************
*Program to generate the Fahrinacci sequence.
*
*Input: First two numbers in the sequence and the size of the
* sequence.
*Output: Fahrinacci sequence
*
* *************************************************************/
#include<iostream>
#include"dlist.hpp"
using namespace std;
int main()
{
int option = 0,occur = 0, size = 0,temp =0, i=0, num1 = 0, num2 =0 ;
dlist list;
do{
cout<<"Enter from below option : "<< endl;
cout<<"1.Initiate the sequence"<<endl;
cout<<"2.Print the sequence"<<endl;
cout<<"3.Find a number of occurence of a value in the sequence" <<endl;
cout<<"4:Print the parents of a number " <<endl;
cout<<"5.Print the successors of the number " <<endl;
cout<<"6.Delete a number " <<endl;
cout<<"7.Exit "<< endl;
cin>>option;
switch(option){
case 1:
cout<<"Enter first number : ";
cin>>num1;
cout<<"Enter second number : ";
cin>>num2;
cout<<"Enter size of the sequence : ";
cin >>size;
list.insert(num1);
list.insert(num2);
for(i=0;i < (size-2);i++){
temp = (num1*3) - num2;
num1 = num2;
num2 = temp;
list.insert(temp);
}
break;
case 2:
list.print();
break;
case 3:
cout<<"Enter the number to be found : " ;
cin >>temp;
occur = list.findOccurence(temp);
cout<<temp << " appeared " << occur << " times in the list"<< endl;
break;
case 4:
cout<<"Enter the number : ";
cin >> temp;
list.findParents(temp);
break;
case 5:
cout<<"Enter the number : ";
cin >> temp;
list.findSuccessor(temp);
break;
case 6:
cout<<"Enter the number : ";
cin >> temp;
list.remove(temp);
break;
case 7:
cout<<"Bye!"<<endl;
break;
default:
cout<<"Wrong option" <<endl;
break;
}
}while(option != 7);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.