The quiz for today consited in five exercises that we had to do during the class, which came from a page called C/C++ PROGRAMMING EXERCISES (they were the first five ones).
In the first problem, we had to modify a determinated code to make it work. The original code was:
#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;
I opted for changing cout for printf, adding the stdio.h and iostrem libraries, as well as the line using namespace std; and declaring main as int. I consulted the page that our professor recommended to learn more about printf. It should be mentioned that what this program does is showing the number of bits that a type of value can store. My final code is the following one:
#include <stdio.h> #include <iostream> using namespace std; int 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",int (sizeof(c)*8)); printf("short: \t\t\t%d bits\n",int (sizeof(s)*8)); printf("int: \t\t\t%d bits\n",int (sizeof(i)*8)); printf("unsigned int: \t\t%d bits\n",int (sizeof(ui)*8)); printf("unsigned long int: \t%d bits\n",int (sizeof(ul)*8)); printf("float: \t\t\t%d bits\n",int (sizeof(f)*8)); printf("double: \t\t%d bits\n",int (sizeof(d)*8)); printf("double: \t\t%d bits\n",int (sizeof(d)*8)); printf("long double: \t\t%d bits\n",int (sizeof(ld)*8)); }
The second exercise was about casting types of values to other one, and return taken values. I have to recognize that I didn’t know what to do at the first, but after meditating on it a little, I realized that it wasn’t difficult at all. I just had to use printf again and specify that I wanted to convert a variable to another type different to the one that it was declared at the first. The utilized types were char, int and float. The next code is the one that I made for this problem:
#include <iostream> #include <stdio.h> using namespace std; int main(){ char C; int I; float F; cout << "Input a single character, followed by : "; cin.get(C); cout << "Input an integer, followed by : "; cin >> I; cout << "Input a float, followed by : "; cin >> F; printf("The character %c when cast to an int gives value %d\n",C,int(C)); printf("The character %c when cast to a float gives value %f\n",C,float(C)); printf("The integer %d when cast to a char gives value %c\n",I,char(I)); printf("The integer %d when cast to a float gives value %f\n",I,float(I)); printf("The float %f when cast to a char gives value %c\n",F,int(F)); printf("The float %f when cast to an int gives value %d\n",F,int(F)); return 0; }
The third exercise only required to print a given pattern. This was my code (you will notice that it is very short and simple):
#include <iostream> using namespace std; int main(){ cout << " C" << endl; cout << " i I" << endl; cout << " s s" << endl; cout << " b b" << endl; cout << " e e" << endl; cout << " s s" << endl; cout << "t s e b s i C i s b e s t" << endl; return 0; }
The next exercise is a little more complicated, because it’s about ordering three numbers in ascending order so I made three different functions (first, second and last term) and each one contained conditionals, so my program resulted a little long (relatively).
#include <iostream> using namespace std; int minor(int x, int y, int z) { int m; if (x<=y && x<=z) m=x; else if (y<=x && y<=z) m=y; else if (z<=x && z<=y) m=z; return m; } int medium(int x, int y, int z) { int m; if ((x>=y && x<=z) || (x>=z && x<=y)) m=x; else if ((y>=x && y<=z) || (y>=z && y<=x)) m=y; else if ((z>=x && z<=y) || (z>=y && z<=x)) m=z; return m; } int maximum(int x, int y, int z) { int m; if (x>=y && x>=z) m=x; else if (y>=x && y>=z) m=y; else if (z>=x && z>=y) m=z; return m; } int main() { int a, b, c, P, S, T; cout << "Number 1: "; cin >> a; cout << "Number 2: "; cin >> b; cout << "Number 3: "; cin >> c; P=minor(a,b,c); S=medium(a,b,c); T=maximum(a,b,c); cout << "\n\nThose numbers, in ascending order, are: \n" << P << endl << S << endl << T << endl; return 0; }
Finally, the last problem I had to solve was this one:
“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.
- There were no leap years before 1752.
- If the year divides by 400 then it is a leap year.
- All other years that divide by 100 are not leap years.
- 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.”
I had some problemas with my logic because I wanted to integrate all the “rules” at once, but I couldn’t do it since the program didn’t return the right values. Therefore I added some if’s, else if’s and nested conditionals to make it work. This was the result:
#include <iostream> #include <string> using namespace std; string leap(int Y) { string R; if (Y<1752) R="That year was/will be NOT a leap year\n"; else if ((Y%100)==0) { if ((Y%400)==0) R="That year was/will be a leap year\n"; else R="That year was/will be NOT a leap year\n"; } else if ((Y%4)==0) R="That year was/will be a leap year\n"; else R="That year was/will be a NOT leap year\n"; return R; } int main() { int Y; cout << "Year: "; cin >> Y; cout << leap (Y); return 0; }
Well, this is all we do during the large quiz of the week six…
See you soon…