--Originally published at The Clueless Programmer
So for the quiz on this week we had to do a bunch of programs, so expect this blog post so be filled with codes, don´t be scared though, they´re not so hard. The excercises we had to do came out of this web page: http://www.utdallas.edu/~ivor/cs1315/clabs96.html
Excercise 1: They gave us a program and we had to make it run. We added some stuff at the beginning and then we had to add a library and change the couts to printfs.
#include <iostream>
#include <stdio.h>
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”,sizeof(c)*8); // \t means tab
printf(“short: \t\t\t%d bits”,sizeof(s)*8);
printf(“int: \t\t\t%d bits”,sizeof(i)*8);
printf(“unsigned int: \t\t%d bits”,sizeof(ui)*8);
printf(“unsigned long int: \t%d bits”,sizeof(ul)*8);
printf(“float: \t\t\t%d bits”,sizeof(f)*8);
printf(“double: \t\t%d bits”,sizeof(d)*8);
printf(“long double: \t\t%d bits”,sizeof(ld)*8);
}
Excercise 2: We had to ask the user to input a char, an int and a float value and then we had to print each value as the other two. Like give the char and the float value of an int.
#include <iostream>
using namespace std;
int main()
{
char c1;
int n1;
float f1;
cout<<“Write a single character: “;
cin>>c1;
cout<<“Write an integer: “;
cin>>n1;
cout<<“Write a float: “;
cin>>f1;
int n2=int(c1);
float f2=float(c1);
cout<<“The character “<<c1<<” when cast to an int gives value: “<<n2<<endl;
cout<<“The character “<<c1<<” when cast to a float gives value: “<<f2<<endl;
char c2=char(n1);
float f3=float(n1);
cout<<“The integer “<<n1<<” when cast to a char gives value: “<<c2<<endl;
cout<<“The integer “<<n1<<” when cast to a float gives value: “<<f3<<endl;
int n3=int(f1);
char c3=char(f1);
Continue reading "Quiz week 6" →