1424555337.688Lab3

advertisement
Lab 3 (Course 2)
1. Write a program that declares and initializes 2 integer variables a and b with the values 35 and
14, and displays and calculates their sum, product, quotient and real division result.
The output should exactly look like:
35 + 14 = 49
35 * 14 = 490
35 DIV 14 = 2
35 % 14 =7
35 / 14 = 2.500000
In order to produce this output, use cout to display text combined with values of variables ! (if you
change the initial values of a and b, you should only modify this in a single place in the program !).
solution
#include "stdfax.h"
#include <iostream>
using namespace std;
int main()
{
int a,b,s,p,q,r ;
float rd;
a=35 ;
b=14 ;
s=a+b;
p=a*b;
q=a/b;
r=a%b;
rd=(float)a/b;
cout<<a<<"+"<<b<<"="<<s<<endl;
cout<<a<<"*"<<b<<"="<<p<<endl;
cout<<a<<"DIV"<<b<<"="<<q<<endl;
cout<<a<<"%"<<b<<"="<<r<<endl;
cout<<a<<"/"<<b<<"="<<rd<<endl;
return 0;
}
2. In the previous program, modify the values of a and b to a=123456789 and b=1000.
Run the program and explain the results. Do the modifications needed in order to get the correct
result.
solution
We must changes int by long
1/3
3. Write a program that declares a variable
initializes it with the constant 'A'. Print
and as an integer.

named letter of type
this variable as a
char and
character
Add the following statement: letter=letter+1;
 Print the updated value for variable letter, as a character and as an integer, explain the
result!
solution
#include <iostream>
Using namespace std;
int main()
{
char letter ;
letter=’A’;
cout<<letter<<endl ;
cout<<(int)letter<<endl ;
letter=letter+1;
cout<<letter<<endl ;
cout<<(int)letter<<endl ;
return 0;
}
4. Floating point
following program:
precision:
Run
and
explain
the
output
produced
by
#include <stdio.h>
#include <conio.h>
int main()
{
float a,b;
b = 2.0e20 + 1.0;
a = b - 2.0e20;
printf("%f \n", a);
getch();
return 0;
}
5. Floating point overflow: Run and explain the output produced by the following program.
#include <stdio.h>
#include <conio.h>
int main(void)
{
float number = 3.4E38;
printf("number is %e\n", number);
number=number *100.0f;
printf("number multiplied with 100 is %e\n", number);
getch();
2/3
the
return 0;
}
6. Use sizeof() to determine the storage size in Bytes for the types short int, long int, long long int,
float, double and long double on your current computer system.
Example of using the sizeof operator:
printf("size of float is %i Bytes\n", sizeof(float));
solution
#include <stdio.h>
#include <conio.h>
int main()
{
printf("size
printf("size
printf("size
printf("size
printf("size
printf("size
of
of
of
of
of
of
float
float
float
float
float
float
is
is
is
is
is
is
%d
%d
%d
%d
%d
%d
Bytes\n",
Bytes\n",
Bytes\n",
Bytes\n",
Bytes\n",
Bytes\n",
sizeof(short ));
sizeof(int));
sizeof(long));
sizeof(float));
sizeof(double));
sizeof(long double));
getch();
return 0;
}
7. What output would you expect from the following program?
#include <stdio.h>
#include <conio.h>
int main()
{
int a,b,c;
a=10;
b=++a*2;
a=a++ + b;
c=a++ - ++b;
b=c--+a++ *2;
printf("a= %d \n", a);
printf("b= %d \n", b);
printf("c= %d \n", c);
getch();
return 0;
}
solution
a= 36
b= 81
3/3
c=10
4/3
Download