RSS

Category Archives: Functions

FUNCTION TO PERFORM SUM OF THE SERIES GIVEN

/* FUNCTION TO PERFORM THE SUM OF THE FOLLOWING SERIES

x – x^2/3! + x^3/5! – ….. upto n terms */

#include <iostream>

double sumseries(double x, int n);

using namespace std;

int main()
{
int n;
double x;

cout<<“\nEnter the value of ‘x’ \n”;
cin>>x;
cout<<“\nEnter the number of terms\n”;
cin>>n;

double result = sumseries(x,n);
cout<<“\nThe result is “<<result;

return 0;
}

double sumseries(double x, int n)
{
double sumo = 0.0, sume = 0.0;
double val = 1.0;
double fact = 1;
for(int i = 1; i<= n; i++)
{
val = 1;
fact = 1;
for(int j = 1; j<=i; j++)
{
val *= x;
}
for(int k = 2*i – 1; k>1; k–)
{
fact *= k;
}
if(i%2 == 0)
{
sume += val/fact;
}
else
{
sumo += val/fact;
}
}

return (sumo – sume);
}

 
Leave a comment

Posted by on November 21, 2015 in Functions, Getting Started

 

PROGRAM TO PERFORM OPERATION ON TEXT FILE BASED ON CHOICE

/*
THIS PROGRAM COUNTS THE FOLLOWING IN A TEXT FILE (CHOICE BASED PROGRAM)

– UPPERCASE ALPHABETS       – TOTAL ALPHABETS
– TOTAL DIGITS              – TOTAL WHITE SPACES
– TOTAL VOWELS              – AVERAGE WORD SIZE
*/

#include <fstream.h>
#include <conio.h>
#include <ctype.h>
#include <process.h>

int upper(ifstream &);
int total(ifstream &);
int digits(ifstream &);
int white(ifstream &);
int vowels(ifstream &);
int wordsize(ifstream &);

void main()
{
clrscr();
ifstream file;

char add[20];
char ch;

cout<<“ENTER THE NAME OF THE FILE \n”;
cin.getline(add,20);

char wish = ‘Y’;

do
{
file.open(add);
if(!file)
{
cout<<“\nFILE COULD NOT BE OPENED\n”;
exit(-1);
}

clrscr();

cout<<“CHOOSE AN OPTION\n\n”;
cout<<“\n1. COUNT UPPERCASE ALPHABETS\n”;
cout<<“\n2. COUNT TOTAL ALPHABETS\n”;
cout<<“\n3. COUNT NUMBER OF DIGITS\n”;
cout<<“\n4. COUNT WHITE SPACES\n”;
cout<<“\n5. COUNT VOWELS\n”;
cout<<“\n6. COUNT AVERAGE WORD-SIZE\n\n”;

int choice, count = 0;
cin>>choice;

switch(choice)
{
case 1: count = upper(file);
break;
case 2: count = total(file);
break;
case 3: count = digits(file);
break;
case 4: count = white(file);
break;
case 5: count = vowels(file);
break;
case 6: count = wordsize(file);
break;
default: cout<<“\nINVALID CHOICE\n”;
break;
}

cout<<“\nTHE REQUESTED VALUE IS \t”<<count;
cout<<endl;
cout<<“\nDO ANOTHER TASK ? (Y/N)\t”;
cin>>wish;
file.close();
}
while(wish == ‘Y’ || wish == ‘y’);

cout<<“\nTHANKS!!!”;
getch();
}

int upper(ifstream &file)
{
char ch;
int count = 0;
while(file.get(ch))
{
if(isupper(ch))
count++;
}
return count;
}

int total(ifstream &file)
{
char ch;
int count = 0;
while(file.get(ch))
{
if(isalpha(ch))
count++;
}
return count;
}

int digits(ifstream &file)
{
char ch;
int count = 0;
while(file.get(ch))
{
if(isdigit(ch))
{
count++;
}
}
return count;
}

int white(ifstream &file)
{
char ch;
int count = 0;
while(file.get(ch))
{
if(ch == ‘ ‘ || ch == ‘\n’ || ch == ‘\t’)
count++;
}
return count;
}

