susanLand

chap 14 recursive. problem number 1

solve for the nth Fibonacci number recursively.

below is the solution to 14.1:

****************************************************************

#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;

int fib(int num);
int main()
{
int num = 0, nth = 0;
cout<< “Enter a integer n for the nth Fibonnaci number:\n”;
cin >> num;
nth = fib(num);
cout <<“fibonnci number is: “<< nth << endl;

system (“pause”);
return 0;

}
int fib(int num)
{
if (num <=1)      // stopping cases
return num;
else                           // recursive case
return fib(num – 1) + fib(num – 2);
}
/*****output****************************************************

Enter a integer n for the nth Fibonnaci number:
8
fibonnci number is: 21
Press any key to continue . . .

***************************************************************/

these past couple of days I have been busy celebrating New Year. So today when when I turned on my computer and started browsing the internet with firefox, I kept getting redirected to other pages of ads and all these shitty sites. It turned out that I got this nasty virus called NTLOAD.DLL.

IF YOU HAVE THIS, THEN I CAN RECOMMEND A REALLY GOOD SOFTWARE TO REMOVE IT and it is FREE!!

I downloaded this program called Regrun from this place http://www.greatis.com/security/

It works great and i removed the infected file along with virus. It also has a Regrun Watch Dog that scans every 10 min and pops up if anything strange enters your computer. Really helpful.

Just be careful not to accidentally remove any important windows shell or kernel boot up files because Regrun suspected some of those as well. Only delete the file with ntload.dll in it. okey dokey. I hope this is helpful.

btw, I will start posting some sample c++ solutions soon (rest assured that they will all run fine because they are all approved by my professor), but from my previous post, I can see that the indentations doesn’t show up. So I will try to attach some sort of file .cpp. Please if you have a specific question or suggestion, please leave a comment. Thank you.

from savitch’s problem solving with C++, chapter 17 templates.

problem 17.4 solution and 7.3 solution

********************************************************************************

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;

template<class T>void fillArray(T sentence[], int size, int& numberUsed);
/*
precondition: size > numberUsed; numberUsed > 0
postcondition: numberUsed is the number of char in sentence[]
*/
template<class T>void deleteRepeats(T sentence[], int& numberUsed);
/*
precondition: numberUsed > 0
postcondition: deletes the repeated letters, and leaves the non repeating ones
*/
template<class T>void output(T sentence[], int& numberUsed);
/*
precondition: numberUsed > 0
postcondition: output the results of deleteRepeats
*/

int main()
{
char sentence[50], ans;
int numberUsed = 0;

do{
fillArray(sentence, 50, numberUsed);
deleteRepeats(sentence, numberUsed);
output(sentence, numberUsed);
cout <<“Do you want to calculate again? Enter Y for yes, N for no.” << endl;
cin >> ans;
}while (ans == ‘y’ || ans == ‘Y’);

cout << “Good bye!!\n”;
system(“pause”);
return 0;
}

template<class T>void fillArray(T sentence[], int size, int& numberUsed)
{
char next;
int index = 0;

cout << “Please type in a sentence and then press enter.\n”;
cin.get(next);

while (next != ‘\n’ && index < size)
{
sentence[index] = next;
index++;
cin.get(next);
}
numberUsed = index;
}

template<class T>void deleteRepeats(T sentence[], int& numberUsed)
{
for (int i = 0; i < numberUsed; i++)
{
for (int j = i + 1; j < numberUsed; j++)
{
if (sentence[i] == sentence[j])
{
for (int k = j; k < numberUsed; k++)
sentence[k] = sentence[k + 1];
numberUsed–;
}
}

}
sentence[numberUsed] = 0;
}

template<class T>void output(T sentence[], int& numberUsed)
{
cout << “The new sentence is:\n”;

for (int i = 0; i < numberUsed; i++)
{
cout << sentence[i];
}
cout << “\nThe new size is: ” << numberUsed << endl;
}

/*********************************output****************************************
Please type in a sentence and then press enter.
12544578541
The new sentence is:
125478
The new size is: 6
Do you want to calculate again? Enter Y for yes, N for no.
y
Please type in a sentence and then press enter.
info invite history seems to be offline
The new sentence is:
info vtehsryembfl
The new size is: 17
Do you want to calculate again? Enter Y for yes, N for no.
n
Good bye!!
Press any key to continue . . .

*******************************************************************************/

I hope this is helpful. please leave a comment so i know how to improve and add other info. thanx so much.

This is a comprehensive blog/journal of my life, i will have photos, comments, reviews, about all aspects of my life. so for those looking for specific things, please go to the individual categories.

Hallo! This is Susan. the main purpose of this blog is to help people who want to master c++, but having a hard time grasping. if you are looking for a companion who can provide insights, experience gained from past mistakes, then my blog is the best source. i recently  finished a C++ course, so im familiar with the topics and problems.  i googled the solutions but there isnt any. so i thought, after im done with this course i would like to post solutions online for free. these would be under the c++ category. btw, im using Problem solving with C++ by Savitch. i usually post by chapter, under titles such as 2.4, meaning chapter 2, problem 4. please take advantage of my hardwork, and leave me comments shareing all your knowledge as well so that i can improve. thank you!