Using c++, visual studios First, study char_ranges.cpp and understand what it do
ID: 3757080 • Letter: U
Question
Using c++, visual studios
First, study char_ranges.cpp and understand what it does. Then compile it and run it, and see if it gives you the output you expected. (If it tells you you have 16-bit chars, then use char16_ranges.cpp, which steps by 64, instead of stepping by 1's.)
Note that the example program demonstrates all 3 types of loops – a do_while, a while, and a for loop.
Then, write a program which demonstrates the range of values for signed and unsigned shorts and ints. You don't want to watch thousands or billions of numbers go by, so you can have your program start outputting values 100 below the (expected) max value, and increment it 200 times, to see what happens. If it doesn't give you the expected results, make sure you know what the max value for your data types is.
This looping is a good place to use for(...) loops – generally used for counting loops – because you know how many times you want to go through the loop. However, use each type of loop in your program, so you are confident you know how each type works.
If your numbers don't wrap (back to 0 or back to a large negative number), that means you didn't get the right answer for the range in part 1.
/*
* char_ranges.cpp
*
* Created on: Sep 12, 2014
*
*
* Sample program to demonstrate the range of values that chars can contain.
*
* Also shows how to use a do-while and a while loop and a for loop.
*
*
*/
#include
#include
using namespace std;
int main(void)
{
// integer types:
bool b;
unsigned char uc;
char c;
if (sizeof(c) > 1)
{
cout << "Oops. your computer has 16-bit chars."< cout << "This would output 65000+ numbers. Please use 'char16_ranges.cpp instead."< return 1;
}
// show all the values for bool:
cout << endl << " bool Values: "< b = false;
cout << "b = false: "< b = true;
cout << "b = true: "<
// note: chars are 'intended' for character data, but are
// really integer data types. If I output a char, c++ treats it as
// a character. But I can cast it as an integer, and it will output
// its numerical value. (You don't need to static_cast the other
// integer types to get them to output as numeric values).
// Here, I output uc first as a char, and then as an integer
// (I cast it as an int to output it's integer value).
// Notice how the same bit pattern can represent either text or numbers.
cout << "Output the ASCII codes for upper and lower case A-Z:"< uc = 'A'; // We'll output A-Z and their respective ASCII codes
c = 'a';
cout << "Chars and their values (aka the ASCII code)."< do // here I'll use a do-while loop.
{
cout << uc << setw(3)<< static_cast(uc) << " " << c << setw(4) << static_cast(c) << endl;
uc += 1;
c += 1;
} while (uc <= 'Z');
// OK. Now let's output all possible values of an unsigned char.
// We'll output a little more than 256, so we can see what happens when
// the value overflows. I use a while loop here just to make sure you have
// an example of a while loop, as well as a do-while loop.
// the setw(n) function , made available by #include , sets the field width
// of the output, so you can make nice fixed-width columns.
// show all the values for chars:
cout << endl << " char Values : this will increment c & uc 300 times, starting at 0: "<
uc = 0;
int n=1; // my counter variable
cout << endl << "unsigned char: all possible values" << endl;
while (n <= 300) // loop using a while loop
{
cout << setw(4) << static_cast(uc) << " ";
if (n % 16 == 0) // output a newline every 16
cout << endl;
uc = uc+1; // 2 different ways to increment.
n++;
}
// And now do the same for signed char. I'll use a for loop here.
c = 0;
cout << endl << "signed char: " << endl;
for (int m=1; m<=300; m++) // now, loop with a for loop.
{
cout << setw(4) << static_cast(c) << " ";
if (m % 16 == 0) // output a newline every 16
cout << endl;
++c; // does it matter if I use ++c or c++ here? why not?
}
cout << " done!" << endl;
return 0;
}
/*
* char16_ranges.cpp
*
* Created on: Sep 12, 2014
*
* Program to show the range of values for a char on a computer
*
* Also shows how to use a do-while and a while loop and a for loop.
*
* This version is for environments with 16-bit chars.
*
*/
#include
#include
using namespace std;
int main(void)
{
// integer types:
bool b;
unsigned char uc;
char c;
if (sizeof(c) == 1)
{
cout << "Oops. your computer has 8-bit chars."< cout << "Please use 'char_ranges.cpp' instead."< return 1;
}
// show all the values for bool:
cout << endl << " bool Values: "< b = false;
cout << "b = false: "< b = true;
cout << "b = true: "<
// show all the values for chars:
cout << endl << " char Values (will increment c & uc 300 times: "<
// note: chars are 'intended' for character data, but are
// really integer data types. If I output a char, c++ treats it as
// a character. But I can cast it as an integer, and it will output
// its numerical value. (You don't need to stat_cast the other
// integer types to get them to output as values).
// output uc as character data and as integer data
// Here, I output uc first as a char, and then as an integer
// Notice how the same bit pattern can represent either text or numbers.
uc = 'A';
c = 'a';
cout << "Chars and their values (aka the ASCII code)."< do // here I'll use a do-while loop.
{
cout << uc << setw(3)<< static_cast(uc) << " " << c << setw(3) << static_cast(c) << endl;
uc += 1;
c += 1;
} while (uc <= 'Z');
// OK. Now let's output all possible values of an unsigned char.
// We'll output a little more than 256, so we can see what happens when
// the value overflows. I use a while loop here just to make sure you have
// an example of a while loop, as well as a do-while loop.
// the setw(n) function , made available by #include , sets the field width
// of the output, so you can make nice fixed-width columns.
uc = 0;
int n=1; // my counter variable
cout << endl << "unsigned char: all possible values" << endl;
while (n <= 256*300) // loop using a while loop
{
cout << setw(6) << static_cast(uc) << " ";
if (n % 16 == 0) // output a newline every 16
cout << endl;
uc = uc+64; // 2 different ways to add 64.
n += 64; // I add 64, so you don't have to watch 76800 numbers go by...
}
// And now do the same for signed char.
c = 0;
cout << endl << "signed char: " << endl;
for (int m=1; m<=256*300; m+=64) // now, loop with a for loop.
{
cout << setw(6) << static_cast(c) << " ";
if (m % 16 == 0) // output a newline every 16
cout << endl;
c += 64;; // does it matter if I use ++c or c++ here? why not?
}
cout << " done!" << endl;
return 0;
}
Explanation / Answer
#include<iostream>
#include<limits.h> // for int,char macros
#include<float.h> // for float,double macros
using namespace std;
int main()
{
// Displaying ranges with the help of macros
cout << "char ranges from : " << CHAR_MIN << " to " << CHAR_MAX;
cout << " short char ranges from : " << SCHAR_MIN << " to " << SCHAR_MAX;
cout << " unsigned char ranges from : " << 0 << " to " << UCHAR_MAX;
cout << " short int ranges from : " << SHRT_MIN << " to " << SHRT_MAX;
cout << " unsigned short int ranges from : " << 0 << " to " << USHRT_MAX;
cout << " int ranges from : " << INT_MIN << " to " << INT_MAX;
cout << " unsigned int ranges from : " << 0 << " to " << UINT_MAX;
cout << " long int ranges from : " << LONG_MIN << " to " << LONG_MAX;
cout << " unsigned long int ranges from : " << 0 << " to " << ULONG_MAX;
cout << " long long int ranges from : " << LLONG_MIN << " to " << LLONG_MAX;
cout << " unsigned long long int ranges from : " << 0 << " to " << ULLONG_MAX;
cout << " float ranges from : " << FLT_MIN << " to " << FLT_MAX;
cout << " negative float ranges from : " << -FLT_MIN << " to " << -FLT_MAX;
cout << " double ranges from : " << DBL_MIN << " to " << DBL_MAX;
cout << " negative double ranges from : " << -DBL_MIN << " to " << +DBL_MAX;
return 0;
}
Output:
char ranges from : -128 to 127
short char ranges from : -128 to 127
unsigned char ranges from : 0 to 255
short int ranges from : -32768 to 32767
unsigned short int ranges from : 0 to 65535
int ranges from : -2147483648 to 2147483647
unsigned int ranges from : 0 to 4294967295
long int ranges from : -9223372036854775808 to 9223372036854775807
unsigned long int ranges from : 0 to 18446744073709551615
long long int ranges from : -9223372036854775808 to 9223372036854775807
unsigned long long int ranges from : 0 to 18446744073709551615
float ranges from : 1.17549e-38 to 3.40282e+38
negative float ranges from : -1.17549e-38 to -3.40282e+38
double ranges from : 2.22507e-308 to 1.79769e+308
negative double ranges from : -2.22507e-308 to 1.79769e+308
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.