#WSQ12 – Word Count

Hey people, we are in the finish line, so, let’s make a little more effort to finish this curse.

So, heré’s what we need to do:

  • Create a program that asks the user for a word which will be your search word and the name of a file to open and search for that word. Then create a function that will receive two parameters (both string) representing those two data points. This function returns the number of occurrences of that word in that file

Seems like a difficult one, but it isn’t, check this out:

WSQ12

If you have any question ask in the comments section below.

Let’s keep moving forward people:)

Quiz 7

Last quiz everyone!!!!! jajajaja in this quiz I had to calculate the dot product of two vectors “seen in physics”, using arrays.

//CODE:

#include <iostream>
using namespace std;

double dot_product(double list1[], double list2[], int l1){
double dot=0;
for(int i=0; i<l1; i=i+1){
dot=dot+(list1[i]*list2[i]);
}
return dot;
}

int main(){
int l1;
double product;

cout<<“This program help to calculate the dot product of the lists of values given by the user.”<<endl;
cout<<“Please enter the values to the first list.”<<endl;
cout<<“How many caracters the lists have?”<<endl;
cin>>l1;
double list1[l1], list2[l1];

for(int i=0; i<l1; i=i+1){
cout<<“Type the ” <<i+1<<” “<<“value of the 1st list.”<<” “;
cin>>list1[i]; }
cout<<“Please enter the values to the second list.”<<endl;

for(int i=0; i<l1; i=i+1){
cout<<“Type the ” <<i+1<<” “<<“value of the 2nd list”<<” “;
cin>>list2[i]; }

product=dot_product(list1, list2, l1);
cout<<“The dot product of both lists is:”<<” “<<product<<endl;
return 0;
}

Captura de pantalla 2016-04-30 a las 21.57.50.png

Quiz 6

Write a function to calculate the greatest common denominator of two positive integers using Euclid’s algorithm.

//CODE:

#include <iostream>
using namespace std;

int gcd(int x, int y){
int r;
if (x==0){
return x;
}
while (y!=0){
r= y;
y= x%y;
x= r;
}
return r;
}

int main (){
int x,y;
cout<<“Please enter a number: ” <<endl;
cin>>x;
cout<<“Please enter a second number: ” <<endl;
cin>>y;
cout<<“The greatest common denominator between (” <<x <<” y ” <<y <<“) is = ” << gcd(x,y) <<endl;

return 0;
}

Captura de pantalla 2016-04-30 a las 21.54.25.png

Captura de pantalla 2016-04-30 a las 21.56.18

Quiz 5

Determine if the word given by the user is a palindrome or not, by making the word backwards and comparing it to the original word with a simple “if condition”.

//CODE:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

string word, fin;

string is_palindrome(string word)

{
string backwards;
cout << “Type a word: n”;
cin >> word;

int large = word.length();
for (int x = large – 1; x >= 0; x–)
{
backwards += word[x];
}
if (backwards == word)
{
fin = “Your word IS a palindrome”;
}
else
fin = “Your word IS NOT a palindrome”;
return fin;
}

int main()
{
cout << is_palindrome(fin);
}

Captura de pantalla 2016-04-30 a las 21.53.33.png

Quiz 4

Create a function called euler_calc with a single parameter precision. The value of precision is used to determine when to stop calculating. Your calculation will stop when the two consecutive values estimating e differ by less than precision

 

//CODE

#include <iostream>
#include <iomanip>
using namespace std;

double factorial(int n){
double fact = 1;
for (int i = 1; i <= n; i++){
if (n==0){
return 1;
}
else {
fact = fact * i;
}
}
return fact;
}
float euler_calc (float ac){
float e = 1.0;
for (int i=1; i<999; i++){
e = e + 1/(factorial(i));
}
cout<< fixed <<setprecision(ac) << e;
return e;
}
int main(){
float ac;
cout<<“How accurate you want the result? “;
cin>>ac;
cout<<“Here is your number: “<<endl;
euler_calc(ac);
cout<<endl;
return 0;
}

Captura de pantalla 2016-04-30 a las 21.49.28.png

#WSQ13-Partial exam 2

Here is the partial exam answered of course jajajaja so the exam just had four questions and I will list them in order.

QUESTION 1:

Write a function called triangles which receives a single parameter (int) which represents the size of a triangle as explained below. The function should print a triangle using loops (for or while). The only characters printed here are ‘T’ and the new-line character. The first line is length one, the middle line is length size and the last line is length one. The example below is for size 6.

      T
      TT
      TTT
      TTTT
      TTTTT
      TTTTTT
      TTTTTT
      TTTTT
      TTTT
      TTT
      TT
      T

