PICTURE OF AUTOR

THIS IS THE #QUIZ6 (WEEK 6) WHOSE OBJECTIVE IS PRACTICE THE CODES THAT ARE MADE IN C++. COVERING #MASTERYTOPIC03

This #QUIZ06 are 5 exercises to help me practice my skills. At present, I’m considering the following as projects: 1,2,3,4,5. These are designed to stretch me gently.

At doing this quiz I learnt that the language C++ keeps changing and runs different on different machines. For the first question I had to use the old command printf instead of cout to format the printing and I had to research the reference in this page: Reference for exercise 1 with also including the old library include <stdio.h>

  1. Exercise 1 Type in the following program and run it:
    #include 
    main()
    { /* PROGRAM TO PRINT OUT SPACE RESERVED FOR VARIABLES */
    	char c;  
    	short s;  
    	int i;  
    	unsigned int ui;  
    	unsigned long int ul; 
    	float f;
    	double d;  
    	long double ld;  
    	cout << endl 
      	     << "The storage space for each variable type is:"
    	     << endl;
    	cout << endl << "char: \t\t\t%d bits",sizeof(c)*8;  //  \t means tab 
    	cout << endl << "short: \t\t\t%d bits",sizeof(s)*8;
    	cout << endl << "int: \t\t\t%d bits",sizeof(i)*8;
    	cout << endl << "unsigned int: \t\t%d bits",sizeof(ui)*8;
    	cout << endl << "unsigned long int: \t%d bits",sizeof(ul)*8;
    	cout << endl << "float: \t\t\t%d bits",sizeof(f)*8;
    	cout << endl << "double: \t\t%d bits",sizeof(d)*8;
    	cout << endl << "long double: \t\t%d bits",sizeof(ld)*8;
    }

Pictures of the quiz6 exercise 1.:

primera

LINK IN GITHUB:Exercise 1 in Github.cpp

Also is in here:

#include <iostream>
#include <cstdlib>
#include <stdio.h>
using namespace std;


main()
{ /* PROGRAM TO PRINT OUT SPACE RESERVED FOR VARIABLES */
char c;
short s;
int i;
unsigned int ui;
unsigned long int ul;
float f;
double d;
long double ld;
cout << endl
<< "The storage space for each variable type is:"
<< endl;
printf ("char: \t\t\t %d bits \n", sizeof(c)*8); // \t means tab
printf ("short: \t\t\t %d bits \n", sizeof(s)*8);
printf ("int: \t\t\t %d bits \n", sizeof(i)*8);
printf ("unsigned int: \t\t%d bits \n", sizeof(ui)*8);
printf ("unsigned long int: \t%d bits \n", sizeof(ul)*8);
printf ("float: \t\t\t %d bits \n ", sizeof(f)*8);
printf ("double: \t\t%d bits \n",sizeof(d)*8);
printf ("long double: \t\t%d bits",sizeof(ld)*8);
cout<<endl;
}

2. Exercise 2 Write a program to read in a character, an integer, and a float, and print out the values, then cast to other types; so that the interaction with the user is something like the following:

	Input a single character, followed by : h
	Input an integer, followed by :  4872
	Input a float, followed by :  182.937
	The character h when cast to an int gives value ?????
	The character h when cast to a float gives value ?????
	The integer 4872 when cast to a char gives value ?
	The integer 4872 when cast to a float gives value ?????
	The float 182.937 when cast to a char gives value ?
	The float 182.937 when cast to an int gives value ?????

With the acknowledge of my teacher, I could use the operation of casting with this command a=(int)h; where h is character and a is int.

Pictures of the quiz6 exercise 2:

segunda

tercera

cuarta

LINK IN GITHUB: Exercise 2 quiz6v2.cpp

Also is in here:


#include <iostream>
#include <cstdlib>
#include <stdio.h>
using namespace std;


int main ()

{

char h,j;
int a,k;
float b,w;

cout<<"Input a single character, followed by :";
cin>>h;

cout<<"Input an integer, followed by :";
cin>>a;
k=a;

cout<<"Input a float, followed by :";
cin>>b;
w=b;


a=(int)h;
cout<<"The character "<< h <<" when cast to an int gives value="<<a<<endl;

b=(float)h;
cout<<"The character "<< h <<" when cast to a float gives value="<<b<<endl;

h=(char)a;

cout<<"The integer "<< k <<" when cast to a char gives value ="<< h <<endl;
b=(float)a;

cout<<"The integer "<< k <<" when cast to a float gives value="<< b <<endl;
h=(char)b;

cout<<"The float "<< w <<" when cast to a char gives value="<< h << endl;

a=(int)b;

cout<<"The float "<< w <<" when cast to an int gives value="<< a <<endl;






}

