Warning: The magic method Slickr_Flickr_Plugin::__wakeup() must have public visibility in /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php on line 152

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101fall2015/wp-includes/feed-rss2.php on line 8
‘Doubs’ Articles at TC101 Fall 2015 https://kenscourses.com/tc101fall2015 Introduction to Programming Python and C++ Wed, 25 Nov 2015 06:25:49 +0000 en hourly 1 https://creativecommons.org/licenses/by/4.0/ Creation and use of Vectors in C++ https://kenscourses.com/tc101fall2015/2015/creation-and-use-of-vectors-in-c-2/ Wed, 25 Nov 2015 06:25:49 +0000 http://myfreakingcrazythoughts.wordpress.com/?p=292 Continue Reading →]]> A vector is a container in the C++ Standard Library (a bunch of stuff which sort of comes “bundled” with C++) which is essentially just an array that can grow and shrink in size. These things have been highly optimized and tried and tested for several years, and as such are generally considered a standard when creating C++ applications.

Vector Constructors.

The available constructors for a vector are given by:

1	vector<int> testVector;
2	vector<long> testVector(10);
3	vector<float> testVector(5,1.0);

 

The first syntax declares an empty vector capable of storing the integer datatype. The second declares a vector with storage space for 10 long integers, each of which is initialized to the default value for the type. The final line declares a vector with storage for 5 floats, and initializes each of their values to 1.0. Any valid type can be used for any of the constructors.

There is also a copy constructor for the std::vector class. The following code creates a vectors of integers with 10 copies of the number 5, and duplicates the vector into a new one using the copy constructor:

01	#include <iostream>
02	#include <vector>
03	 
04	using namespace std;
05	 
06	int main(int argc, char** argv) {
07	     
08	    vector<int> vectorOne(10,5);
09	     
10	    vector<int> vectorTwo(vectorOne);
11	     
12	    return EXIT_SUCCESS;
13	}

 

Accessing Elements of a Vector.

There are a number of ways to access the elements of a vector. For the moment, I will focus on two of them, one safe and one unsafe. And as a reminder, C++ vectors (and other STL containers), like raw C/C++ arrays, are accessed with indices starting at zero. This means that the first element is at position 0 in the vector, and the last element is at position (number of elements)-1.

The vector class contains a member function at() for accessing individual elements of a vector. This is the safe way of accessing elements, since attempting to access an element beyond the valid range will cause an exception to be thrown. However, the raw data stored in the vector can still be accessed using the usual [] operator, just like in a raw array. Unfortunately, just like with a raw array of data, overrunning the end of the vector using the [] operator can cause weird and unexpected things to occur, such as program crashes or unexpected results. It may also return garbage data that follows the meaningful data of the vector, which has the potential to be disastrous if it is used in subsequent operations. The following two code snippets demonstrate each of these access methods:

Safe access version:

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

int main(int argc, char** argv) {
     
    /*  Initialize vector of 10 copies of the integer 5 */

    vector<int> vectorOne(10,5);
     
    /*  Display size of vector */

    cout << "Size of vector is " << vectorOne.size() << " elements." << endl;
     
    /*  run through the vector and display each element, using size() to determine index boundary */

    for (long index=0; index<(long)vectorOne.size(); ++index) {

        cout << "Element " << index << ": " << vectorOne.at(index) << endl;
    }

    return EXIT_SUCCESS;
}

 

Unsafe access version

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

 
int main(int argc, char** argv) {
     

    /*  Initialize vector of 10 copies of the integer 5 */
    vector<int> vectorOne(10,5);
     

    /*  run through the vector and display each element, if possible */

    for (int index=0; index<20; ++index) {

        cout << vectorOne[index] << endl;

    }
    return EXIT_SUCCESS;
}

 

Sources:

http://www.cplusplus.com/reference/vector/vector/vector/

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

-The Admin.

]]>
https://creativecommons.org/licenses/by/4.0/
Yo soy 196! https://kenscourses.com/tc101fall2015/2015/yo-soy-196-5/ Sat, 31 Oct 2015 02:38:25 +0000 http://myfreakingcrazythoughts.wordpress.com/?p=158 Continue Reading →]]> In this lesson we had to do a really complicated program in which we had to ask the user for two values, a lower and a upper bound, and then the program will search for lychrel numbers, natural palindromes, and non-lychrel numbers.

I had to ask for help to my friend Marco and his blog! He advised me to create the codes and to execute the program and also, a video made by Ken helped me to use the big integer library properly.

Here is the code in my GitHub account!

And of course here is how the program looks like:

yosoy196 execution

Any doubt please let me know!

-The admin.

]]>
https://creativecommons.org/licenses/by/4.0/
Quiz #6! https://kenscourses.com/tc101fall2015/2015/quiz-6-17/ Thu, 08 Oct 2015 23:47:06 +0000 https://myfreakingcrazythoughts.wordpress.com/?p=115 Continue Reading →]]> For this quiz we had to create two programs.

In the first one we had to 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.

For example, superpower (3, 4) would return 81.

And in the second program we wrote a function called stars that has one parameter of type int and prints (the function does not return anything) that many stars on a single line followed by a end-of-line character, so if we call the function with stars (5), the function will print like this:

*****

At first it was hard to write this codes but then, when I looked for help I could notice that actually they were pretty easy and with a few minutes of reading I could understand a little bit more about how the functions work.

You can check the codes at the bottom of this post or in my GitHub account in the Quizzes Repository, it’s up to you!

Here are the codes of my two programs.

SuperPower!

#include <iostream>
#include <cmath>

using namespace std;

long superpower (long a, long b){
  long superpower = 0;
  superpower = pow(a,b);
  return superpower;
}

int main (){
 cout<< superpower(3,4) <<endl;

 return 0;

}

Stars!

#include <iostream>
using namespace std;

void stars(int iStars){
  for (int i = 1 ; i <= iStars; i++){
    cout << "*";
  }
  cout << endl;

}

int main(){
  stars(5);
  return 0;
}

]]>
https://creativecommons.org/licenses/by/4.0/
Quiz 04. https://kenscourses.com/tc101fall2015/2015/quiz-04-11/ Tue, 08 Sep 2015 21:44:05 +0000 http://myfreakingcrazythoughts.wordpress.com/?p=55 ]]> For this Quiz we had to verify that our blog was already registered in Ken’s Courses Plataform and in fact it is already registered, as you can see in my screenshot. 😀

quiz4

]]>
https://creativecommons.org/licenses/by/4.0/