Watch & Subscribe my SQL videos on YouTube | Join me on Facebook

C++ Program to check Command Line Arguments – Q4

January 4, 2010 Leave a comment

Q4. Program to check Command line arguments:

Accept an n digit number as command line argument & perform various tasks specified below:
 i) Find the sum of digits of the number
ii) Find the reverse of a number

… from College notes (BCA/MCA assignments):

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

void Sum_Num(char *);
void Rev_Num(char *);

void main(int argv, char *argc[]){
	char patt[10];
	clrscr();
	if(argv < 1)
		cout<<"\n Insuffecient Arguments. \n";
	else{
		strcpy(patt, argc[1]);
		cout<<”\n Argument is: “<<patt;
		Sum_Num(patt);
		Rev_Num(patt);
		}
	getch();
	}

void Sum_Num(char *pt){
	int sum = 0, i = 0;
	while(*(pt+i) != '\0'){
		sum += int(*(pt+i)) - 48;
		i++;
		}
	cout<<"\n Sum: "<<sum;
	}

void Rev_Num(char *pt){
	long r_num = 0;
	int i = 0;
	while(*(pt+i) != '\0'){
		r_num += (int(*(pt+i)) - 48) * pow(10, i);
		i++;
		}
	cout<<"\n Reverse: "<<r_num;
	}

 

Output:

Argument is: 12345

Sum: 15

Reverse: 54321


Categories: Cpp Tags: , , , ,

C++ Program for Array Sorting, Searching and Deletion of element by using Pointers – Q3

January 3, 2010 Leave a comment

Q3. Program for Array Sorting, Searching and Deletion of element:

Perform following task with an array of N numbers:
  i) Sort the array in ascending order
 ii) Search the given number in the array & notify its position in the list if present.
iii) Delete a given no from the array from a given position

… from College notes (BCA/MCA assignments):
 

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

void Sort_Array (int *, const int);
void Search_Elm (int *, const int);
void Delete_Elm (int *, int &);

void main(){
	int *arr = new(int);
	int n, ch;

	clrscr();

	cout<<"\n How many Number of Elements in an Array?: ";
	cin>>n;
	cout<<"\n Enter "<<n<<" Elements: ";
	for(int i=0; i<n; i++)
		cin>>*(arr+i);

	while(1){
		clrscr();
		cout<<"\n Array is: ";
		for(i=0; i<n; i++)
			cout<<*(arr+i)<<" ";
		
		cout<<"\n\n Main Menu. \n";
		cout<<"\n 1 -> Sort Elements.";
		cout<<"\n 2 -> Search a Given Element.";
		cout<<"\n 3 -> Delete an Element.";
		cout<<"\n 4 -> Exit.";

		cout<<"\n Enter your choice: ";
		cin>>ch;

		switch(ch){
			case 1:
				Sort_Array(arr, n);
				break;
			case 2:
				Search_Elm(arr, n);
				break;
			case 3:
				Delete_Elm(arr, n);
				break;
			case 4:
				delete(arr);
				exit(1);
			default:
				cout<<"\n Enter Correct Choice.";
			} //  end of switch.
		getch();
		} // end of while.
	} // end of main.

void Sort_Array(int *arr, const int n){
	int temp;

	for(int i=0; i<n; i++){
		for(int j=i; j<n; j++){
			if( *(arr+i) > *(arr+j) ){
				temp = *(arr+i);
				*(arr+i) = *(arr+j);
				*(arr+j) = temp;
				}
			}
		}

	cout<<"\n Array Sorted.";
	}

void Search_Elm(int *arr, const int n){
	int elm, found=0;

	cout<<"\n Enter an Element to Search: ";
	cin>>elm;

	for(int i=0; i<n; i++){
		if( elm == *(arr+i) ){
			cout<<"\n Element "<<*(arr+i)<<" in position " 
    <<i+1;
			found = 1;
			}
		}
		if(!found)
			cout<<"\n Element not Found.";
	}

void Delete_Elm(int *arr, int &n){
	int elm, found = 0;

	cout<<"\n Enter an Element to Delete: ";
	cin>>elm;

	for(int i=0; i<n; i++){
		if( elm == *(arr+i) ){
			found = 1;
			break;
			}
		if(i >= n)
			found = 0;
		}
	for(int j=i; j<n; j++)
		*(arr+j) = *(arr+j+1);

	if(!found)
		cout<<"\n Element not Found.";
	else{
		cout<<"\n Element Deleted.";
		n--;
		}
	}

 

Output:

How many Number of Elements in an Array?: 5

Enter 5 Elements: 21
23
645
32
19

Array is: 19 21 23 32 645

Main Menu.

1 -> Sort Elements.
2 -> Search a Given Element.
3 -> Delete an Element.
4 -> Exit.
Enter your choice: 1

Array Sorted.

Array is: 19 21 23 32 645

Main Menu.

1 -> Sort Elements.
2 -> Search a Given Element.
3 -> Delete an Element.
4 -> Exit.
Enter your choice: 2

Enter an Element to Search: 33

Element not Found.

