Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Sunday, January 29, 2012

Adding Timer in C code

Hello everyone! Hope you are passing great time with coding!!
Today I am here with how to add or insert a timer in your C code to show the time how much your code taking time to execute. Sometimes it becomes important to see time your code taking. Okay let’s do it…
We will discuss with a very simple problem. User input two integers and a code to add them !!!
-----------------------------------------------------------------------------------------------------------------
Inserting timer:

#include <stdio.h>
#include <time.h>  //inserting time.h library function.

int main ( )
{
    int a, b, add;  //a & b two integers and add is their addition.
scanf ( “ %d %d ”, &a, &b ); // taking input.

clock_t start=clock(); // clock starting.
add = a + b; //adding a & b.

printf(“\nAddition of %d & %d is =  %d, a, b, add );
                      //printing result.
printf("\nTime = %.2f secondes\n",(double)(clock()-start)/CLOCKS_PER_SEC); // printing time.
    return 0 ;
-----------------------------------------------------------------------------------------------------------------
Inserting current time:
#include <stdio.h>
#include <time.h>

int main(void)
{
    time_t timer = time(NULL);
    printf("current time is %s", ctime(&timer));
    return 0;
}
-----------------------------------------------------------------------------------------------------------------
Reference:  

Tuesday, January 17, 2012

Beginners C Programming : Variables & Data Types


The Journey Begins
I always thought to do something for the beginners, to make those lessons easier which gave me lots of pain. (:P) So, the journey begins from here. Actually, today I attended my first class of C++. The discussions will be on C++. But before that I would like to review some chapters for the beginners so that those terms became easy for them as well as for them also who are practicing C.

Why do we have to learn a programming language??
Simply, we came to learn about the computer because we love computer, that what I realized and also told by my favorite course teacher. And a programming language is a language to communicate with the computer. (hmm… In a simple line.)

What is C or Why C?
The C is called the mother of programming language, the root or base level language, developed by Dennis Ritchie (September 9, 1941 to October 12, 2011). For detail, take a tour to the Wikipedia.
C is one of the most widely used computer language. It is a procedural or step by step procedure language.

Please Note: Don’t panic. I don’t want to make it lengthy. Search the web if you want. I like to make it short as possible but more useful.

Okay, it’s time now! Let’s get into C!!
Before that, we must to be familiar with some definitions which are so much important.

Variable & Data Type:
This is the first thought of a programming language.

Variable:
We have used some variables in Math. We imagined some ‘x’ or ‘y’ to prove a problem. Finally the ‘x’ or ‘y’ had a value. Our variable is also like that. A variable is that, which can store a value. Finish!!!
Tips: Your variable should be any alphabet (uppercase or lowercase), any word (like ‘sum’), and any combination of alphabets (like ‘aa’). Do not use ‘space’ in your variable.

Data Type:
Now read attentively.
How many students are there in your section? Look carefully at them and you will find them their characteristics will not match with each other, even yours. Everyone have different personality. Isn’t it?
If the students are your variables then their personality or characteristics are their Data Type. Clear? I think No. Data Type of a variable is that which represents the type of the variable like ‘x’ is a integer number or real number or a character. This is important that your variable must contain a valid Data Type.

Some of the Data Types used in C are:
int (stands for integer numbers i.e like 3)
float (stands for real numbers i.e like 3.1416)
char (stands for character i.e like ‘a’)


Hope that it was easy for you to understand. You can mail me from my profile that is below Blog Archive.
Enjoy your stay on this blog.
ENJOY SAFE COMPUTING.
Thank you.

Saturday, December 31, 2011

Beginner's Typing Test !


I was supposed to take a typing test of my two little student. I thought how shall I make it !!!
Its not a matter of pen & pencil !! Finally I decide to write my own code. Its finished and
I am so happy now. : )


I am very thankful to Muntasir Muzahid Chowdhury, my senior and FORTH-RIGHT for
helping me and showing me the right path....

Credit: Muntasir Muzahid Chowdhury
          & FORTH-RIGHT 
Project Description:
This test uses "a s d f j k l ;" these eight buttons. You can add as much as you can. It has 5 levels
and each level contains 5 sub-levels. Total score is 5*5=25. A timer is included and all result is
written in a text file named "Result.txt" where the .exe file belongs.
So, here is the code...
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
#include<math.h>
#include <time.h>

int score=0;
int length=3;