//CODE

// Ever Olivares
#include <stdio.h>
#include <iostream>
using namespace std;

int triangle (int x)
{
for (int i=1; i<=x ;i++)
{
for (int j=1; j<i+1; j++)
cout <<“T”; cout <<endl;
}
for (int i=x-1; i>=1; i–)
{
for (int j=1; j<i+1; j++)
cout <<“T”; cout <<endl;
}
return 0;
}
int main ()
{
int y;
std::cout<<“type the numbers of T you want” <<std::endl;
std::cin >> y;
triangle (y);
}

Captura de pantalla 2016-04-30 a las 21.38.49.png

QUESTION 2:

Write a function called superpower that has two parameters of type long and returns a long which is first parameter raised to the power of the second, which is to say it returns ab So, superpower(3,4) would return 81.

//CODE:

// Ever Oliavres
#include <iostream>
#include <cmath>

void superpower(int num1, int num2){
int R;
R= pow(3,4);
std::cout << “The result is… “<<R << std::endl;
}

int main(int argc, char const *argv[]) {
int R;
superpower(3,4);

return 0;
}

Captura de pantalla 2016-04-30 a las 21.40.17.png

QUESTION 3:

Write a function called fibonacci which receives a long “n” and returns a long which is the value of the nth number in the fibonacci series which is: 0,1,1,2,3,5,8,13,21,34,55,89…………So, fibonacci(0) would return 0. fibonacci(5) would return 5, fibonacci(8) would return

Captura de pantalla 2016-04-30 a las 21.42.16.png
Captura de pantalla 2016-04-30 a las 21.43.21.png

Continue reading “#WSQ13-Partial exam 2”

#WSQ12-Word count

This one was cool but I do not know if it really work because it compiles and runs but do not count any words :(… anyway here it is.

//CODE

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int find_word(string word, string tipo){
string line;
int cont=0;
ifstream myfile(tipo);
if (myfile.is_open()){

while ( getline (myfile,line) ){
if(‘line’ == ‘word’){
cont=cont+1;
}
// cout << line << endl;
}
myfile.close();
}
else {
cout<<“CANNOT OPEN THE FILE”<<endl;
}
cout<<“The word…”<<word<<“… is used “<<cont<<” times.”<<endl;
return cont;
}
int main(int argc, char const *argv[]) {
string word, tipo;
int cont;
std::cout << “Please type the word you want to search.” << std::endl;
std::cin >> word;
std::cout << “Please enter the name of the file to search the word” << std::endl;
std::cin >> tipo;
find_word(word, tipo);
return 0;
}

Captura de pantalla 2016-04-30 a las 21.09.17.png

#WSQ11-Yo soy 192

This one in particular was a challenge… but with help everything is possible. Also for this WSQ I had to “add” a new library so I could work with bigger numbers it is called “BigIntegerLibrary.hh”.

//CODE

#include <iostream>
#include <string>
#include “BigIntegerLibrary.hh”
using namespace std;

BigInteger lychrel (BigInteger x){

string y = bigIntegerToString(x);
lychrel(y.begin(), y.end());
BigInteger lychrel = stringToBigInteger(y);
return lychrel;
}

int main() {
int low,up,number,j,k,p=0;
int l=0, n=0;
BigInteger z,count=0, sum=0;

cout << “Give me the lower bound”;
cin>>low;

cout<< “Give me the upper bound”;
cin>>up;

for(count=low;count<=up;count++){
for(BigInteger i=count; i<=count;i++){

z= lychrel(i); sum=y+count;}

if(count==z) {
n=n+1;
} else {
int times= 0;

BigInteger zeit = count;
z=lychrel(zeit);

while(zeit != z && times < 30){

zeit = zeit + z;
z = lychrel(zeit);
times=times+1;
}
if(times < 30){
p = p + 1;
} else {
cout << “Lychrel found ” << count << endl;
l = l +1;
}
}
}

cout <<“The palindrom is: ” << n <<endl;
cout<<“Lychrel doesn’t become palindrome in: “<< p <<endl;
cout<<“The lychrel is: “<< l <<endl;

return 0;
}

Captura de pantalla 2016-04-30 a las 21.04.04Captura de pantalla 2016-04-30 a las 21.04.09