Array is: 19 21 23 32 645

Main Menu.

1 -> Sort Elements.
2 -> Search a Given Element.
3 -> Delete an Element.
4 -> Exit.
Enter your choice: 2

Enter an Element to Search: 32

Element 32 in position 4


C++ Program to find Sum of Series (When n is Odd/Even) – Q2

January 2, 2010 Leave a comment

Q2. Program to find Sum of Series:
i) x-x^3/3!-x^5/5!+x^7/7!-….. x^n/n! (When n is odd)
ii) x^2/2!-x^4/4!+x^6/6!-….. x^n/n! (When n is even)

… from College notes (BCA/MCA assignments):
 

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

long Fact(long);
float Odd_Series(int, int);
float Even_Series(int , int);

int main(){
	int x, n;
	float sum;
	clrscr();

	cout<<"\n Enter the Value of x: ";
	cin>>x;
	cout<<"\n Enter the Value of n: ";
	cin>>n;

	sum = Odd_Series(x, n);
	cout<<"\n Sum of Odd Series: "<<sum;

	sum = Even_Series(x, n);
	cout<<"\n Sum of Even Series: "<<sum;

	getch();
	return 0;
	}

long Fact(long f){
	if(f<=0)
		return 1;
	else
		return (f * Fact(f-1));
}

float Odd_Series(int x, int n){
	int i, f;
	float sum = 0;
	for(i=1, f=0; i<=n; i=i+2, f++)
		sum += pow(-1, f) * pow(x, i) / Fact(i);
	return sum;
}

float Even_Series(int x, int n){
	int i, f;
	float sum = 0;

	for(i=2, f=0; i<=n; i=i+2, f++)
		sum += pow(-1, f) * pow(x, i) / Fact(i);

	return sum;
}

 

Output:

Enter the Value of x: 4

Enter the Value of n: 5

Sum of Odd Series: 1.866667
Sum of Even Series: -2.666667


Categories: Cpp Tags: ,

C++ Program to find Roots (Real & Complex) of a Quadratic Equation – Q1

January 1, 2010 Leave a comment

Q1. Program to find Roots (Real & Complex) of a Quadratic Equation:

… from College notes (BCA/MCA assignments):
 

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

void main(){
	int aint, bint, cint, b2, dsq;
	float rt1 , rt2, d;
	clrscr();

	cout<<"\n Quadratic Equation is:- \n";
	cout<<"\n\t a*x^2 + b*x + c";

	cout<<"\n Enter the Value of a: ";
	cin>>aint;

	cout<<"\n Enter the Value of b: ";
	cin>>bint;

	cout<<"\n Enter the Value of c: ";
	cin>>cint;

	b2 = pow(bint, 2);
	d = b2 - 4 *  aint * cint;

	if(d > 0){
		dsq = sqrt(d);
		rt1 = (-bint + dsq) / (2 * aint);
		rt2 = (-bint - dsq) / (2 * aint);
		cout<<"\n Roots are Real and distinct";
		}
	else if(d == 0){
		rt1 = -bint/2 * aint;
		rt2 = rt1;
		cout<<"\n Roots are Real and same";
		}
	else {
		rt1 = -bint/2 * aint;
		rt2 = sqrt(-bint)/2 * aint;
		cout<<"\n Roots are Complex and distinct";
		}

	cout<<"\n rt1 : "<<rt1;
	cout<<"\n rt2 : "<<rt2;

	getch();
}

 

Output:

Equation is:-

a*x^2 + b*x + c

Enter the Value of a: 3

Enter the Value of b: 4

Enter the Value of c: 5

Roots are:-
rt1 : 9.476982e-41
rt2 : 219.999786


SQL Basics – Working with Foreign Key (FK) constraints

December 27, 2009 3 comments

A foreign key (FK) is a column or combination of columns that is used to establish and enforce a link between the data in two tables, as per MSDN BoL.
 

–> Foreign Key (FK) Constraint

1. We can create a FK by defining a FOREIGN KEY constraint while Creating or modifying (Altering) a table.

2. We can define a FK in a table that points to a PK or UK in another table.
   – Like Employee & Department, shown in video-demo below.

3. A FK constraint can reference columns within the same table, these are called self-referencing tables. For example: Employee & Manager Relationship.
 

–> Referential Integrity:

1. The FK constraint enforces Referential Integrity by not allowing values in the Child table outside form the domain of the Parent table.

2. This also disallows any changes to the Parent table, like deleting any row or modifying PK value in the Parent table.
 

–> Check the video on Foreign Key:

FK Constraint
 

–> Department and Employee table FK relationship:

FK Constraint
 

–> SQL Code used in above video:


-- 1. Parent table:

CREATE TABLE [dbo].[Department](
	 [DeptID]	int IDENTITY (101, 1) PRIMARY KEY
	,[DeptName]	nvarchar(100)
	,[isActive]	bit DEFAULT(1)
)
GO

INSERT INTO [dbo].[Department] ([DeptName])
SELECT 'HR'
UNION ALL
SELECT 'Finance'
UNION ALL
SELECT 'Admin'
UNION ALL
SELECT 'IT'

