Author Archives: carlos green

How to be successful in Ken’s class

1017  

Flipping class is a really cool concept of learning, instead of just going to class everyday and wait for the teacher to show all the work, you can study by yourself and interact with problemas given by the professor to then, solve these problems and doubts in class. The concept is pretty cool because it lets students learn how to look up for info by themselves and be able to learn stuff independently (ofcourse always with help and advice of the teacher)!

here is the youtube link: http://youtu.be/_yS3rzdRp6k?hd=1

mastery 23

23 1017 ++ 

Vectors, how to create one and a little intro to using them to store elements (no size)

here is the git link:https://github.com/carlosgreen/TC1017/blob/master/vectors.cpp

here is the youtube tutorial link:https://www.youtube.com/watch?v=-bGTeb2HibQ&feature=youtu.be&hd=1

Final Project

1017

This final project was a great experience to use lots of funtions, loops, conditionals, vectors and arrays.  Everything that we learned through out the semester was really helpful to connect the ideas of the boardgame with functions that helped the user interact with the program.

We started our project by creating a sudoku board which was given in a text file. After giving the paramters to the program (from the text file) we desided to change some things from the board such as a row and a column of numbers for the user to give of the coordenates of the numbers inside the board. The purpose was to create a sudoku game, with enough functions to be able to write on it, erase from it (just the numbers given by the user), print the board as many times the user wanted to, restart the game as many times the function was called and also be able to quit the game. 

For the text format from the board, we used a series of conditionals including string files, using the numbers from the text file to add lines to it.
Originally the file was like this:

5 3 0 0 7 0 0 0 0

6 0 0 1 9 5 0 0 0

0 9 8 0 0 0 0 6 0

8 0 0 0 6 0 0 0 3

4 0 0 8 0 3 0 0 1

7 0 0 0 2 0 0 0 6

0 6 0 0 0 0 2 8 0

0 0 0 4 1 9 0 0 5

 

0 0 0 0 8 0 0 7 9

But after the modifications on the board that we created it looked like this:

In order to display the numbers like that and also display the data, we used a vector inside another to store the user’s choice. We also created functions to check if the number entered by the user was already in the same row or column and therefore, tell the user that the number entered couldn´t be registered. 

The same concept was used for numbers bigger than nine or smaller than 1. Whenever the user provided a number 9 then, the program displayed a mesage asking the user to provide a number within the parameters from the game. 

Of course in order for the user to understand the whole concept we displayed some instructions.

At the end of the code, are a bunch of conditionals which store the user’s choice (print, erase, write, quit, restart) in order to call our functions and display a message. 

Github link: https://github.com/carlosgreen/Sudoku_Caro_Carlos/tree/master

mastery 28

 

here is the youtube link to mastery 28: https://www.youtube.com/watch?v=x2EqFB5OYjs&feature=youtu.be&hd=1

Sometimes reading files is really important because you may find a problem in which you need to open a text file in order to gather information about something and then use it in your c++ code. For that, it is really important to learn how to open and read files. 

mastery22

 

Here is the youtube link to mastery 22: https://www.youtube.com/watch?v=6ElQFCcUb2o&feature=youtu.be&hd=1

there a different types of loops and they can be used depending in the situation you are facing.

 

Mastery18

 

Here is the youtube link to mastery 18: https://www.youtube.com/watch?v=CeLnp1FGiB4

So basically nested conditional statements allow us to create an if-else statement that validates more than two options for one variable, here is an example:

<iostream>

 

using namespace std;

int main(){

int a;

cout<<“give me a: “<<endl;

cin>>a;

if (a<0){

  cout<<“less”<<endl;

}else {

  if(a==0){

    cout<<“equals “;

  }else{

    cout<<“greater “;

  }

}

return 0;

}

Mastery12

 

Here is the youtube link to mastery12: https://www.youtube.com/watch?v=kNgEAiYSGkg

Functions in c++ are really important in order to do lots of repetitive stuff. Instead of writing down the code for a sum or something like that, you can just call a function that does that every time you want. Here is a simple example:

<iostream>

using namespace std;

 

int function1(int a, int b){

  int c;

  c=a*b;

  cout<<“c: “<<c<<endl;

  return 0;

}

int main(){

  int a,b;

  cout<<“give me a:”<<endl;

  cin>>a;

  cout<<“give me b:”<<endl;

  cin>>b;

  function1(a,b);

}

WSQ 17

 

Scilab is a pretty cool and friendly open source software that alouds you to interact with a really sweet system of maths and simulations. It also lets you play around with 2 and 3D simulations which is amazing due to compatively issues in other programs. 

This software is amazing for engineering purposes because it alouds you to interact using tools to perform data analisis and modeling. Also, it features a set of tools for mechanics modeling and control systme duty. 

One of the best parts from scilab is that you can interact with it using external tools. And coding is really confortable, because its simple and clear.

 