3. Exercise 3

Write a program to print the following pattern:

                        C
                      i   I
                    s       s
                  b           b
                e               e
              s                   s
            t s e b s i C i s b e s t

In this exercise you just need to know that the triangle is just text.

Pictures of the quiz6 exercise 3:

exercise3

LINK IN GITHUB: Exercise 3 quizv3.cpp

Also is in here:

#include <iostream>
#include <cstdlib>
#include <stdio.h>
using namespace std;


int main ()

{

cout<<" C \n";
cout<< " i I \n";
cout<< " s s \n";
cout<< " b b \n";
cout<< " e e \n";
cout<< " s s\n ";
cout<<" t s e b s i C i s b e s t\n ";




}


4. Exercise 4 Write a program to read three ints and to print them in ascending order.

In this exercise you need the condition of the greater or lower number entered by the user and if the number is in between those numbers, that variable will be in the position in the middle of the counting

Pictures of the quiz6 exercise 4:

octava

quiz6v4

quiz6v4444

LINK IN GITHUB: Exercise 4 quiz6.cpp

Also is in here:


#include <iostream>
#include <cstdlib>
#include <stdio.h>
using namespace std;


int main ()

{

int a,b,c,greater,greater2,greater3;

cout<<"Give me a first integer number:";
cin>>a;

cout<<"Give me a second integer number:";
cin>>b;

cout<<"Give me a third integer number:";
cin>>c;

if(a>=b && a>=c)
{
greater=a;


}

if(b>=a && b>=c)
{

greater=b;
}

if(c>=a && c>=b)
{

greater=c;


}

cout<<endl;
cout<<greater<<endl;

if(a<=b && a<=c)
{
greater2=a;


}

if(b<=a && b<=c)
{
greater2=b;


}

if(c<=a && c<=b)
{
greater2=c;


}


if(a==greater && b==greater2)
{

greater3=c;

}


if(b==greater && c==greater2)
{
greater3=a;

}

if(c==greater && a==greater2)
{

greater3=b;

}
if(c==greater && b==greater2)
{

greater3=a;
}





cout<<greater3<<endl;

cout<<greater2<<endl;







}

5. Exercise 5 Given the following rules, write a program to read a year (4 digit integer) and tell whether the given year is/was a leap year.

  1. There were no leap years before 1752.
  2. If the year divides by 400 then it is a leap year.
  3. All other years that divide by 100 are not leap years.
  4. All other years that divide by four are leap years.

For example, 1800,1900 were not leap years but 2000 will be; 1904, 1908,…,1996 were/will be leap years.

In this exercise you need to learn all of these conditions and with the help of the logic operators of NOT, AND AND OR you can make these conditions true.

Pictures of the quiz6 exercise 5:

decima

decima2

quiz6v5

LINK IN GITHUB: Exercise 5 quiz6v5.cpp

Also is in here:


#include <iostream>
#include <cstdlib>
#include <stdio.h>
using namespace std;


int main ()
{

int year;
cout<<"Give me a year with 4 integer digits:";
cin>>year;
if (year<1752)
{cout<<"There were no leap years at this time"<<endl;}


if(year>=1752 && (year%4)==0 && (year%400)==0 && (year%100)!=0)
{
cout<<"The year is a leap year"<<endl;

}

if(year>=1752 && (year%100)==0 || (year%4)!=0)
{
cout<<"The year is not a leap year"<<endl;

}

if((year%4)==0 && (year%100)!=0)
{

cout<<"The year is a leap year"<<endl;


}





}

IN CONCLUSION, FIRST TO DO IN THIS #QUIZ06 IS DOING THE EXERCISE WITH LANGUAGE C IN ORDER TO SHOW THAT IN C++ keeps changing and runs different on different machines. Therefore, in exercise 2 you need to know the command for casting one type of variable into other type of variable with for example the command a=(int)b; where b is a float variable and a is an int variable.

Therefore, in exercise 3 you need to know that is just arranging text in order to have that triangle.

Futhermore, in exercise 4 you need the condition of the greater or lower number entered by the user and if the number is in between those numbers, that variable will be in the position in the middle of the counting.

Last but not least, in exercise 5 you need to use the conditions and with the help of the logic operators of NOT, AND AND OR you can make these conditions true.

 

 

Un comentario en “#Quiz06

Deja un comentario