select * from [dbo].[Department]
GO


-- 2. Child Table:

-- Method #1.a : with Column inline
CREATE TABLE [dbo].[Employee](
	 [EmployeeID]	int IDENTITY (100, 1) PRIMARY KEY
	,[EmployeeName]	nvarchar(100)
	,[Gender]		nchar(1)
	,[DeptID]		int NOT NULL FOREIGN KEY REFERENCES Department(DeptID)
	,[isActive]		bit DEFAULT(1)
)
GO

-- Method #1.b : with Column inline & Named FK
DROP TABLE [dbo].[Employee]
GO
CREATE TABLE [dbo].[Employee](
	 [EmployeeID]	int IDENTITY (100, 1) PRIMARY KEY
	,[EmployeeName]	nvarchar(100)
	,[Gender]		nchar(1)
	,[DeptID]		int NOT NULL CONSTRAINT FK_EmployeeID_DeptID FOREIGN KEY REFERENCES Department(DeptID)
	,[isActive]		bit DEFAULT(1)
)
GO

-- Method #2.a : with Explicit
DROP TABLE [dbo].[Employee]
GO
CREATE TABLE [dbo].[Employee](
	 [EmployeeID]	int IDENTITY (100, 1) PRIMARY KEY
	,[EmployeeName]	nvarchar(100)
	,[Gender]		nchar(1)
	,[DeptID]		int NOT NULL
	,[isActive]		bit DEFAULT(1)
		FOREIGN KEY ([DeptID]) REFERENCES Department(DeptID)
)
GO

-- Method #2.b : with Explicit & Named FK
DROP TABLE [dbo].[Employee]
GO
CREATE TABLE [dbo].[Employee](
	 [EmployeeID]	int IDENTITY (100, 1) PRIMARY KEY
	,[EmployeeName]	nvarchar(100)
	,[Gender]		nchar(1)
	,[DeptID]		int NOT NULL
	,[isActive]		bit DEFAULT(1)
		CONSTRAINT FK_EmployeeID_DeptID FOREIGN KEY ([DeptID]) REFERENCES Department(DeptID)
)
GO

-- Method #3 : with ALTER Table Named FK
DROP TABLE [dbo].[Employee]
GO
CREATE TABLE [dbo].[Employee](
	 [EmployeeID]	int IDENTITY (100, 1) PRIMARY KEY
	,[EmployeeName]	nvarchar(100)
	,[Gender]		nchar(1)
	,[DeptID]		int NOT NULL
	,[isActive]		bit DEFAULT(1)
)
GO

-- SSMS or ALTER Table stmt:

-- Generated by SSMS - Table Designer:
ALTER TABLE dbo.Employee ADD CONSTRAINT
	FK_Employee_Department FOREIGN KEY
	(
	DeptID
	) REFERENCES dbo.Department
	(
	DeptID
	)

-- OR --

ALTER TABLE [dbo].[Employee]
ADD CONSTRAINT FK_EmployeeID_DeptID FOREIGN KEY ([DeptID]) REFERENCES Department(DeptID)
GO


INSERT INTO Employee ([EmployeeName], [Gender], [DeptID])
VALUES ('Manoj Pandey', 'M', 101)

INSERT INTO Employee ([EmployeeName], [Gender], [DeptID])
VALUES ('Deepak B', 'M', 102)

INSERT INTO Employee ([EmployeeName], [Gender], [DeptID])
VALUES ('Jhon B', 'M', 103)

INSERT INTO Employee ([EmployeeName], [Gender], [DeptID])
VALUES ('Mariya Y', 'F', 104)

select * from [dbo].[Employee]
GO


-- REFERENTIAL INTEGRITY:

INSERT INTO Employee ([EmployeeName], [Gender], [DeptID])
VALUES ('Mary J', 'F', 105)
--Msg 547, Level 16, State 0, Line 115
--The INSERT statement conflicted with the FOREIGN KEY constraint "FK_EmployeeID_DeptID". The conflict occurred in database "TestManDB", table "dbo.Department", column 'DeptID'.
--The statement has been terminated.

DELETE FROM [dbo].[Department]
WHERE DeptID = 102
--Msg 547, Level 16, State 0, Line 118
--The DELETE statement conflicted with the REFERENCE constraint "FK_EmployeeID_DeptID". The conflict occurred in database "TestManDB", table "dbo.Employee", column 'DeptID'.
--The statement has been terminated.

DROP TABLE [dbo].[Department]
--Msg 3726, Level 16, State 1, Line 121
--Could not drop object 'dbo.Department' because it is referenced by a FOREIGN KEY constraint.

GO


-- Allowed updates:

select * from [dbo].[Department]

UPDATE [dbo].[Department]
SET DeptName = 'Human Resource'
WHERE DeptID = 101

select * from [dbo].[Department]

GO


-- Final Cleanup:

DROP TABLE [dbo].[Employee]
GO
DROP TABLE [dbo].[Department]
GO

Check Primary Key & Unique Key constraints

To know about more on Constraints and their types check this blog post.