Showing posts with label c language. Show all posts
Showing posts with label c language. Show all posts

Sunday, 9 December 2018

How to insert images in C/ C++ using graphics(100% working)

Hello friends today we will learn how to insert images in  C++ language very easily using graphics. The things that are required for this work are listed here .....

1.Code blocks compiler installed on your system:

Code::Blocks is a free, open-source cross-platform IDE that supports multiple compilers including GCC, Clang and Visual C++. It is developed in C++ using wxWidgets as the GUI toolkit. Using a plugin architecture, its capabilities and features are defined by the provided plugins. Currently, Code::Blocks is oriented towards C, C++, and Fortran. It has a custom build system and optional Make support.(by wikipedia).

2.Graphics libraries should set up with code blocks:

When we install code blocks then there is no library and header file of graphics are given by the developers with it, so we have to configure these things manually.I come across a lots of videos on youtube to configure graphics in codeblocks and i found all the videos are just waste of time except one video and i learnt  from that video .It was a bit difficult for me to configure graphics in codeblocks.
This is why because i was getting the libraries and header file with bug.
So i thought to create a video on this topic on my own  :How to use graphics in codeblocks video






  If you did these two things then you can use this program or concept to insert images in C++ language using graphics in Codeblocks.
Here we go:
 #include<iostream>  
 #include<graphics.h>  
 int main()  
 {  
   initwindow(600,400,"Images");  
   readimagefile("image_name.jpg",0,0,600,400);  
   getch();  
   readimagefile("image_name.jpg",0,0,600,400);  
   getch();  
   return 0;  
 }  



Description of the program:


If you are using code blocks to make programs in graphics mode then we can make the programs in only C++ because in the graphics.h header file there is reference to iostream and if we work in C language then it will not link and then we get error in header file graphics.h. Download File



here we have used the two important predefinded functions of graphics they are....

  • initwindow():this function is used to initialise a new window or you can say a  new window
  • readimagefile():this is the function that is used to set images in our program.
readimagefile() takes five parameters the first parameter is the name of the image with extension of the image and the rest are the co-ordinates of the screen where to set.


Thanks for visiting ...we will come with new typical programs so stay connected.







How to insert image in c language in turboc IDE

Its been very interesting as well as difficult to work with graphics in c or c++ languages.
But it will be more interesting to work with image insertion in C or C++ language.

I have already created the post to insert image in c/c++ in codeblocks IDE but now its time to achieve this using Turbo C++ IDE.

 Befor writing code we have to take care of  these points.
  • Create a  bmp(16 color Bitmap) image file using paint or any other image editor. 
  • paste that file into bin folder of turbo c where this program will be saved.
  • After pasting we will continue to write the below code.




 
 #include <alloc.h>  
   #include <conio.h>  
   #include <graphics.h>  
   #include <stdio.h>  
   #include <stdlib.h>  
   struct BMP  
   {  
    char Type[2];      //File type. Set to "BM".  
    unsigned long Size;   //Size in BYTES of the file.  
    unsigned long Reserved;   //Reserved. Set to zero.  
    unsigned long OffSet;  //Offset to the data.  
    unsigned long headsize; //Size of rest of header. Set to 40.  
    unsigned long Width;   //Width of bitmap in pixels.  
    unsigned long Height;   // Height of bitmap in pixels.  
    unsigned int Planes;  //Number of Planes. Set to 1.  
    unsigned int BitsPerPixel;    //Number of Bits per pixels.  
    unsigned long Compression;  //Compression. Usually set to 0.  
    unsigned long SizeImage; //Size in bytes of the bitmap.  
    unsigned long XPixelsPreMeter;   //Horizontal pixels per meter.  
    unsigned long YPixelsPreMeter;   //Vertical pixels per meter.  
    unsigned long ColorsUsed;  //Number of colors used.  
    unsigned long ColorsImportant; //Number of "important" colors.  
   };  
   int ShowBMP(int x, int y, char* FileName)  
   {  
     int b,a;  
     struct BMP Obj;  
     unsigned char* Datas;  
     int in=0;  
     unsigned char c=0;  
     FILE * fp;  
     fp = fopen(image.bmp,"rb"); //give filename of image with extension.  
     if(!fp){  
     printf("Error : Unable to open file ..");  
     exit(0);  
     }  
     fread(&Obj, sizeof(Obj), 1, fp);  
     if(Obj.BitsPerPixel!=4) // This isn't a 16 color bmp we can read;  
     {  
      fclose(fp);  
      printf("Error : File format not supported ..");  
      exit(0);  
     };  
     fseek(fp,Obj.OffSet,SEEK_SET);  
     Datas=(unsigned char*) calloc(Obj.Width/2+1, sizeof(unsigned char));  
     for(b=Obj.Height;b>=0;b--)  
     {  
      fread(Datas, sizeof(unsigned char), Obj.Width/2, fp);  
      c=0;  
      in=0;  
      for(a=0;a<=Obj.Width;a+=2)  
      {  
        c = (Datas[in] | 0x00) >>4;  
        putpixel(a+x,b+y,c);  
        c = (Datas[in] | 0xF0) & 0x0F;  
         putpixel(a+1+x,b+y,c);  
        in++;  
      }  
     }  
     free (Datas);  
     fclose(fp);  
     return 1;  
   }  
   void main()  
   {  
   int color;  
   int gd , gm ;  
   gd = VGA ; gm = VGAHI;  
   initgraph(&gd,&gm,"");  
   ShowBMP(0,0,"j.bmp"); /* Enter File Name Here */  
   getch();  
   closegraph();  
   }  