WSQ 16

Link to github: https://github.com/carlosgreen/TC1017/blob/master/cars.cpp

This program needs to read a file which has a data base with various information relevant to cars. By reading the information, the program needs to print out the follow data:

  • average gas mileage in city (City MPG)
  • average gas mileage on highway (Highway MPG)
  • average midrange price of the vehicles in the set.

The program opens the file and then uses a string variable in order to read stuff from the subline (substring aka substr).

Also, the program has a if inside a loop, which permits the file to be read several times until getting the average midrange price of a vehicle. 

Here is how the code should look:

<iostream>

<fstream>

<sstream>

<cstdlib>

<string>

 

using namespace std;

int cars(string carstext){

  string range, city, highway;

  int counter=0;

  float range2=0.0, city2=0.0, highway2=0.0, sumcity=0.0, sumrange=0.0, sumhigh=0.0;

  ifstream cars;

  string line;

  cars.open(carstext.c_str());

  while(getline(cars, line)){

    if((counter%2)==0){

      range=line.substr(42, 47);

      range2=atoi(range.c_str());

 

      city=line.substr(53, 54);

      city2=atoi(city.c_str());

 

      highway=line.substr(56, 57);

      highway2=atoi(highway.c_str());

 

      sumrange=sumrange + range2;

      sumcity=sumcity + city2;

      sumhigh=sumhigh + highway2;

 

    counter++;

  }

  }

  cout<<“city mileage: “<<sumcity/93<<endl;

  cout<<“highway mileage: “<<sumhigh/93<<endl;

  cout<<“midrange vehicle price: “<<sumrange/93<<endl;

}

int main(){

  cout<<cars(“93cars.txt”);

 

}

 

QUIZ #11

Here is the gitbub link to both exercises: https://github.com/carlosgreen/TC1017/blob/master/bananas.cpp and https://github.com/carlosgreen/TC1017/blob/master/readnumbers.cpp

When using external files, you can open them with a program and read them line by line in order to receive information relevant for your program.

In this case, the first code will read a series of numbers that were placed line by line and then add those numbers.

In order to do that, we will need to open the file, and create a vector to store all the values in each line.

Also, we will need to transform string characters (the numbers from the file) into integers and for that we will use the function (atoi). 

Here is an example of how the code should look:

<iostream>

<fstream>

<sstream>

<cstdlib>

<string>

<vector>

<cmath>

using namespace std;

 

int ReadNumbersFromFiles (string filename){

 

  int number_of_lines = 0;

  int sum = 0;

  int x;

  float deb;

  int mean;

  float sdev;

  float var;

  vector <int> numbers;

  ifstream myFile;

  myFile.open(filename.c_str());

  string line;

  int numLines = 0;

 

while ( getline(myFile, line) ){

  ++numLines;

  x = atoi(line.c_str());

  sum = sum + x;

  numbers.push_back(x);

}

      mean= sum/ numLines;

      for(int i = 1; i <= numLines; i++){

              deb = (x – mean)*(x – mean);

              sdev = sdev + deb;

          }

      var = sdev / (numLines – 1);

      deb= sqrt(var);

      cout << “Number of lines in text file: ” << numLines<<endl;

      myFile.close();

      cout<<“the sum of the numbers: “<< sum<<endl;

      cout<<“mean: “<<mean<<endl;

      cout<<“The standard deviation: “<<deb;

    }

 

int main(){

  string file;

//  cout<<“please enter the name of the file you wish to open: “<<endl;

//  cin>>file;

  ReadNumbersFromFiles(“numbers.txt”);

}

Bananas:

The program needs to find a word from a text file and then print out where it find it no matter the way the word (STRING) is arranged:

<iostream>

<iostream>

<fstream>

<sstream>

<cstdlib>

<string>

 using namespace std;

 

 int bananas(string banana){

   string str2=”banana”;

   ifstream myFile;

   myFile.open(banana.c_str());

   string line;

   size_t found;

    while ( getline(myFile, line) ){

     found = line.find(str2);

        if (found!=string::npos)

            cout << “first ‘banana’ found at: ” <<found<<endl;

            found=line.find(“banana”,found+1);

          if (found!=string::npos)

            cout << “second ‘banana’ found at: ” << found <<endl;

 

            found=line.find(“banana”,found+2);

          if (found!=string::npos)

            cout << “third ‘banana’ found at: ” << found <<endl;

 

            found=line.find(“banana”,found+4);

          if (found!=string::npos)

            cout << “forth ‘banana’ found at: ” << found <<endl;

 

            found=line.find(“banana”,found+5);

          if (found!=string::npos)

            cout << “fifth ‘banana’ found at: ” << found <<endl;

    }

 

    myFile.close();

 

  return 0;

}

int main(){

  bananas(“bananas.txt”);

}