Monday, December 21, 2020

, ,

r plot program example and how to use it.

 R Plot Function is used to plot a graph in r programming in this r function we can vector and then we get a graph which is about  magnitude vs index.

r plot function has so many method which is called according to the passed value to this r plot function.

For example we pass two vector plot(c(1,2),c(3,5)) and then this function plot a scatter graph for these two point.

here we plot graph for sine function form range -pi to pi as you can see in this picture.


<- pi="" plot="" pre="" seq="" sin="" x="">x <- seq(-pi,pi,0.1) plot(x, sin(x))

 

R Plot Function
 R Plot Function

Adding Titles and Labeling Axes to the r plot function

Here you can add title and label to the with parameter main and xlab and ylab two parameter for labeling x-axis and y-axis in plot graph.

plot(x, sin(x),
main="The Sine Function",
ylab="sin(x)")

 

Axes to the r plot function
Labeling Axes to the r plot function

 Changing Color and Plot Type for r function : -

 Well you can change the color for plot type with the help of the argument type in r languages.

Check out the example for r plot with color:- 

plot(x, sin(x),
main="The Sine Function",
ylab="sin(x)",
type="l",
col="blue")

 here we can define the color with col

 

changing Color and Plot Type
changing Color and Plot Type

 

Friday, December 18, 2020

,

Use of clrscr function in c with example

 clrscr function in C programming is use to clear the screen and move the arrow upper left-hand corner of your computer screen.

this function work only for the GCC compiler otherwise use clear/cls command.

Example program for clrscr function in c

#include<stdio.h>
#include<conio.h>

int main()
{
   printf("Press any key to clear the screen.\n");

   getch();

   clrscr();

   printf("This appears after clearing the screen.\n");
   printf("Press any key to exit...\n");

   getch();
   return 0;
}

As you can see with the help of printf function computer is asking to press any key to clear the screen and then the second message will be displayed.

,

Example program for pow function in c

 pow function c programming is use to computer the power of a given number to the function.

pow() c function takes two arguments first one is known as base and second one as power value and pow() c function return a base with the power raised to that base number.

Syntax for pow() c

[Mathematics] xy = pow(x, y) [In programming]

pow c function defines under the math.h header file in c.

If you want to find the pow of any int and float number then convert the operator type into the double.

int base = 3;
int power = 5;
pow(double(base), double(power));

Example for C pow() function


#include <stdio.h>
#include <math.h>

int main()
{
    double base, power, result;

    printf("Enter the base number: ");
    scanf("%lf", &base);

    printf("Enter the power raised: ");
    scanf("%lf",&power);

    result = pow(base,power);

    printf("%.1lf^%.1lf = %.2lf", base, power, result);

    return 0;
}


program output for pow function in c
program output for pow function in c

, ,

Write a program for feof function in c

 feof function in c programming is used for file handling.

 feof c use to find the end of an file.

Syntax for feof c program :- 

int feof(FILE *fp)

Example program for feof function in c

# include <stdio.h>
int main( )
{
   FILE *fp ;
   char c ;
   printf( "Opening the file test.c in read mode" ) ;
   fp = fopen ( "test.c", "r" ) ; // opening an existing file
   if ( fp == NULL )
   {
     printf ( "Could not open file test.c" ) ;
     return 1;
   }
   printf( "Reading the file test.c" ) ;
   while ( 1 )
   {
     c = fgetc ( fp ) ; // reading the file
     if( feof(fp) )
     break ;
     printf ( "%c", c ) ;
   }
   printf("Closing the file test.c as end of file is reached.");
   fclose ( fp ) ; // Closing the file
   return 0;
}

This file handling program use to read a open file and feof c function used to find the end of the file.

feof c
program output for feof c


, ,

Example for rand function in c programming

 rand function in c programming is used to generate the pseudo-random numbers with the help of rand c function.

rand c function return the pseudo-random numbers which is exist between 0 and RAND_MAX.

rand function in c didn't use any parameters.

Syntax for rand c : - 

int rand(void)

Example program for rand c 

#include <stdio.h>
#include <stdlib.h>

int main () {
   int i, n;
   time_t t;
   
   n = 5;
   
   /* Intializes random number generator */
   srand((unsigned) time(&t));

   /* Print 5 random numbers from 0 to 49 */
   for( i = 0 ; i < n ; i++ ) {
      printf("%d\n", rand() % 50);
   }
   
   return(0);
}


output program for rand c
output program for rand c

, ,

Example program for strcat() function in c

 If you have a computer programming background then we believe you heard the name of concatenation this word in programming language is known for joins to strings.

program for strcat() function in c
Example program for strcat() function in c

Here in c language strcat() function in c use to joins two given string in c programs. 

Syntax for strcat c

char *strcat(char *destination, const char *source)

strcat c function defines in string.h header file.

strcat() c arguments

strcat() function use two arguments

  • destination - first one is destination string.
  • source - source of the string is second one.

strcat() function in c add  destination string and the source string and final result will be stored in destination string.

Example program for strcat() c

 

#include <stdio.h>
#include <string.h>
int main() {
   char str1[100] = "This is ", str2[] = "functioninc.in";

   // concatenates str1 and str2
   // the resultant string is stored in str1.
   strcat(str1, str2);

   puts(str1);
   puts(str2);

   return 0;
}


, ,

Example program for sprintf c function

  •  sprintf c function in used to convert any integer into a string.
  •   sprintf function in c didn't send the output to the console return an formatted string.

Syntax for sprintf c function 

int sprintf(char *str, const char *control_string, [ arg_1, arg_2, ... ]);
  •  First argument is pointer to a targeted string for sprintf function in c. 
  •  Other arguments are same like printf() function in c.
  • sprintf function in c use a variable str to write the data and give the number characters written to str.
  • sprintf c return the -1 if any error occurs. 

 Example for sprintf c function : - 

#include<stdio.h>
#include<string.h>
int factorial(int );

int main()
{

    int sal;
    char name[30], designation[30], info[60];

    printf("Enter your name: ");
    gets(name);

    printf("Enter your designation: ");
    gets(designation);

    printf("Enter your salary: ");
    scanf("%d", &sal);

    sprintf(info, "Welcome %s !\nName: %s \nDesignation: %s\nSalary: %d",
        name, name, designation, sal);

    printf("\n%s", info);

    // signal to operating system program ran fine
    return 0;
}


program output for sprintf function in c
program output for sprintf function in c