int vowels(ifstream &file)
{
char ch;
int count = 0;
while(file.get(ch))
{
switch(ch)
{
case ‘A’ :  count++;
break;
case ‘E’ :  count++;
break;
case ‘I’ :  count++;
break;
case ‘O’ :  count++;
break;
case ‘U’ :  count++;
break;
case ‘a’ :  count++;
break;
case ‘e’ :  count++;
break;
case ‘i’ :  count++;
break;
case ‘o’ :  count++;
break;
case ‘u’ :  count++;
break;
default  :  break;
}
}
return count;
}

int wordsize(ifstream &file)
{
int count = 0;
int size = 0;
char word[20];

while(file.eof() == 0)
{
file.getline(word,20,’ ‘);

if(file.eof())
break;
int num = 0;
for(int i =0; word[i] != ”; i++)
{
if(word[i] != ‘\n’ || word[i] != ‘.’ || word[i] != ‘\t’ || word[i] !=’ ‘)
num++;
}
size += num;
if(num != 0)
count++;
}

cout<<“\nNUMBER OF WORDS ARE \t”<<count<<‘\n’;

return size/count;
}

 
Leave a comment

Posted by on July 31, 2015 in File Handling, Functions

 

FUNCTION TO COUNT THE NUMBER OF LINES IN A TEXT FILE

// THIS FUNCTION COUNTS THE NUMBER OF LINES IN A FILE

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

int countlines(ifstream &);

void main()
{
clrscr();
ifstream file;
char add[20];

cout<<“ENTER THE NAME OF THE FILE\n”;
gets(add);

file.open(add,ios::in);

if(!file)
{
cout<<“\nFILE COULD NOT BE OPENED\n”;
exit(1);
}

int lines = countlines(file);

cout<<“\nTHE NUMBER OF LINES ARE \t”<<lines;
getch();
}

int countlines(ifstream &file)
{
char ch;
int count = 0;
while(file.get(ch))
{
if(file.good() == 0)
break;
if(ch == ‘.’)
count++;
}
return count;
}

 
Leave a comment

Posted by on July 31, 2015 in File Handling, Functions

 

FUNCTION TO PRINT THE MIDDLE ROW AND MIDDLE COLUMN IN A MATRIX

/* THIS PROGRAM DISPLAYS THE ELEMENTS OF THE MIDDLE ROW AND MIDDLE COLUMN
IN A MATRIX  */

#include <iostream.h>
#include <conio.h>

void display(int [][100], int);

void main()
{
clrscr();
int arr[100][100],size=0;

cout<<“Enter the Order of Matrix\n”;
cin>>size;
cout<<“\nNow input the Matrix\n”;

for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
cin>>arr[i][j];
}
}

display(arr,size);
getch();
}

void display(int arr[][100], int size)
{
if(size%2==0)
{
cout<<“\nMiddle Rows are\n”;
}
else
{
cout<<“\nThe Elements of the Middle row Are\n”;
}

int i = (size-1)/2, j=0;

while(j<size)
{
cout<<arr[i][j]<<‘ ‘;
j++;
}
cout<<‘\n’;

if(size%2==0)
{
j=0;
i++;
while(j<size)
{
cout<<arr[i][j]<<‘ ‘;
j++;
}
}

cout<<“\n\nThe Elements of the Middle Column are\n”;

j = (size-1)/2;
i=0;

while(i<size)
{
cout<<arr[i][j]<<‘\n’;
i++;
}

if(size%2==0)
{
cout<<“\nand\n”;
j++;
i=0;
while(i<size)
{
cout<<arr[i][j]<<‘\n’;
i++;
}
}
}

 
Leave a comment

Posted by on June 2, 2015 in Functions, Matrices

 

FUNCTION TO PRINT THE NON DIAGONAL ELEMENTS IN A MATRIX

// THIS FUNCTION PRINTS ALL THE NON DIAGONAL ELEMENTS IN A MATRIX

#include <iostream.h>
#include <conio.h>

void non_diagonal(int arr[][4]);

void main()
{
clrscr();
int arr[4][4];

cout<<“Enter the Matrix\n”;

for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
cin>>arr[i][j];
}
}

cout<<“\nThe Non Diagonal Elements are \n”;

non_diagonal(arr);

getch();
}

void non_diagonal(int arr[][4])
{
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(!((i==j) || (i+j == 3)))
{
cout<<arr[i][j];
}
cout<<‘ ‘;
}
cout<<‘\n’;
}
}

 
Leave a comment

Posted by on June 2, 2015 in Functions, Matrices

 

PROGRAM TO RETURN THE SUM OF ODD/EVEN/ALL ELEMENTS (AS PER USER’S CHIOCE) OF AN INTEGER ARRAY

/* THIS PROGRAM TAKES AN ARRAY AND PERFORMS OPERATIONS ON IT
BY CALLING AN OVERLOADED FUNCTION    */

#include <iostream.h>
#include <conio.h>

int sum( int [], int);
int sum( int [], int, char);

void main()
{
clrscr();

int arr[30], size=0, val;

cout<<“ENTER THE NUMBER OF ELEMENTS YOU WANT TO ENTER\n”;
cin>>size;

cout<<“\nENTER THE ELEMENTS OF THE ARRAY\n”;

for(int i=0; i<size; i++)
{
cin>>arr[i];
}

char ch, wish;
clrscr();

cout<<“GOOD! NOW CHOOSE ANY ONE OPERATION\n”;
cout<<“\n1. SUM OF ALL ELEMENTS (PRESS ‘A’)\n”;
cout<<“2. SUM OF EVEN ELEMENTS (PRESS ‘E’)\n”;
cout<<“3. SUM OF ODD ELEMENTS (PRESS ‘O’)\n\n”;

do{
cout<<“\nPLEASE ENTER YOUR CHOICE\n”;
cin>>ch;

if(ch == ‘A’ || ch == ‘a’)
{
val = sum(arr, size);
}
else
{
val = sum(arr, size, ch);
}

cout<<“\nTHE REQUIRED ANSWER IS \t”<<val;
cout<<“\n\n\nTRY ANOTHER OPTION ? (Y/N)  “;
cin>>wish;

if(wish != ‘N’  ||   wish  !=  ‘n’)
clrscr();

}while(wish == ‘Y’ ||  wish == ‘y’);

if(wish == ‘N’  ||  wish == ‘n’)
cout<<“\nProgram Over. Press Any key to Terminate…\n”;

getch();
}

int sum(int arr[], int size)
{
int sum=0;

for(int i=0; i<size; i++)
{
sum += arr[i];
}

return sum;
}

int sum(int arr[], int size, char choice)
{
int sum=0;

if(choice == ‘E’  ||   choice == ‘e’)
{
for(int i=0; i<size; i++)
{
if(arr[i] % 2 == 0)
sum += arr[i];
}
}

else if(choice == ‘O’  ||   choice == ‘o’)
{
for(int i=0; i<size; i++)
{
if (arr[i] % 2 != 0)
sum += arr[i];
}
}

else
sum = 0;

return  sum;

}

 
Leave a comment

Posted by on May 15, 2015 in Functions

 

FUNCTION TO CONVERT A STRING TO SUITABLE CASE

/* THIS PROGRAM CREATES A FUNCTION TO CONVERT A STRING TO CORRESPONDING CASE
e.g if the string is:    “HappY Go lUcKy dAys”
then it prints: “Happy Go Lucky Days”       */
#include <iostream.h>
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

void top(char[]);

void main()
{
clrscr();
char ary[40];
cout<<“Enter any string\n”;
gets(ary);
cout<<“\nConverting to uppercase\n”;
top(ary);
getch();
}

void top(char a[40])
{
for(int x=0;a[x]!=”;x++)
{
if (x==0)
{
a[x]=toupper(a[x]);
}
else
{
if((a[x-1])==’ ‘)
{
a[x]=toupper((a[x]));
}
else
{
if(a[x-1]!=’ ‘)
{
a[x]=tolower(a[x]);
}
}
}
cout<<a[x];
}
return;
}

 
Leave a comment

Posted by on March 2, 2015 in Functions