Simple C++ Program

User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5221
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

Hello World, here is a simple C++ program, compile and execute it to see what it does!

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int main ()
  5. {
  6. /* cout<< "Hello world"<<endl; // Prints Hello world
  7. cout<< "I am a C++ Program"<<endl;
  8. for (int i = 1; i<= 100; i++) if (i%5==0) cout<<"i = "<<i<<endl;
  9. return 0; //Terminate the program
  10. */
  11. /* cout <<"What the Hell!"<<endl;
  12.  
  13. const int rows = 3;
  14. const int cols = 6;
  15. int numbers[rows][cols];
  16. int Table[rows][cols] = {{1,2,3,4,5,6},{5,4,3,6,7,8}, {9,6,7,5,4,3}};
  17. for (int r = 0; r<rows; ++r)
  18.      for (int c = 0; c<cols; ++c)
  19.            numbers[r][c] = 0;
  20.          
  21.        for(int r = 0; r<rows; ++r) {
  22.                for (int c = 0; c<cols; ++c)
  23.                   cout<<" "<<numbers[r][c]<<" ";
  24.                       cout<<endl;
  25.                   }
  26.                   return 0;
  27.      */
  28.    const int rows = 6;
  29.    const int cols = 6;
  30.    int Total = 0;
  31.    double average = 0.0;
  32.    int grades[rows][cols] = {{89, 98, 78, 79, 100, 96},    //initialization of 6 by 6 array
  33.                           {97, 87, 77, 80,90, 95},
  34.                           {86, 88, 99, 94, 92, 84},
  35.                           {75, 85, 81, 83, 98, 90},
  36.                           {80, 76, 77, 89, 96, 77},
  37.                           {90,80,78,91, 93, 100}};
  38.                          
  39.   for (int r = 0; r < rows; ++r) {cout<<"Students"<<r+1<<"Marks"<<" :";
  40.                                for (int c = 0; c < cols; ++c){
  41.                                cout<<grades[r][c]<<" ";
  42.                                Total + = grades[r][c];
  43.                                }
  44.                                  
  45.                          average = Total/cols;
  46.                          cout<<"Average is"<<average<<endl;
  47.                          Total = 0;
  48.                          average = 0.0;  
  49.                      }  
  50. return 0;
  51.  }

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
mwihechi
Member
Reactions: 1
Posts: 1
Joined: 5 years ago

#2

What is the difference between double and float?
1
1 Image
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5221
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#3

When programming, especially, in C-type programming languages such as C++ we need to store the variables in a computer memory. Before the computer stores any variable, it needs to know the type of data we want to store in it. The reason the computer has to know in advance the type of data before storing it, is that different data types occupy different memory sizes in a computer. It will not take the same amount of memory to store a simple number as to store a single letter or a large number. Furthermore, the variables (say numbers, characters etc.,) are not going to be interpreted the same way by computer in storage. Simply a computer needs to spare different memory partitions appropriate for storing different data types. That is why in order to use a variable in C++, you must first declare it to specify which data type you want it to be.

Computer memory is organized in bytes - the minimum manageable amount of memory in C++. One byte is composed of 4 bits. A bit (in digital storage) is either 0 or 1 and not otherwise.

A byte can store a relatively small unit of data, for example, one single character "char" or a small integer "int", generally an integer between 0 and 255. In addition, the computer can do manipulation to form more complex data types by grouping several bytes. We can therefore think of computer memory as a long tape of bytes.

Float and double variable types differ in the way they store the values. Double and float are both floating point numbers, their main difference is in "precision". Think of precision as "a measure of the detail in which the quantity is expressed".

But, in relation to computer memory, precision refers to as the number of bits used to hold the fractional part of floating-point numbers. The more precision a system uses, the more exactly it can represent fractional quantities.

A "single-precision floating-point format" is a computer number format, usually occupying 4 bytes (32 bits) in computer memory while
"double-precision floating-point format" is a computer number format, usually occupying 8 bytes (64 bits) in computer memory . This means a double has 2 times the precision of float, implying that double-precision floating point number can represent fractional quantities much more accurately than single-precision.

Generally speaking, double has 15 - 16 decimal digits of precision while float has 6 - 7 decimal digits of precision.

However,

float: Floating point number (single-precision) can be stored in a computer memory of size 4 bytes and the value range is \(\pm 3.4e^{\pm 38} \)

double: Double-precision floating point number (double-precision float) can be stored in a computer memory of size 8 bytes and the value range is \(\pm 1.7e^{\pm 308} \)

Computer manages memory by assigning unique address to each byte, where each address is represented by a single integer.

It is important to note that, memory sizes for storing the data types (int, char, float, double etc.,) and their value ranges depend on the system architecture under which the program is compiled, i.e., whether the system is 32-bit or 64-bit. For example, depending on the system environment an integer memory can either be 32 bits or 64 bits.

Memory management is central in C++ programming.
2 Image
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Robot
Super Administrator
Senior Expert Member
Reactions: 24
Posts: 124
Joined: 7 years ago
Has thanked: 7 times
Been thanked: 7 times

#4

Float and double concepts are often confusing to most people, post #3 explains the two concepts with clarity.
0
Post Reply

Return to “C & C++ Programming”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest