Monday, April 26, 2021

, , , , ,

strncpy() Function in c programming with example

 strncpy() Function in c programming is used to copies the specified number of characters from source string (src) to destination string and this is an only difference between strcpy() function and strncpy() Function.

 Syntax for strncpy() in c

char *strncpy(char *str1, const char *str2, size_t n)


In the given syntax str1 is Destination string and str2 – Source string.

strncpy() in c return the pointer to the destination string after this function copied  n characters of source string.

Example program for strncpy() function in c

#include <stdio.h>
#include <string.h>
int main () {
   char str1[20]; 
   char str2[25];
   /* The first argument in the function is destination string. 
    * In this case we are doing full copy using the strcpy() function. 
    * Here string str2 is destination string in which we are copying the 
    * specified string 
    */ 
   strcpy(str2, "welcome to fucntioninc.in");

   /* In this case we are doing a limited copy using the strncpy()
    * function. We are copying only the 7 characters of string str2 to str1
    */
   strncpy(str1, str2, 7);

   printf("String str1: %s\n", str1); 
   printf("String str2: %s\n", str2);

   return 0;
}



Friday, April 23, 2021

, , , ,

ellipse function in C programming

 ellipse function in c

 Learn to Declarations ellipse function in computer graphic

 void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius)


  This function is used to draw an ellipse where (x,y) are coordinates of center of that draw ellipse, stangle is the starting angle, end angle is the ending angle,  int xradius, int yradius are parameters specifies the X and Y radius of that drawn ellipses. 

Well if you want to draw an ellipse for strangles and end angle should be 0 and 360 respectively.

Program example for ellipse function in computer graphic


#include<graphics.h>
#include<conio.h>

main()
{
   int gd = DETECT, gm;

   initgraph(&gd, &gm, "C:\\TC\\BGI");

   ellipse(100, 100, 0, 360, 50, 25);

   getch();
   closegraph();
   return 0;
}


Wednesday, April 14, 2021

, , , ,

line function in computer graphics

line function in c programming :-

Well line function in c computer graphics is used as it name says to draw a line from a point(x1,y1) to point(x2,y2) where (x2,y2) are end points to draw a line.

Here you can also check the c program to draw a line in computer graphics.

 C example program

 #include <graphics.h>
#include <conio.h>

main()
{
   int gd = DETECT, gm;

   initgraph(&gd, &gm, "C:\\TC\\BGI");

   line(100, 100, 200, 200);

   getch();
   closegraph();
   return 0;
}

Monday, April 5, 2021

, , , , ,

Arc function in C with graphics in c

 Declaration: void arc(int x, int y, int stangle, int endangle, int radius);

"arc" operate is employed to draw associate arc with center (x, y) for example draw a corcle in c programming and stangle specifies beginning angle for arc, end angle for arc can be define by endangle and the and last parameter specifies the radius of the arc.

 arc operate may also be accustomed draw a circle except for that beginning angle and finish angle ought to be zero and 360 severally.

Draw A circle in c programming 

 #include <graphics.h>
#include <conio.h>

int main()
{
   int gd = DETECT, gm;

   initgraph(&gd, &gm, "C:\\TC\\BGI");

   arc(100, 100, 0, 135, 50);
   
   getch();
   closegraph();
   return 0;
}



Well in the written c program for circle in c which is having (100,100) center '0' is the starting angle, 135 is the end angle and '50' is an radious.

Thursday, March 18, 2021

, , ,

getline Function in c++

 Well as we know cin is an object which is used to take input from the user but does not allow to take the input in multiple lines.To accept the multiple lines as input we use getline c++

It is a pre-defined function in C++ which is defined in a <string.h> header file getline function c++ is used to  accept a line or a string from the input stream until the delimiting character is encountered by compiler.

 Syntax of getline() C++ function: 

this function can be represent in two way  :-

  • The first way of declaring function is to pass three parameters.
  1. istream& getline( istream& is, string& str, char delim ); 

the three parameter are  is, str, and delim.

Where :-

is: It is an object which is used from where to read the input stream.

str object: is used to stored a string.


delim: delimiting character.

Return value

getline function c++ returns the input stream object, which is passed as a parameter to the getline function.

  • The second mwthod of declaring is to pass two parameters.


istream& getline( istream& is, string& str ); 

as you can in above syntax we have two parameters whicch are is and str.

 this method does not have any delimiting character like first one.


is: It is an object which is used from where to read the input stream.

str object: is used to stored a string.

 Example of getline c++

 In this example we will take the input from user without using getline() function in C++

#include <iostream>   
#include  <string.h>
using namespace std;  
int main()  
{  
string name; // variable declaration  
std::cout << "Enter your name :" << std::endl;  
cin>>name;  
cout<<"\nHello "<<name;
return 0;  
}

OUTPUT 

getline c++
OUTPUT
As you see we give 'John Miller' as user input, but only 'John' was displayed in output. 

Let's resolve the above problem by using getline() C++ .

 #include <iostream>   
#include  <string.h> 
    using namespace std;  
    int main()  
    {  
    string name; // variable declaration.  
    std::cout << "Enter your name :" << std::endl;  
    getline(cin,name); // implementing a getline() function  
    cout<<"\nHello ""<<name;
return 0;  
}

In the above example we can get the full name with the help of getline function c++.

OUTPUT 

getline c++
OUTPUT getline function c++


Wednesday, March 17, 2021

, ,

fmod() function in C++

 fmod() function in C++ very useful for computes the floating point remainder of numerator/denominator (rounded towards zero).

Syntax for fmod() C++

fmod (x, y) = x - tquote * y

fmod() prototype

double fmod(double x, double y);
float fmod(float x, float y);
long double fmod(long double x, long double y);
double fmod(Type1 x, Type2 y); // Additional overloads for other combinations of arithmetic types

fmod() function in c++ takes two arguments and returns a value with the type of double, float or long double type.

fmod() C++ Parameters


    x as The value of numerator.
    y as The value of denominator.

Example 1 for How fmod() works in C++?


#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double x = 7.5, y = 2.1;
    double result = fmod(x, y);
    cout << "Remainder of " << x << "/" << y << " = " << result << endl;
    
    x = -17.50, y = 2.0;
    result = fmod(x, y);
    cout << "Remainder of " << x << "/" << y << " = " << result << endl;
    
    return 0;
}

OUTPUT : - 

fmod c++ example
OUTPUT



Friday, March 12, 2021

, , , , ,

Friend function in C++ with example program

Well hiding the data is core of  object-oriented programming but friend function in C++ is an exception which break the rule of OOP's.

friend function and friend classes in C++ programming helps to give the access member functions from outside the class.

or in the other words the protected and private data of a class can be accessed with the help of function in C++.

Declare friend function in C++

 friend function can access the private and protected it should be declare with the the friend keyword.

 

class className {
    ... .. ...
    friend returnType functionName(arguments);
    ... .. ...
}

 Example program to understand Working of friend Function

 

// C++ program to demonstrate the working of friend function

#include <iostream>
using namespace std;

class Distance {
    private:
        int meter;
        
        // friend function
        friend int addFive(Distance);

    public:
        Distance() : meter(0) {}
        
};

// friend function definition
int addFive(Distance d) {

    //accessing private members from the friend function
    d.meter += 5;
    return d.meter;
}

int main() {
    Distance D;
    cout << "Distance: " << addFive(D);
    return 0;
}

As you can see here, addFive() in upper example is a friend function that can access both the private and public data members.

 friend Class in C++

 friend class can be used in c++ programming with friend keyword.

class ClassB;

class ClassA {
   // ClassB is a friend class of ClassA
   friend class ClassB;
   ... .. ...
}

class ClassB {
   ... .. ...
}

 if a class is declared a friend class in c++ programming then all the member functions of newly become friend class become friend functions.

See in the upper example  ClassB is a friend class so we can access all members of ClassA from inside ClassB.

 But we can't access members of ClassB from inside ClassA. 

Example program for C++ friend Class


// C++ program to demonstrate the working of friend class

#include <iostream>
using namespace std;

// forward declaration
class ClassB;

class ClassA {
    private:
        int numA;

        // friend class declaration
        friend class ClassB;

    public:
        // constructor to initialize numA to 12
        ClassA() : numA(12) {}
};

class ClassB {
    private:
        int numB;

    public:
        // constructor to initialize numB to 1
        ClassB() : numB(1) {}
    
    // member function to add numA
    // from ClassA and numB from ClassB
    int add() {
        ClassA objectA;
        return objectA.numA + numB;
    }
};

int main() {
    ClassB objectB;
    cout << "Sum: " << objectB.add();
    return 0;
}