Question

In: Computer Science

C++ program, include comments stating what each part of code does please. I want to be...

C++ program, include comments stating what each part of code does please. I want to be able to understand it so I'll be more knowledgeable in the future. The program is multiple files(fibonacci.h file, fibonacci.cpp file, main.cpp file and loops_simple_data_test.cpp). After the directions I also included any starter code or comments left by my professor within the files to aide us.

Directions:

In folder 04_loops_simple_data write prototype and definition for string value - return function get_fibonacci with an int parameter that returns the fibonacci sequence up to that number. Write the required unit test(s) in folder 04_loops_simple_data_test. Main program flow: Program runs until user opts out. For each loop prompt user for a number, use number as function argument, call get_fibonacci function and display the output.

fibonacci.h

/*
Write prototype for string value-return function get_fibonacci with an int
parameter that returns the fibonacci sequence up to that number.
*/

fibonacci.cpp

/*
Write prototype for string value - return function get_fibonacci with an int
parameter that returns the fibonacci sequence up to that number.
DO NOT USE A RECURSIVE FUNCTION
*/

main.cpp

//Write include statements
//Write using statements
/*
Program runs until user opts out.
For each loop prompt user for a number, use number as function argument,
call get_fibonacci function and display the output.
*/
int main()
{
return 0;
}

loops_simple_data_test.cpp

#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
//Write includes
/*
Write test case for get fibonacci function with values
10 result "0, 1, 1, 2, 3, 5, 8"
and
5 result "0, 1, 1, 2, 3, 5"
*/

Solutions

Expert Solution

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks

fibonacci.h

#pragma once
#include<string>
   /*
   Write prototype for string value-return function get_fibonacci with an int
   parameter that returns the fibonacci sequence up to that number.
   */
class fibonacci
{
public:
   std::string get_fibonacci(int);
};

fibonnacci.cpp

#include "fibonacci.h"


std::string fibonacci::get_fibonacci(int num){
   std::string series="";
   int first=1,second=1,temp;
   if(num<0){
   return series;
   }else if(num==0){
       series="0 ";
   }

else if(num==1){
       series="0 1 1";
   }
   else{
       series="0 1 1";
       temp=first+second;
       while(temp<=num){
           series=series+" "+std::to_string(temp);
           first=second;
           second=temp;
           temp=first+second;
       }
   }
   return series;
}

Main.cpp

// fibonacciSequence.cpp : Defines the entry point for the console application.
//

#include "fibonacci.h"
#include "iostream"
using namespace std;


int getNumber(){
   int num;
   cin>>num;
   cin.clear();
   cin.ignore();
   return num;
}

char getChoice(){
   char ch;
   cin>>ch;
   cin.clear();
   cin.ignore();
   return ch;
}
void main(){
   fibonacci obj;
   int num;
   char choice;
   while(true){
       cout<<"Please give number :";
       num=getNumber();
       cout<<obj.get_fibonacci(num)<<endl;
       cout<<"Do you want to check again? (y) :";
       choice=getChoice();
       if(choice!='y'){
           break;
       }
   }
   system("pause");
}

loops_simple_data_test.cpp

// tests-main.cpp
#define CATCH_CONFIG_MAIN
#include "catch.hpp"

#include "fibonacci.h"

TEST_CASE("Testing Fibnacci") {
fibonacci testObject;

SECTION("First section, works") {
REQUIRE(obj.get_fibonacci(5)=="0 1 1 2 3 5");
}
SECTION("Second section, works") {
REQUIRE(obj.get_fibonacci(10)=="0 1 1 2 3 5 8");
}
SECTION("Second section, fails") {
REQUIRE(obj.get_fibonacci(10)=="1 1 2 3");
}
}

Output:


Related Solutions

I want a unique c++ code for the following. PLEASE HIGHLIGHT THESE FUNCTIONS WITH COMMENTS ....
I want a unique c++ code for the following. PLEASE HIGHLIGHT THESE FUNCTIONS WITH COMMENTS . Add the following functions to the class arrayListType: Then, update the main function to test these new functions. removeAll - which removes ALL of the instances of a value in the list min - which returns the minimum value in the list max - which returns the maximum value in the list arrayListType.h : #ifndef H_arrayListType #define H_arrayListType class arrayListType { public: bool isEmpty()...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
Please Complete this C Code using the gcc compiler. Please include comments to explain each added...
Please Complete this C Code using the gcc compiler. Please include comments to explain each added line. /*This program computes the Intersection over Union of two rectangles as a percent: IoU = [Area(Intersection of R1 and R2) * 100 ] / [Area(R1) + Area(R2) - Area(Intersection of R1 and R2)] The answer will be specified as a percent: a number between 0 and 100. For example, if the rectangles do not overlap, IoU = 0%. If they are at the...
I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
Assembly language: please comment on every line of code explaining each part. include head comments describing...
Assembly language: please comment on every line of code explaining each part. include head comments describing what your program does. Assignment 3A - A program that adds and subtracts 32-bit numbers After installing the assembler on the computer, enter the following program, save it, assemble it and run it. Do not forget to add a comment with your name in it. You will hand in a listing (e.g., addsum.asm) that should include your name ________________________________________ TITLE Add and Subtract (AddSum.asm)...
1. Write code in mips that will play battleships. Include comments in code on what each...
1. Write code in mips that will play battleships. Include comments in code on what each part is doing.
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h"...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h" #include"BST.cpp" using namespace std; std::vector<std::string> tokenize(char line[]) {    std::vector<std::string> tok;        std::string word = "";        for (int i = 0; i < strlen(line); i++)        {            if (i == strlen(line) - 1)            {                word += line[i];                tok.push_back(word);                return tok;       ...
i need C++ program with possible comments what is going on,(a) Write a program that reads...
i need C++ program with possible comments what is going on,(a) Write a program that reads a char as input, and determines if it is a lowercase letter, uppercase letter, a digit or something else (call the last one a special character).
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
I NEED THIS CODE FOR C++ IUSING SEMAPHORES PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ IUSING SEMAPHORES PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT