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;
}


Tuesday, March 9, 2021

, , , ,

Substring function in r programming

 Substring() function in R can be used to extract the characters present in the data or to manipulate the given data.

 You can easily extract the required characters from a string and  Substring() in r can also replace the values in a string.

Syntax for substring r :-

substr(x,start,stop)
substring(x,first,last=1000000L)

 Here we can do so many things with substring function in r link extracting of values, replacement of values etc.

    x =  input data / file.
    Start / First= starting index of the given substring.
    Stop / Last= Ending index of the substring.

r extract substring :-

#returns the characters from 1,11
df<-("computer_dev_private_limited")
substring(df,1,11)

OUTPUT : - 

"computer_de"

We hope you understand how to r extract part of the data from the given string.

r replace substring with program : -

#returns the string by replacing the _ by space
df<-("We are_developers")
substring(df,7,7)=" "
df


Output = “We are developers”

Put the same value to replace the values in a string with the help of
 Substring() function in R.

String replacement using substring() function with multiple value :-

#replaces the 4th letter of each string by $
df<-c("Alok","Joseph","Hayato","Kelly","Paloma","Moca")
substring(df,4,4)<-c("$")
df


Output = “Alo$” “Jos$ph” “Hay$to” “Kel$y” “Pal$ma” “Moc$”

Here the 4th letter in the given strings replaced by the "$" sign with the help of substring r.

Learn to use of str_sub() and substr() function in R :-

 well r programming is used to create the data in the form od row or colums.

With the help of given example you can learn to add data in existing data with the help of substr() function in R.

Example for substr() in r : -

#creates the data frame
df<-data.frame(Technologies=c("Datascience","machinelearning","Deeplearning","Artificalintelligence"),Popularity=c("70%","85%","90%","95%"))
df

OUTPUT : - 

OUTPUT
OUTPUT

Example for str_sub() function in R

#using the str_sub function
df$Extracted_Technologies=str_sub(df$Technologies,10,15)
> df

substring r
substr in r


Saturday, March 6, 2021

, , , , ,

Example program for isxdigit() function in c

 The isxdigit() function in c programming checks whether a character is a hexadecimal digit character (0-9, a-f, A-F) or not.

Syntax for isxdigit in c :-

int isxdigit( int arg );

 isxdigit() Parameters

The isxdigit() in c takes a single character as it's parameter.

 C isxdigit() Return Value

 if passed argument is an hexadecimal character then isxdigit() return non-zero integer otherwise return 0.

Example program for C isxdigit() function


#include <ctype.h>
#include <stdio.h>

int main() {
   char c = '5';
   int result;

   // hexadecimal character is passed
   result = isxdigit(c); // result is non-zero
   printf("Result when %c is passed to isxdigit(): %d", c, isxdigit(c));

   c = 'M';

   // non-hexadecimal character is passed
   result = isxdigit(c); // result is 0

   printf("\nResult when %c is passed to isxdigit(): %d", c, isxdigit(c));

   return 0;
}

OUTPUT : - 



C isxdigit()
C isxdigit()