Now Run the code and you will get your image as output.
 
 

Tuesday, 26 September 2017

how to generate colorful output in c language

To generate colorful output on console in c language we are going to use a predefined function of c language that is textcolor().

Now ,in this post we will learn how to use this textcolor funtion to generate colorful output on console.

declaration of textcolor -

void textcolor(int color);

here we have to send the integral value of the corresponding  color that we want to use .

Color Numerical Value
BLACK 0
BLUE 1
GREEN 2
CYAN 3
RED 4
MAGENTA 5
BROWN 6
LIGHTGRAY 7
DARKGRAY 8
LIGHTBLUE 9
LIGHTGREEN 10
LIGHTCYAN 11
LIGHTRED 12
LIGHTMAGENTA 13
YELLOW 14
WHITE 15

here are more colors than this. The colors available depend on the installed graphics drivers and current mode. Colors must be written in all Upper Case
because these colors are predefined macros.



                        note :we must use cpreintf function instead printf.

 #include<conio.h>  
 #include<stdio.h>  
 int main()  
 {  
         textcolor(RED);// the color is set to red  
         cprintf("I am Red");///the output will be in red color  
         textcolor(GREEN);// the color is set to green  
         cprintf("I am green");///the output will be in green color  
         textcolor(BLUE);// the color is set to blue  
         cprintf("I am blue");///the output will be in blue color  
         textcolor(CYAN);// the color is set to cyan  
         cprintf("I am Cyan");///the output will be in Cyan color  
         textcolor(YELLOW);// the color is set to yellow  
         cprintf("I am Yellow");///the output will be in yellow color  
        return 0;  
 }  

Friday, 1 September 2017

CONSTANT EXPRESSION REQUIRED-ERROR IN C PROGRAMMING

CONSTANT EXPRESSION REQUIRED-ERROR IN C PROGRAMMING 




The above program has error 'constant expression required' that means in between the [ ] constant yielding expression is needed or constant of type integer is needed.
While declaring an array the compiler must know how much memory will be reserved for how many elements of the array. So in C language at the time of declaration of an array we must provide a meaningful size of the array and the number of elements of any array can not be non- integral.
The size of the array must be integer because the number of elements can not be in fraction they can only be in integral numbers and cant be float or double.
               On the other context ,if we want to give the size of the array at the time of execution then we have to deal with a concept known as dynamic memory allocation and we need to use some predefined functions as calloc() ,malloc() and realloc().

Thursday, 31 August 2017

TOO MANY TYPES IN DECLARATION - ERRORS IN C PROGRAMMING

TOO MANY TYPES IN DECLARATION - ERROR IN C PROGRAMMING 

Compile Time Error

Too many types in declaration is a common error that occurs when we try to declare a function or variable of multiple types that is not allowed in C programming .As for example
int char a;
then this statement results in this type of error and exactly this situation is happening with the program in the above image. Here compiler gets confused that what is the exact data type of a;

In the above program in image, structure is defined that is a user defined data type thats why the compiler is condensed about the return type of main() function.Compiler is considering the structure as a data type so there are two data types there the first one is int and the second one is structure.And this ambiguity results in this error. And the second error is also explaining the same thing that incompatible types. So,when we put a semicolon before int and after the structure definition the error will be gone.
I hope you enjoyed this post...stay connected I will be posting  such interesting posts.

Other posts:
 how to insert image in C/C++ language
Introduction to Errors in C programming
C program for Bisection method

Wednesday, 30 August 2017

INTRODUCTION TO ERRORS AND THEIR TYPES IN BRIEF

INTRODUCTION TO ERRORS AND THEIR TYPES IN BRIEF

While writing programs we have to take care of many things and whenever we miss any thing required this creates a situation that prevent the program execution.
In simple words any problem generated while developing a program is known as an Error.Technically errors are known as bugs in computer science.
Basically there are three types of errors in c programming:
Runtime Errors
Compile Errors
Logical Errors

C Runtime Errors

C runtime errors are those errors that occur during the execution of a c program and generally occur due to some illegal operation performed in the program. Examples of some illegal operations that may produce runtime errors are:

Dividing a number by zero
Trying to open a file which is not created
Lack of free memory space

It should be noted that occurrence of these errors may stop program execution, thus to encounter this, a program should be written such that it is able to handle such unexpected errors and rather than terminating unexpectedly, it should be able to continue operating. This ability of the program is known as robustness and the code used to make a program robust is known as guard code as it guards program from terminating abruptly due to occurrence of execution errors.

Compile Errors


Compile errors are those errors that occur at the time of compilation of the program. C compile errors may be further classified as:

Syntax Errors

When the rules of the c programming language are not followed, the compiler will show syntax errors. For example, consider the statement,
int a,b:
The above statement will produce syntax error as the statement is terminated with : rather than ;

Semantic Errors

Semantic errors are reported by the compiler when the statements written in the c program are not meaningful to the compiler. For example, consider the statement,
b+c=a;
In the above statement we are trying to assign value of a in the value obtained by summation of b and c which has no meaning in c. The correct statement will be
a=b+c;
Logical Errors

Logical errors are the errors in the output of the program. The presence of logical errors leads to undesired or incorrect output and are caused due to error in the logic applied in the program to produce the desired output. Also, logical errors could not be detected by the compiler, and thus, programmers has to check the entire coding of a C program line by line.

Tuesday, 29 August 2017

C program for Bisection method

C program to implement bisection method in easy way..

bisection method theory

Here we have used a term tolerance value .Tolerance value is the difference between two consecutive

approximate roots of the equation that can be neglected.
As for example 1.00008 and 1.00004 are two approximate consecutive roots .

.One of them can be answer if there will be tolerance value of 0.00004 or greater otherwise not.
Here we have used a function fabs that returns the absolute double value and also takes double value 
as an argument.

 #include<conio.h>  
 #include<stdio.h>  
 #include<math.h>  
 float F(float x)  
 {  
   return x*x*x-x-1;  
 }  
 /*  
 f(0)=-ve  
 f(1)=-ve  here a=1 f(a)  
 f(2)=+ve  and b=2  
 tolerance value:0.0005  
 */  
 int main()  
 {  
   float a,b,c,d;  
   int i=0;  
   printf("Enter the interval(a,b) in which the roots of the equation lies::");  
   scanf("%f %f",&a,&b);  
   printf("\nEnter the tolerance value::");  
   scanf("%f",&d);  
   do{  
       c=(a+b)/2;  
       ++i;  
       if(F(a)*F(c)<0)//f(a)<0  
       {  b=c;  
         printf("\nValue of a and b in %d iteration is %f and %f",i,a,b);  
       }  
       else  
       {  
         a=c;  
         printf("\nValue of a and b in %d iteration is %f and %f",i,a,b);  
       }  
   }while(fabs(a-b)>d||F(c)==0);  
   printf("\nRoot is %f",c);  
   printf("\nPress any key to exit");  
   getche();  
   return 0;  
 }  
                                               


Friday, 26 May 2017

C program to create music like sa re ga ma pa

C program to create music like sa re ga ma pa 

Hello friends its very interesting to produce sound in a programming language like c language .
While playing with C language programs and some function and also frequencies i found some  unique things .I learnt how to produce sounds of some known frequencies and then I succeeded in producing the music sa re ga ma pa .To produce this music it requires a little bit knowledge of some functions for example delay()  and beep() .
First of all we learn about these two functions ..
1.Delay -This function is used to stop execution of the program for some time .

Declaration- void delay(unsigned miliseconds);

we provide the argument as integer in the term of milisecond to just stop the execution of the program for the time.

We will use this function and another two sound () and nosound() while working with Turbo C++ IDE .

2.Beep() - This function is provided by the headerfile windows.h ,so this is frequently used by the latest working IDEs with mingw compilers.

It takes two arguments the first one is frequency and the second one is duration to produce the sound.

Now we are good go to the source code to create a music like saa re ga ma pa

 #include<stdio.h>
#include<windows.h>
int main()
{
    Beep(314,1500);
    Beep(350,1500);
    Beep(390,1500);
    Beep(467,1500);
    Beep(526,1500);
    Beep(624,1500);
    Beep(314,2000);

     Beep(314,500);
    Beep(350,500);
    Beep(390,500);
    Beep(467,500);
    Beep(526,500);
    Beep(624,500);
    Beep(314,500);

    return 0;

}

These are the frequencies that produce sa re ga ma pa music ...
So, I hope you enjoyed this post .
Feel free to comment  ,share and and like...

Saturday, 20 May 2017

How to configure graphics.h header file in codeblocks IDE within few steps

How to configure graphics.h header file in codeblocks IDE within few steps

I have searched a lot on internet and gone through a lots of tutorials and posts but neither of them was working correctly  even if on youtube one can file a lots of videos about how to configure and add graphics.h header file in codeblocks but they are not working in most of the cases.The reason was buggy graphics.h header file.So searching on the internet I succeeded to find out the buggy graphics.h header file and got success in configuring the graphics.h header file successfully with codeblocks.
So its good to go with the steps.

Step-1.Download the file from here and extract it.
                                                           Download now

Step-2.copy graphics.h and winbgim.h and paste it in the include folder  where your codeblocks is installed.
for example: (C:\Program Files (x86)\CodeBlocks\MinGW\include).

Step-3.Now copy the libbgi.a file to the lib folder near to the include folder
for example: (C:\Program Files (x86)\CodeBlocks\MinGW\lib).

step-4:The second last step is to go to Setting->compiler then linker setting.
You can find two boxes named Link Libraries and Other Linker Options.
Then you can find and add button in Link Libraries Just click on add and brows and select the file libbgi.a that we have pasted in lib folder in step 3.

step-5 :The last and important step is the linker command that we have to write in the linker box which is in the right side of Linker Libraries .
Paste these commands in that box as it is.

-lbgi

-lgdi32

-lcomdlg32

-luuid

-loleaut32

-lole32

 Thats it we have successfully configured the codeblock with graphics.h.
Now enjoy coding..........

Insert image in c using codeblocks

How to implement Facebook audience network banner ad in android app using Android Studio

Its very simple to implement Facebook audience network banner ad in android app using Android Studio. We will complete it in 4 Steps. 1. ...