Tag Archives: #eeeeee

Mastery 24

Creation and use of arrays in C++

C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Declaring Arrays:

To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows:

This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement:

Initializing Arrays:

You can initialize C++ array elements either one by one or using a single statement as follows:

The number of values between braces { } can not be larger than the number of elements that we declare for the array between square brackets [ ]. Following is an example to assign a single element of the array:

If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write:

You will create exactly the same array as you did in the previous example.

The above statement assigns element number 5th in the array a value of 50.0. Array with 4th index will be 5th, i.e., last element because all arrays have 0 as the index of their first element which is also called base index. Following is the pictorial representaion of the same array we discussed above:

Mastery 24

Credits:

http://www.tutorialspoint.com/cplusplus/cpp_arrays.htm

http://www.cplusplus.com/doc/tutorial/arrays/

 

1017 24

Mastery 20

Use of loops with “for”

for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax:

The syntax of a for loop in C++ is:

Here is the flow of control in a for loop:

  • The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

  • Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.

  • After the body of the for loop executes, the flow of control jumps back up to theincrement statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

  • The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.

Flow Diagram:

Mastery 20

Example:

When the above code is compiled and executed, it produces the following result:

Credits:

http://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm

1017 20

Mastery 19

Use of loops with “while”

while loop statement repeatedly executes a target statement as long as a given condition is true.

Syntax:

The syntax of a while loop in C++ is:

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.

When the condition becomes false, program control passes to the line immediately following the loop.

Flow Diagram:

Mastery 19

Here, key point of the while loop is that the loop might not ever run. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Example:

When the above code is compiled and executed, it produces the following result:

Credits:

http://www.tutorialspoint.com/cplusplus/cpp_while_loop.htm

1017 18 

Mastery 18

Nesting of conditional statements

It is always legal to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).

Syntax:

The syntax for a nested if statement is as follows:

 

You can nest else if…else in the similar way as you have nested if statement.

Example:

When the above code is compiled and executed, it produces the following result:

Credits:

http://www.tutorialspoint.com/cplusplus/cpp_nested_if.htm

1017 18

Use of loops with “while” – Mastery 19

Como usar un loop con “while”

Antes de empezar tenemos que saber que es un loop; es una serie de acciones que se repiten en este caso, con ayuda del while, las instrucciones dentro del loop van a repetirse mientras algo este pasando, como en el siguiente ejemplo:

 
 
   
  using namespace std;
   
  int main()
  {
  int sec, num, cont =0;
  srand(time(NULL));
  sec = rand()%100+1;
   
  while(num != sec){
  cout
  cin >> num;
   
  if (num > sec){
  cout
  }
  if (num
  cout
  }
  cont++;
  }
   
  cout
  cout
   
  return 0;

 

En el ejempo anterior existe un loop con while, este nos indica que mientras la condicional sea false, todo el loop va a repetirse, pero si el usuario ingresa un numero correcto que para nuestro codigo seria el 42, la variable num seria true, por lo que la condicion del while ya no se cumple y el loop se saldria y el programa continuaria corriendo.

1017 19

#Mastery14 #TC1017

Creación y uso de librerias creadas por uno mismo.

Una de las herramientas que te deja usar los programas de programación es la facilidad de poder usar tus propias librerias estas librerias se pueden crear para definir variables con un significado mas especifico.

uno de los casos mas usados en la declaracion de librerias es el  una libreria que te permite utilizar el nombre de una variable y darle usos especificos ya sean de sistema, secuenciales y condicionales.

Un ejemplo que muchos usaron para la realización de su sudoku es la implementación de colores, para esto, muchos definieron cada color, para así, poder ponerlos mas facilmente

 
  GREEN “33[32m”
  PURPLE “33[35m”
  BLUE “33[34m”
  WHITE “33[37m”
  BLU “33[36m”
  YELLOW “33[33m”

When to use what type of repetition in a program

                                                                                                                      @PablO_CVi

The for statement iterates through a collection or iterable object or generator function.

The while statement simply loops until a condition is False.

It isn’t preference. It’s a question of what your data structures are.

Often, we represent the values we want to process as a range (an actual list), or xrange (which generates the values). This gives us a data structure tailor-made for the for statement.

Generally, however, we have a ready-made collection: a set, tuple, list, map or even a string is already an iterable collection, so we simply use a for loop.

In a few cases, we might want some functional-programming processing done for us, in which case we can apply that transformation as part of iteration. The sorted and enumerate functions apply a transformation on an iterable that fits naturally with the for statement.

 

If you don’t have a tidy data structure to iterate through, or you don’t have a generator function that drives your processing, you must use while.

 

 

#TC1017 #QUIZ11

1

Something new that I learned:

ifstream myfile;
  myfile.open (filename);

 

—-this is for open a file 😀

Quizz 1

https://github.com/alejcbgmz/WSQ-s-Ale-Jcb-Gmz/blob/master/banana1.cpp

Quizz 2

https://github.com/alejcbgmz/WSQ-s-Ale-Jcb-Gmz/blob/master/banana2.cpp

P.D when you insert the name of the text file remember write .txt at the end 😀 

I used 

http://c.conclase.net/curso/?cap=039

https://www.youtube.com/watch?v=Iho2EdJgusQ

 

#WSQ14 #TC1014 #LaNieveDeColimaExploto #PenguinLove

Here is my WSQ 14.

I took a little of information from a page that Ken gave us that is this one: http://en.wikipedia.org/wiki/E_%28mathematical_constant%29

GitHub: https://github.com/chocotorroblue/WSQ-S/blob/master/wsq14.py

Image:

Mastery 17

Use of “switch” as a conditional

switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

Syntax:

The syntax for a switch statement in C++ is as follows:

switch(expression){
    case constant-expression  :
       statement(s);
       break; //optional
    case constant-expression  :
       statement(s);
       break; //optional
  
    // you can have any number of case statements.
    default : //Optional
       statement(s);
}

The following rules apply to a switch statement:

  • The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.

  • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

  • The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.

  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

  • Not every case needs to contain a break. If no break appears, the flow of control will fall throughto subsequent cases until a break is reached.

  • switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.