Skip to main content

Some interesting observations in C++

//I used Dev C++ Ver 5.11 (compiler) ... i told since some of this might run differently on other IDEs

Some interesting observations in C++
----------------------------------------
1. About pow() function
*pow() cant find 'fractional' powers, but can find decimal powers
for eg. pow(4,1/2) gives 1;
but, pow(4,0.5) gives 2;
     pow(4,0.55) gives 2.14;

*roots of negative numbers:
pow(-4,0.5) gives "nan"

2. Time Battle between cout and for loop (UnExpEcTEd)
In this program, see the time taken by them (individually) "on my computer" (on faster computers there may be more/less gap)

#include<iostream>
using namespace std;
int main()
{

//for(int i=1; i<100000;i++);  //0.215 sec
cout<<"hello";  //0.303 sec
}

3. Dont ask user to enter 0, if you are going to use an IF statement, because 0 is treated as false; instead ask for -1 instead of 0 (*you may even as for any other number)
For eg. cout<<"Enter 0 if you want to exit : "; cin>>choice;
if(choice==0) exit;
else goto proceed;

//in this piece of code, even if the user enters 0, the if will become false, and hence will not exit
//ALTERNATIVE (Not recommended) - you may go like this : "if(choice!=0) exit;" but if the user is asked to enter number choice from a menu, for eg. then the program will exit instead of proceeding :p

4. About the size of array...
   When you declare an array globally, you will have to give its size also
   When you declare an array in a function, you will have to give its size also
   When you declare an array in a CLASS or STRUCT, you are NOT REQUIRED TO GIVE ITS SIZE
   When you declare an array in a FUNCTION'S PROTOTYPE, you are NOT REQUIRED TO GIVE ITS SIZE

5. C++ is CASE SENSITIVE

6. C++ printed a 100MB file in 6s (with random characters from the heap storage) on a 7 year old laptop, with an intel core i5, and 4GB DDR3 RAM

7. More to be added...

Comments

Popular posts from this blog

Using 'ls' command as a replacement of 'find'

Advantage over find - Actually not probably in terms of speed, but... only point of this is that it's an observation, but... you can do like 'search only in' */*/*Programs* , /*/*Programs*/* , this may compensate the extra time took by using two commands 1. To search in current folder only (1 level... only this folder, no subfolder)     Type... ls | grep Search_Term 2. To search in current folder only (till level 2... this folder + subfolder)     Type... ls * | grep Search_Term #ls and ls * are NOT same! 3. To search in current folder only (till level 3... this folder + subfolder + subfolder)     Type... ls */* | grep Search_Term 4. To search in current folder only (till level 4... this folder + subfolder + subfolder + subfolder)     Type... ls */*/* | grep Search_Term ... and so on... FORMULA - To search till 'n' level (considering current folder as 1)... go till (n-1) stars If wanting to search whole disk... better type stars, like this... ls * ** *** **** *****

AP, GP Generator (using Linked List)

//Future advancements-> add save as file, in text or in a database option (for the sequences) //                      add more series to this //Programmer - Aditya Gupta #include<iostream> #include<stdlib.h> #include<math.h> //#include<fstream> //reqd when save support is added to this! using namespace std; struct Node{ int data; Node* next; }; void inline title(void){ system("cls"); cout<<"\tNAMASTE!...\n\nProgram-AP,GP Generator\nPrograammer - MR. ADITYA GUPTA (TEChY 3)\n\n"; } class List{ //NOTE: I HAVE REMOVED ALL COMMENTS, REFER TO THE LINKED LIST TEMPLATE FOR DETAILED COMMENTS             //here only generator specific comments are there, and that too which are not in template Node *top,*rear; public: void Push(int dat); int isempty(); int search(int dat); int noofelements(); void Pop(); //CAUTION- will decrease number of terms by 1 each time it runs, and pushing the last element again will be same

More_Inferences_in_C++(1Aug'19-16thOct'19)

Programmer/Tester- Aditya Gupta (aka Techy3.5 :-) I used g++ to compile these on linux 1. Type casting of floating point numeral to integer (either automatic or by type casting)- Conversion of floating point to integer is by taking the GIF (Greatest Integer Funtion) and NOT ROUNDING OFF Greatest Integer Function(Maths,12th) it gives the integer part when the original number is written as integer+fractional part (where fractional part is less than 1 but positive) 2. NEsted MultiLine Comment [actually wont work as expected?!] if you do this in C++.... /*  blah /*blah blah blah*/ blah*/ the last blah will be visible to the compiler!!! (Inference- Multiline-Comment ends whenever */ is encountered, no matter where!!)NEsted MultiLine Comment 3. Size of various types (of C++) on 'Linux' Mint - (and same as in later versions of windows, and most PCs nowadays) char - 1 int - 4  (and: short - 2, unsigned long - 8) long long - 8 float - 4 double - 8 (and: lo