this Mastery might sound too complicated but in fact it is too easy to regard this mastery is known in Spanish as conditional nested , and no forces have to be an if as it is not the only type of conditional exists, nested conditional may be of different types such as:
if / else
for
while
switch
do
which can be nested ( put inside other ) as often as desired. for instance:
using namespace std;
int main(){
int uno;
cout
cin>>uno;
if (uno!=0){
if(uno
cout
}
else
{
cout
}
}
else
{
cout
}
return 0;
}
]]>// writing on a text file
//Reference: http://www.cplusplus.com/doc/tutorial/files/
using
namespace
std;
int
main () {
ofstream myfile (
"Write.txt"
);
if
(myfile.is_open()) {
myfile "This is a line.\n"
;
myfile "Hello world.\n"
;
myfile.close();
}
else
{
cout "Unable to open file"
;
}
return
0;
}
// reading a text file
//Reference: http://www.cplusplus.com/doc/tutorial/files/
using
namespace
std;
int
main () {
string line;
ifstream myfile (
"CNC.txt"
);
if
(myfile.is_open()){
while
( getline (myfile,line) ){
cout '\n'
;
}
myfile.close();
}
else
{
cout "Unable to open file"
;
}
return
0;
}
en este, tenemos que abre el archivo y este es leido linea por linea y convertida en strings, para esto t
// writing on a text file
//Reference: http:/ / www.cplusplus.com/ doc/ tutorial/ files/
using
namespace
std;
int
main () {
ofstream myfile (
"Write.txt"
);
if
(myfile.is_open()) {
myfile "This is a line.\n"
;
myfile "Hello world.\n"
;
myfile.close();
}
else
{
cout "Unable to open file"
;
}
return
0;
}
// reading a text file
//Reference: http:/ / www.cplusplus.com/ doc/ tutorial/ files/
using
namespace
std;
int
main () {
string line;
ifstream myfile (
"CNC.txt"
);
if
(myfile.is_open()){
while
( getline (myfile,line) ){
cout '\n'
;
}
myfile.close();
}
else
{
cout "Unable to open file"
;
}
return
0;
}
en este, tenemos que abre el archivo y este es leido linea por linea y convertida en strings, para esto t
[]
. El elemento 5° (quinto) de un arreglo, es representado por el índice [4]
, ya que los índices comienzan en 0
. Esto significa que un arreglo de 10 elementos tendría los índices del 0 al 9: [0...9].
[]
. El elemento 5° (quinto) de un arreglo, es representado por el índice [4]
, ya que los índices comienzan en 0
. Esto significa que un arreglo de 10 elementos tendría los índices del 0 al 9: [0...9].
int
a[5];
// Vector de 5 enteros
float
b[5];
// vector de 5 flotantes
Producto product[5];
// vector de 5 objetos de tipo Producto
1 2 3 | int a[] = {5, 15, 20, 25, 30}; float b[] = {10.5, 20.5, 30.5, 12.5, 50.5} Producto product[] = {celular, calculadora, camara, ipod, usb} |
int
a[4] = 30;
// le asigno el valor 30 a la posición 4 del vector.
product[2].setPrecio(300)
// le asigno un precio de 300 al producto en la posición 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using namespace std; int main() { int dim; cout cin >> dim; // Supongamos que ingrese 10 int vector[dim]; // mi vector es de tamanyo 10 for ( int i = 0; i vector[i] = i * 10; cout } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | using namespace std; void sumar( int a[], int b[], int c[], int dim) { for ( int i = 0; i c[i] = a[i] + b[i]; } } void imprimir( int v[], int dim) { for ( int i = 0; i cout } cout } int main() { int dim; cout cin >> dim; int a[dim]; int b[dim]; int c[dim]; for ( int i = 0; i a[i] = i * 10; b[i] = i * 5; } cout imprimir(a, dim); cout imprimir(b, dim); sumar(a, b, c, dim); cout imprimir(c, dim); return 0; } |
int
a[5];
// Vector de 5 enteros
float
b[5];
// vector de 5 flotantes
Producto product[5];
// vector de 5 objetos de tipo Producto
1
2
3
|
int a[] = {5, 15, 20, 25, 30}; float b[] = {10.5, 20.5, 30.5, 12.5, 50.5} Producto product[] = {celular, calculadora, camara, ipod, usb} |
int
a[4] = 30;
// le asigno el valor 30 a la posición 4 del vector.
product[2].setPrecio(300)
// le asigno un precio de 300 al producto en la posición 2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
using namespace std; int main() { int dim; cout cin >> dim; // Supongamos que ingrese 10 int vector[dim]; // mi vector es de tamanyo 10 for ( int i = 0; i vector[i] = i * 10; cout } return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
using namespace std; void sumar( int a[], int b[], int c[], int dim) { for ( int i = 0; i c[i] = a[i] + b[i]; } } void imprimir( int v[], int dim) { for ( int i = 0; i cout } cout } int main() { int dim; cout cin >> dim; int a[dim]; int b[dim]; int c[dim]; for ( int i = 0; i a[i] = i * 10; b[i] = i * 5; } cout imprimir(a, dim); cout imprimir(b, dim); sumar(a, b, c, dim); cout imprimir(c, dim); return 0; } |
Here´s my quiz 11, it was a little more complicated than I thought, but in the end I receive some help from my friends Marco Patiño and Esaú Preciado, and well they teach me how to do the programms.
so here are my programms
q1.
#include <iostream>
#include <iomanip>
using namespace std;
void newline(){
cout<<endl;
}
double factor(int a){
double output= 1.0;
for (int i = 1; i <= a; i++){
if (a == 0)
return output;
else
output = output * i;
}
return output;
}
double define_e(int exact){
double output1 = 0.0, output2 = 1.0;
for(int i= 1; i<1000 ; i++){
output1 = output2;
output2 = output1 + (1/factor(i));
}
cout << fixed << setprecision(exact) << output2 << endl;
return output1;
}
int x = 0;
int main(){
float e;
string z;
cout<<"Hi, I´m a calculator of the e constant "<<endl;
newline();
cout<<"Please insert how precisely you want this mathematical constant "<<endl;
newline();
cin>>e;
newline();
cout<<"The result equals to "<<define_e(e)<<endl;
newline();
}
return 0;
}
q2.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
string Read;
string Banana = "banana";
char archive[50];
int x = 0;
int y = 0;
int counter = 0;
cout << "Write the name of your file: ";
cin >> archive;
ifstream read_file (archive);
if (read_file.is_open()){
while (getline(read_file , Read)){
x = 0;
while (x < Read.length()) {
char character = Read[x];
if ( character == 'B' || character== 'b'){
y = x + 1;
char character = Read[y];
if (character == 'A' || character== 'a'){
y++;
char character = Read[y];
if (character == 'N' || character== 'n'){
y++;
char character = Read[y];
if (character == 'A' || character== 'a'){
y++;
char character = Read[y];
if (character == 'N' || character== 'n'){
y++;
char character = Read[y];
if (character == 'A' || character== 'a') {
counter++;
}
}
}
}
}
}
x = x + 1;
}
}read_file.close();
}else{
cout << "Error 404 not found" << endl;
}
cout << "I found " << counter << " bananas....So eat them all!!!...or not..." << endl;
return 0;
}
Also on github.
Program1.
https://github.com/everibarra/TC101-C-/blob/master/quiz11q1.cpp
Program2
https://github.com/everibarra/TC101-C-/blob/master/quiz11q2.cpp
]]>Here´s my quiz 11, it was a little more complicated than I thought, but in the end I receive some help from my friends Marco Patiño and Esaú Preciado, and well they teach me how to do the programms.
so here are my programms
q1.
#include <iostream>
#include <iomanip>
using namespace std;
void newline(){
cout<<endl;
}
double factor(int a){
double output= 1.0;
for (int i = 1; i <= a; i++){
if (a == 0)
return output;
else
output = output * i;
}
return output;
}
double define_e(int exact){
double output1 = 0.0, output2 = 1.0;
for(int i= 1; i<1000 ; i++){
output1 = output2;
output2 = output1 + (1/factor(i));
}
cout << fixed << setprecision(exact) << output2 << endl;
return output1;
}
int x = 0;
int main(){
float e;
string z;
cout<<"Hi, I´m a calculator of the e constant "<<endl;
newline();
cout<<"Please insert how precisely you want this mathematical constant "<<endl;
newline();
cin>>e;
newline();
cout<<"The result equals to "<<define_e(e)<<endl;
newline();
}
return 0;
}
q2.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
string Read;
string Banana = "banana";
char archive[50];
int x = 0;
int y = 0;
int counter = 0;
cout << "Write the name of your file: ";
cin >> archive;
ifstream read_file (archive);
if (read_file.is_open()){
while (getline(read_file , Read)){
x = 0;
while (x < Read.length()) {
char character = Read[x];
if ( character == 'B' || character== 'b'){
y = x + 1;
char character = Read[y];
if (character == 'A' || character== 'a'){
y++;
char character = Read[y];
if (character == 'N' || character== 'n'){
y++;
char character = Read[y];
if (character == 'A' || character== 'a'){
y++;
char character = Read[y];
if (character == 'N' || character== 'n'){
y++;
char character = Read[y];
if (character == 'A' || character== 'a') {
counter++;
}
}
}
}
}
}
x = x + 1;
}
}read_file.close();
}else{
cout << "Error 404 not found" << endl;
}
cout << "I found " << counter << " bananas....So eat them all!!!...or not..." << endl;
return 0;
}
Also on github.
Program1.
https:/
Program2
https:/
We finally did the project, we just need to adjust some functions, because we were using functions from theimagemagick library, tomorrow we´re going to change the programm and see if it runs with arrays.
here´s the code:
#include <Magick++.h>
#include <iostream>
using namespace std;
using namespace Magick;
void newline(){
cout<<endl;
}
int main(int argc,char **argv)
{
InitializeMagick(*argv);
try{
int x;
cout<<"Hi, Im a manipulator of images "<<endl;
newline();
cout<<"What do you want to do? (press the number in your keyboard) "<<endl;
newline();
cout<<"1. Rezise the image to one half of its original size"<<endl;
cout<<"2. Switch to black and white"<<endl;
newline();
cin>>x;
if(x==1){
newline();
cout<<"Alright, now introduce the name of the file with its extension; example: new.jpg"<<endl;
newline();
Image image;
string img;
cin>>img;
image.read(img);
image.minify();
image.write("resized.jpg");
}
if(x==2){
newline();
cout<<"Alright, now introduce the name of the file with its extension; example: new.jpg"<<endl;
newline();
Image image2;
string img2;
cin>>img2;
image2.read(img2);
image2.quantizeColorSpace( GRAYColorspace );
image2.quantizeColors( 256 );
image2.quantize( );
image2.write("blackandwhite.jpg");
}
}
catch( Exception &error_ )
{
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
We finally did the project, we just need to adjust some functions, because we were using functions from theimagemagick library, tomorrow we´re going to change the programm and see if it runs with arrays.
here´s the code:
#include <Magick++.h>
#include <iostream>
using namespace std;
using namespace Magick;
void newline(){
cout<<endl;
}
int main(int argc,char **argv)
{
InitializeMagick(*argv);
try{
int x;
cout<<"Hi, Im a manipulator of images "<<endl;
newline();
cout<<"What do you want to do? (press the number in your keyboard) "<<endl;
newline();
cout<<"1. Rezise the image to one half of its original size"<<endl;
cout<<"2. Switch to black and white"<<endl;
newline();
cin>>x;
if(x==1){
newline();
cout<<"Alright, now introduce the name of the file with its extension; example: new.jpg"<<endl;
newline();
Image image;
string img;
cin>>img;
image.read(img);
image.minify();
image.write("resized.jpg");
}
if(x==2){
newline();
cout<<"Alright, now introduce the name of the file with its extension; example: new.jpg"<<endl;
newline();
Image image2;
string img2;
cin>>img2;
image2.read(img2);
image2.quantizeColorSpace( GRAYColorspace );
image2.quantizeColors( 256 );
image2.quantize( );
image2.write("blackandwhite.jpg");
}
}
catch( Exception &error_ )
{
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
#include string>
#include iostream>
using namespace std;
main() {
string mensaje;
mensaje = "Hola";
cout mensaje; }
La forma más común y fácil de acceder a un caracter en una determinada posición que queramos es mediante el uso de [] (corchetes) cómo se hace con los arrays, de esta forma si tenemos un tipo string de nombre str al cual le asignamos la palabra “programa” y queremos que se nos retorne la letra que está en la posición 3 (contando desde cero, como siempre) entonces el código sería el siguiente.
#include <iostream>
#include <string>
using namespace std;
int main(){
//definimos la cadena str
string str;
//le asignamos el contenido & quot programa& quot;
str = “programa”;
//mostramos en consola cual es el caracter
//en la posición 3 (contando desde 0)
cout<<str[3]<<endl<<endl;
return 0;
}
#include string>
#include iostream>
using namespace std;
main() {
string mensaje;
mensaje = "Hola";
cout mensaje; }
¡¡¡¡Hi!!!! this is the advance of the project, but we dont know how to change this to pixels or vectors,
Link to code:
https://github.com/kenwbauer/TC101F15_Team01/commit/62ff22fecfd823e3d4496eea9a7cdad78116c46d
#include <Magick++.h>#include <iostream>
using namespace std;
using namespace Magick;
void newline(){
cout<<endl;
}
int main(int argc,char **argv)
{
InitializeMagick(*argv);
try{
int x;
cout<<"Hi, Im a manipulator of images "<<endl;
newline();
cout<<"What do you want to do? (press the number in your keyboard) "<<endl;
newline();
cout<<"1. Rezise the image to one half of its original size"<<endl;
cout<<"2. Switch to black and white"<<endl;
newline();
cin>>x;
if(x==1){
newline(); cout<<"Alright, now introduce the name of the file with its extension; example: new.jpg"<<endl;
newline();
Image image;
string img;
cin>>img;
image.read(img);
image.minify();
image.write("resized.jpg");
}
if(x==2){
newline();
cout<<"Alright, now introduce the name of the file with its extension; example: new.jpg"<<endl;
newline();
Image image2;
string img2;
cin>>img2;
image2.read(img2);
image2.quantizeColorSpace( GRAYColorspace );
image2.quantizeColors( 256 );
image2.quantize( );
image2.write("blackandwhite.jpg");
}
}
catch( Exception &error_ )
{ cout << "Caught exception: " << error_.what() << endl; return 1;
}return 0;
}
]]>
¡¡¡¡Hi!!!! this is the advance of the project, but we dont know how to change this to pixels or vectors,
Link to code:
https:/
#include <Magick++.h>#include <iostream>
using namespace std;
using namespace Magick;
void newline(){
cout<<endl;
}
int main(int argc,char **argv)
{
InitializeMagick(*argv);
try{
int x;
cout<<"Hi, Im a manipulator of images "<<endl;
newline();
cout<<"What do you want to do? (press the number in your keyboard) "<<endl;
newline();
cout<<"1. Rezise the image to one half of its original size"<<endl;
cout<<"2. Switch to black and white"<<endl;
newline();
cin>>x;
if(x==1){
newline(); cout<<"Alright, now introduce the name of the file with its extension; example: new.jpg"<<endl;
newline();
Image image;
string img;
cin>>img;
image.read(img);
image.minify();
image.write("resized.jpg");
}
if(x==2){
newline();
cout<<"Alright, now introduce the name of the file with its extension; example: new.jpg"<<endl;
newline();
Image image2;
string img2;
cin>>img2;
image2.read(img2);
image2.quantizeColorSpace( GRAYColorspace );
image2.quantizeColors( 256 );
image2.quantize( );
image2.write("blackandwhite.jpg");
}
}
catch( Exception &error_ )
{ cout << "Caught exception: " << error_.what() << endl; return 1;
}return 0;
}
]]>
Hi everyone this is the "report" of the project.
This time we are using Imagemagick, Magick++; we can half size the image yeah, but we can't change the color, ooooooooo u.u, so with this problem we are in a hurry, but we can do it and we feel positive with it because we are in the half of the way to complete this task :)
I feel like I´m not doing my best with this project, but here I am trying.
Some links that we found, (more Ever) and this can help:
http://www.imagemagick.org/Magick++/Color.html#ColorGray
http://www.imagemagick.org/script/perl-magick.php
The code:
#include
#include
using namespace std;
using namespace Magick;
void newline(){
coutendl;
}
int main(int argc,char **argv)
{
InitializeMagick(*argv);
try{
int x;
cout"Hi, Im a manipulator of images "endl;
newline();
cout"What do you want to do? (press the number in your keyboard) "endl;
newline();
cout"1. Rezise the image to one half of its original size"endl;
cout"2. Switch to black and white"endl;
newline();
cin>>x;
if(x==1){
newline();
cout"Alright, now introduce the name of the file with its extension; example: new.jpg"endl;
newline();
Image image;
string img;
cin>>img;
image.read(img);
image.minify();
image.write("resized.jpg");
}
if(x==2){
newline();
cout"Alright, now introduce the name of the file with its extension; example: new.jpg"endl;
newline();
Image image;
string img2;
cin>>img2;
image.read(img2);
image.quantize(colorspace.gray);
image.write("blackandwhite.jpg");
}
}
catch( Exception &error_ )
{
cout "Caught exception: " error_.what() endl;
return 1;
}
return 0;
}
]]>
Hi everyone this is the “report” of the project.
This time we are using Imagemagick, Magick++; we can half size the image yeah, but we can’t change the color, ooooooooo u.u, so with this problem we are in a hurry, but we can do it and we feel positive with it because we are in the half of the way to complete this task
I feel like I´m not doing my best with this project, but here I am trying.
Some links that we found, (more Ever) and this can help:
http:/
http:/
The code:
#include
#include
using namespace std;
using namespace Magick;
void newline(){
coutendl;
}
int main(int argc,char **argv)
{
InitializeMagick(*argv);
try{
int x;
cout"Hi, Im a manipulator of images "endl;
newline();
cout"What do you want to do? (press the number in your keyboard) "endl;
newline();
cout"1. Rezise the image to one half of its original size"endl;
cout"2. Switch to black and white"endl;
newline();
cin>>x;
if(x==1){
newline();
cout"Alright, now introduce the name of the file with its extension; example: new.jpg"endl;
newline();
Image image;
string img;
cin>>img;
image.read(img);
image.minify();
image.write("resized.jpg");
}
if(x==2){
newline();
cout"Alright, now introduce the name of the file with its extension; example: new.jpg"endl;
newline();
Image image;
string img2;
cin>>img2;
image.read(img2);
image.quantize(colorspace.gray);
image.write("blackandwhite.jpg");
}
}
catch( Exception &error_ )
{
cout "Caught exception: " error_.what() endl;
return 1;
}
return 0;
}
]]>
So, right now I technically push even forward my programming skills, I mean...using imagemagick is a little bit hard, but today I compile some code and it runs!!!
Marco and I manage to resize the image to one half of its original size, but now we´re struggling with the grayscale functions and stuff, at least we have something to keep working on and we feel positive about the results in our final project.
We find some useful functions in the following pages:
http://www.imagemagick.org/script/perl-magick.php
http://www.imagemagick.org/Magick++/Color.html#ColorGray
#include <Magick++.h>
#include <iostream>
using namespace std;
using namespace Magick;
void newline(){
cout<<endl;
}
int main(int argc,char **argv)
{
InitializeMagick(*argv);
try{
int x;
cout<<"Hi, Im a manipulator of images "<<endl;
newline();
cout<<"What do you want to do? (press the number in your keyboard) "<<endl;
newline();
cout<<"1. Rezise the image to one half of its original size"<<endl;
cout<<"2. Switch to black and white"<<endl;
newline();
cin>>x;
if(x==1){
newline();
cout<<"Alright, now introduce the name of the file with its extension; example: new.jpg"<<endl;
newline();
Image image;
string img;
cin>>img;
image.read(img);
image.minify();
image.write("resized.jpg");
}
if(x==2){
newline();
cout<<"Alright, now introduce the name of the file with its extension; example: new.jpg"<<endl;
newline();
Image image;
string img2;
cin>>img2;
image.read(img2);
image.quantize(colorspace.gray);
image.write("blackandwhite.jpg");
}
}
catch( Exception &error_ )
{
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
The first option is the only one that works, we are going to keep working with the colors.
See you later!
]]>So, right now I technically push even forward my programming skills, I mean…using imagemagick is a little bit hard, but today I compile some code and it runs!!!
Marco and I manage to resize the image to one half of its original size, but now we´re struggling with the grayscale functions and stuff, at least we have something to keep working on and we feel positive about the results in our final project.
We find some useful functions in the following pages:
http:/
http:/
#include <Magick++.h>
#include <iostream>
using namespace std;
using namespace Magick;
void newline(){
cout<<endl;
}
int main(int argc,char **argv)
{
InitializeMagick(*argv);
try{
int x;
cout<<"Hi, Im a manipulator of images "<<endl;
newline();
cout<<"What do you want to do? (press the number in your keyboard) "<<endl;
newline();
cout<<"1. Rezise the image to one half of its original size"<<endl;
cout<<"2. Switch to black and white"<<endl;
newline();
cin>>x;
if(x==1){
newline();
cout<<"Alright, now introduce the name of the file with its extension; example: new.jpg"<<endl;
newline();
Image image;
string img;
cin>>img;
image.read(img);
image.minify();
image.write("resized.jpg");
}
if(x==2){
newline();
cout<<"Alright, now introduce the name of the file with its extension; example: new.jpg"<<endl;
newline();
Image image;
string img2;
cin>>img2;
image.read(img2);
image.quantize(colorspace.gray);
image.write("blackandwhite.jpg");
}
}
catch( Exception &error_ )
{
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
The first option is the only one that works, we are going to keep working with the colors.
See you later!
]]>