int flevel()
{
int sublevel=5,sub_score=0;;
char arr[20]={'R','U','E','I','i','u','r','e','T','A',';','F','J','Y','L','G','H','\0'};
    char tmp[10];
char tmp2[10];clock_t start=clock();
while(sublevel--)
{
int j=0;
for(int i=0;i<length;i++)
{
   tmp[i]=arr[rand()%17];
   tmp[++j]=' ';
}
tmp[j]=NULL;
puts("");
printf("\t%s\n\t",tmp);
scanf("%s",tmp2);
if(strcmp(tmp2,tmp)==0){
sub_score++;score++;}
else
puts("\a\a\tWrong Answer\a\a!");
}
length++;
printf("Score: %d",sub_score);
printf("\nTime = %.2f secondes\n", (double)(clock()-start)/CLOCKS_PER_SEC);
return 0;
}
int writefile(char name[],double &a)
{
FILE *fp;
fp=fopen("Result.txt","a+");
fputs("\n==========================\n",fp);
fputs(" Beginner's Typing Test 4 \n",fp);
fputs("==========================\n",fp);
time_t timer = time(NULL);
fprintf(fp,"TIME: %s\n", ctime(&timer));
fputs("\n============================",fp);fflush(fp);
fprintf(fp,"\nName: %s",name);
fprintf(fp,"\nTotal Score: %d",score);
fprintf(fp,"\nTotal time = %.2f secondes\n", a);
fputs("============================\n",fp);
fclose(fp);
return 0;
}
int main()
{
    puts("==========================");
    puts(" Beginner's Typing Test 4 ");
    puts("==========================");
    puts("LEVELS 5 : SCORE (5*5)=25");
puts("==========================");
    int cases;
    printf("How Many Examinee(s)?: ");
    scanf("%d",&cases);
    while(cases--)
    {
char name[30];score=0;length=3;
puts("");puts("");
puts("===============================");
printf("Enter Your Name: ");
scanf("%s",name);
puts("===============================");
printf("\nGet Ready %s !!!\n",name);
system("pause");
        int level=5,levelcase=1;
clock_t start=clock();
        while(levelcase<=level)
        {
            printf("\n\tLEVEL: %d\n",levelcase++);
puts("============================");
            flevel();
        }
puts("");puts("");
puts("============================");
printf("Total Score: %d\n",score);
double a=(double)(clock()-start)/CLOCKS_PER_SEC;
printf("\nTotal time = %.2f secondes\n", a);
puts("============================\n\n");
        writefile(name,a);system("pause");
    }
system("pause"); return 0;

}
The Code is working fine as a Beginner's TYPING Test !!! Obviously you can change or upgrade this and Please give me the upgraded code...  :D
If you want your own app then send me a mail with your requirements.
This sample can be used as a COMPLEX TYPING TEST.... 

Thank you for reading this post. I am so beginner also and if you have any questions or suggestions please post a comment or mail me.
Enjoy Safe Computing.

Sunday, December 25, 2011

Data Type (View Size)

Credit : FORTH-RIGHT (http://forth-right.blogspot.com)
Using this code you can check your size of data types in your compiler.

#include<stdio.h>
#include<string.h>

int main ()
{
    int A[10];

    A[0]= sizeof(int);
    A[1]= sizeof (short int);
    A[2]= sizeof(long);
    A[3]= sizeof(float);
    A[4]= sizeof(double);
    A[5]= sizeof(long double);
    A[6]= sizeof(char);
    A[7]= sizeof(unsigned long);
    A[8]= sizeof(long long int);

    printf("Default\t\t%d\n",A[0]);
    printf("Short\t\t%d\n",A[1]);
    printf("long\t\t%d\n",A[2]);
    printf("float\t\t%d\n",A[3]);
    printf("double\t\t%d\n",A[4]);
    printf("long double\t%d\n",A[5]);
    printf("char\t\t%d\n",A[6]);
    printf("unsigned long\t%d\n",A[7]);
    printf("long long int\t%d\n",A[8]);

    getchar ();
    return 0;
}

OUTPUT:
Visual Studio 2010


Forth-Right: How to Pass Arguments to Functions Using "Call By Reference"

Click This Button To Read The Main Article:

"When learning C many people face problem with pointers. "Call by Pointers" seem complicated to some people. So for those, who are looking for easier way to get by (though they both seem same to me), here is details of passing arguments using "Call by Reference"......."


Forth-Right.blogspot.com



Saturday, December 17, 2011

C Programming



Finally, I have decided to start again with C programming. I will discuss about things some other day but now I want to share Digit Conversion Program using function & case control structure.


***The continue_exit function does not directly exit.


Digit Conversion: (using Function & Case Control)

Thanks - Jajabor, 2014