User Tools

Site Tools


user:nbrimme1:portfolio:dataproject1

Data Project 1: Exploring Data Types:

A project for Data Structures by Nicholas Brimmer during the Spring 2013 semester.

This project was began on Monday, September 2 and is anticipated to take a few days of time. The program took a weekend to write but could have been written in a few hours if I had the time.

Objectives

The purpose of this project is to learn about the different kinds of data types and there sizes (in bytes). Accomplishing this project should reinforce knowledge of C/C++ data types and there ranges on different types of systems. Depending on the system being used; these values do change. This knowledge should help prevent “Overflows/Underflows” and “Off-by-one/Fence post” errors.

Note: There is no project page for this project.

Prerequisites

In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • Logging into Lab46.
  • Create and edit text files.
  • Write and compile C source code.
  • Execute the compiled source code.
  • Extensive knowledge of Google is always recommended.

Background

The purpose of this project is to represent and display the sizes (in bytes) of the different data types of C/C++. Matt provided some example code:

/*
Nick Brimmer
Data Structures
Mathew Haas
Spring 2013
Explore C Data types, there sizes in bytes, and ranges.
*/
 
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
  // Program Variables.
  /* Quantity variable: Stores the maximum unique values the data type can
  hold. The maximum number of unique values is 1 less than 0; 0 - 1 causes
  quantity to "roll-over" to its maximum value. */
  unsigned long long int quantity = 0;
  unsigned char uc = 0;
  signed char sc = 0;
 
  // Aesthetics, heading for the output table with a divider.
  printf("\nData Type Sizes (in bytes) and Ranges:\n");
  printf("----------------------------------------\n");
 
  // unsigned char:
  printf("An unsigned character is: %d bytes.\n", sizeof(uc));
  quantity = (unsigned char) (uc - 1);
  printf("The range of an unsigned character is %hhu to %hhu.\n", uc, (uc - 1));
  printf("An unsigned character can store %llu unique values.\n\n", quantity);
 
  // Signed char:
  printf("A signed character is: %d bytes.\n", sizeof(sc));
  quantity = (unsigned long long int)pow(2, (sizeof(sc) * 8));
  printf("The range of a signed character is %hhd to %hhd.\n", (sc - (quantity / 2)), (sc + (quantity / 2) - 1));
  printf("A signed character can store %llu unique values.\n\n", quantity);
  printf("\n");
 
  return(0);
}

Upon approval, you'll want to fill this section out with more detailed background information. DO NOT JUST PROVIDE A LINK.

Providing any links to original source material, such as from a project page, is a good idea.

You'll want to give a general overview of what is going to be accomplished (for example, if your project is about installing a web server, do a little write-up on web servers. What is it, why do we need one, how does it work, etc.)

Scope

This project will display all C/C++ data types as well as there ranges. This can be useful in preventing some errors in future C/C++ programs. The program will display the size (in bytes) and the lower and upper bounds of each data type. The data types that will be displayed include:

  • signed char
  • unsigned char
  • signed short int
  • unsigned short int
  • signed int
  • unsigned int
  • signed long int
  • unsigned long int
  • signed long long int
  • unsigned long long int
  • floating point *Not required.

The C functions sizeof() and printf() will be used, as well as arithmetic operators. A potential improvement would be to insert “,”'s into the data type sizes to better represent the numbers.

Attributes

State and justify the attributes you'd like to receive upon successful approval and completion of this project.

  • attribute1: why you feel your pursuit of this project will gain you this attribute
  • attribute2: why you feel your pursuit of this project will gain you this attribute
  • etc…

Procedure

The actual steps taken to accomplish the project. Include images, code snippets, command-line excerpts; whatever is useful for intuitively communicating important information for accomplishing the project.

The program was pretty easy to implement using Matt's code as a reference. Looking at Matt's example code; to determine the size of a data type the function “sizeof()” can be used. This function also supports arithmetic operators.

Code

// C Code.

Execution

lab46:~/$

Reflection

Since some of the numbers were so large, I had them “converted” to their equivalent number name:

  • The value 216 - 1 is 65,535; not too big and is of course Sixty Five Thousand, Five Hundred Thirty Five unique values.
  • The value 232 - 1 is 4,294,967,295 or Four Billion, Two Hundred Ninety Four Million, Nine Hundred Sixty Seven Thousand, Two Hundred Ninety Five unique values.
  • The value 264 - 1 is 18,446,744,073,709,551,615 or Eighteen Quintillion, Four Hundred Forty Six Quadrillion, Seven Hundred Forty Four Trillion, Seventy Three Billion, Seven Hundred and Nine Million, Five Hundred Fifty One Thousand, Six Hundred Fifteen unique values.

The first thing I learned was about signed and unsigned numbers. The max value for an unsigned number is 0 - 1. This causes the lowest value (0) to “overflow” like an odometer to its maximum value. This is called “Integer overflow”. If it was a signed number, the number would go from positive to a negative number. Another important thing I learned was the size of the data types is dependent on the system that the code is being ran on. This can cause the data type ranges to be different from system to system. For example; I found that on lab46, the “long int” and “long long int” data types have the same size (8 bytes). This could be a system property of lab46. I also found that you cannot use the “signed” or “unsigned” modifiers on the “float” or “double” data types.

The first problem I encountered was a variable we declared in class. I had declared the “unsigned long long int ulli = 0;” variable incorrectly and mistyped my quantity variable. The “long float” variable is also broken. Another problem was my use of parenthesis and getting the order of operations correctly implemented.

One Possible improvement is to enter “,”'s into the output to make the numbers look all pretty:

  1. Convert the number to a string.
  2. Iterate to the end of the string to count the number of characters. Assign to *chars.
  3. Decrement 3 chars; iterator is already at the end of the string.
  4. Insert a comma into the string.
  5. Repeat steps 3 and 4 until strlen() = 0 or *chars < 3. *chars is the number of characters in the string.

References

In performing this project, the following resources were referenced:

  • Google (If ya' don't know, now ya' know; and knowing is half the battle): Of course Google was used, it knows everything. No particular page from Google was used, it was mainly used for information about the project in general (Interger types, sizes, etc). Also helped in checking if my math and values were correct.
  • Wikipedia: 2 different pages were actually used:
    • This page has some information about the different kinds of data types: Data Types.
    • This page has everything needed to check the values in one pretty little table: Integers

Back to my Portfolio
Back to my Opus

user/nbrimme1/portfolio/dataproject1.txt · Last modified: 2013/11/04 23:52 by nbrimme1