Tuesday, April 6, 2010

For the undergraduates

Not only B.E./B.Tech, but there are several courses out there that teach C/C++ in 1st or 2nd semester. Students who don't have slightest idea of this language at this tender age run helter-skelter to complete their assignments. For them, I'm posting here all the programs that I developed/wrote/coded for assignments when I was in second semester, thats long back in first half of 2007. Copy these, learn these, understand these, throw these, I don't care. I'm upping them here with a hope that they will be of some use to someone in great need indeed.

Disclaimer: These are programs, not software(if you consider IEEE definition of software) so these don't have any documentation, nor do I guarantee their successful run in all test cases. Its up to you to edit, modify, improve the programs. At the time of posting I didn't check the programs, so I won't be liable for any bugs. ANother thing is that these programs should be bug free. If you are getting any error from your compiler then it may be some trivial issues like conio.h or anything like that. In such a case do some research and elliminate the bug yourself, this will help you in your academics. But if you aren't able to do, then post a comment here and tell me which program is giving what error in which compiler and I'll post the solution.

Here I start, from easy to tougher:

1. Program to print the table of a user entered number.

#include <iostream.h>
int a, i;
int main()
{
cout<<"Enter the number whose table u want to print:\n ";
cin>>a;
for(i=1;i<=10;i++)
{
cout<<a<<" * "<<i<<"="<<a*i<<"\n";
}
return (0);
}


2. Program to find factorial of a number using recursion

#include <iostream.h>
long double fact(long int num){
if (num==1){
return (1);
}
else{
return (double)(num*fact(num-1));
}
}
int main(){
long int a;
cout<<"Enter number:\n";
cin>>a;
cout<<fact(a)<<endl;
return (0);
}



3. Program to find sum of digits of a number

#include <iostream.h>
#include <conio.h>
void main(){
int a, s=0, i, r;
cout<<"Enter the number: ";
cin>>a;
for(i=0; a>=9; i++){
r=a%10;
a=(a/10);
s=s+r;
}
cout<<"The sum of digits is: "<<s+a;
getch();
}



4. Program to simplify a rational number to fractions(e.g. 6/8 to 3/4)

#include<iostream.h>
void fract(int a, int b){
int min=a;
if(b<min){
min=b;
}
for(int n=1; n<=min; n++){
if((a % n == 0) && (b % n == 0)){
a=a/n;
b=b/n;
min=min/n;
n=1;

}
}
cout<<"The required fraction is "<<a<<"/"<<b;
}

void main()
{
int x, y;
cout<<"Enter the numerator:";
cin>>x;
cout<<"Enter the denominator:";
cin>>y;
fract(x, y);
}



5. Pogram to find whether a string is a palindrome or not

#include <stdio.h>
//#include <conio.h>
#include <process.h>
void main(){
char input[20];
int length=0;
printf("Enter the word for palindrome test:\n");
gets(input);
//Find the length of entered string
while(input[length]!='\0'){
length++;
}
//Now check for palindrome status
for (int i=0; i<=length--; i++){
if (input[i]!=input[length]){
printf("Entered word isn't a palindrome.");
exit(0);
}
}
printf("Entered word is a palindrome.");
// getch();
}


6. Program to print all prime numbers upto the number input by the user

#include <iostream.h>
#include <math.h>
int main(){
int i=0, n, j=0, flag=0, mid;
cout<<"Enter the number upto where you want prime numbers to be displayed:\n";
cin>>n;
cout<<"Following are the required prime numbers:\n2\n3\n";
for(i=2;i<=n;i++){
mid = floor(sqrt(i));
for(j=2;j<=mid;j++){
if(i%j==0){
flag=0;
break;
}
else{
flag=1;
}
}
if(flag==1){
cout<<i<<"\n";
}
}
return (0);
}

7. Your own strlen function. Program to find the length of a user input string

#include <iostream.h>
int i;
int strlen(char *input){
for(i=0; input[i]!='\0'; i++){
}
return i;
}
int main(){
char *data;
cin>>data;
cout<<strlen(data);
return (0);
}


8. Your own strrev function. Program to reverse a user input string

#include <iostream.h>
#include <stdio.h>
int main(){
char *name;
cout<<"Enter the string:";
gets(name);
int count=0;
for(int i=0; name[i]!='\0'; i++){
count++;
}
for (int i=count; i>=0; i--){
cout<<name[i];
}
return (0);
}

9. Program to reverse a number

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
int y=1,revs=0;
cout<<"No daalo";
cin>>a;
while(y>0)
{
b=a%10;
a=a/10;
revs=revs*10+b;
y=a;
}
cout<<revs;
getch();
}


10. Demonstrate a practical use of switch case

#include<iostream.h>
#include<conio.h>
int main()
{
float r,a;
char n;
cout<<"To find area of circle press C and for sphere press S:";
cin>>n;
switch(n)
{
case 'c':
{
cout<<"Enter the radius of the circle: ";
cin>>r;
a=3.1414*r*r;
cout<<"Area of the circle is " <<a;
break;
}
case 's':
{
cout<<"Enter the radius of the sphere: ";
cin>>r;
a=4*3.1414*r*r;
cout<<"Area of the sphere is " <<a;
break;
}
default:
cout<<"invalid entry!!!!!!!";
}
getch();
return(0);
}



11. Program to compute the union of 2 or more sets

#include <iostream.h>
int b;
int howmany(){
cout<<"How many elements you want to enter in this set";
cin>>b;
return b;
}
void main(){
int a, c[100], i=0, j=0, k=0;
cout<<"How many sets do you want to enter?";
cin>>a;
if (a<2){
cout<<"Blah blah! Enter at least two sets moron, for computation of union!";
}

for(int m=k;k<a;m++){
j=j+howmany();

cout<<"Enter the elements of this set one by one\n";
for(int n=i;i<j;n++)
{
cin>>c[i];
i++;
}
k++;
}

int temp=0;
for(a=0;a<i;a++){
for(b=a+1;b<i;b++){
if(c[a]==c[b]){
c[b]=0;
}

}
}
/////////////////////
cout<<"The union of all the sets above is:\n";
for (a=0;a<i;a++){
if (c[a]!=0){
cout<<c[a]<<"\n";
}
}
}



12. Program for matrix addition and multiplication using operator overloading and classes and ADT

#include <iostream.h>
int i, j, k, num;
class matrix{
int element[3][3];
public:
void display(){
for (int i=0;i<=2;i++){
for(int j=0; j<=2; j++){
cout<<element[i][j];
cout<<"\t";
}
cout<<"\n";
}
}
void getdata(){
cout<<"Enter the elements of matrix row wise:\n";
for (int i=0;i<=2;i++){
for(int j=0; j<=2; j++){
cin>>element[i][j];
}
cout<<"\n";
}
}

matrix operator +(matrix target){
matrix temp;
for(int i=0;i<=2;i++){
for(int j=0;j<=2;j++){
temp.element[i][j] = element[i][j]+target.element[i][j];
}
}
return temp;
}
matrix operator *(matrix target){
matrix temp;
int num=0;
for (i=0;i<=2;i++){
for(j=0;j<=2;j++){
for(k=0;k<=2;k++){
num = ((element[i][k])*(target.element[k][j]) + num);
}
temp.element[i][j] = num;
num=0;
}
}
return temp;
}
};
void main(){
matrix a, b,c ,d;
a.getdata();
b.getdata();
c.getdata();
d=a*b+c;
d.display();

}



13. Program to find the age difference between two dates using operator overloading and classes and ADT (Note that, calculating age difference is not as simple as normal subtraction other 3 part data like KM,M and CM trio)

#include <iostream.h>
#include <process.h>
class date{
int day;
int month;
int year;
bool leap;
public:
void getdata(void){
cout<<"Enter year(eg. 1988): \n";
cin>>year;
if((year%100)==0){
if((year%400)==0){
leap=true;
}
}
else if((year%4)==0){
leap=true;
}
else leap=false;
inmonth:
cout<<"Enter month(eg. 11): \n";
cin>>month;
if((month>12) || (month<1)){
cout<<"Invalid month entered. Please re-enter:\n";
goto inmonth;
}
inday:
cout<<"Enter day(eg. 25):\n";
cin>>day;
if((day>31) || (day<1)){
cout<<"Invalid day entered. Please re-enter:\n";
goto inday;
}
else if(((4==month)||(6==month)||(9==month)||(11==month))&&(day>30)){
cout<<"This month can't have more than 30 days. Please re-enter:\n";
goto inday;
}
else if((2==month)&&(leap==true)&&(day>29)){
cout<<"February can't have more than 29 days in a leap year. Please re-enter:\n";
goto inday;
}
else if((2==month)&&(leap==false)&&(day>28)){
cout<<"February can't have more than 28 days in a non-leap year. Please re-enter:\n";
goto inday;
}
}
void disp(){
cout<<year<<" years "<<month<<" months "<<day<<" days\n";
}
date operator-(date target){
int last_day_diff;
date result;
if(target.year>year){
cout<<"Please enter a higher date to subtract.";
exit(0);
}
if(target.day<=day){
result.day=day-target.day;
}
else if(target.day>day){
switch (month){
case 4:
case 6:
case 9:
case 11:
last_day_diff=30-target.day;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
last_day_diff=31-target.day;
break;
}
if ((true==leap)&&(2==month)){
last_day_diff=29-target.day;
}
else if((false==leap)&&(2==month)){
last_day_diff=28-target.day;
}
result.day=last_day_diff+day;
month--;
}
if(target.month<=month){
result.month=month-target.month;
}
else if(target.month>=month){
result.month=12-target.month+month;
year--;
}
result.year=year-target.year;
return result;
}
};


int main() {
date a, b, result;
cout<<"Enter higher date:\n";
a.getdata();
cout<<"Enter lower date:\n";
b.getdata();
result=a-b;
result.disp();
return 0;
}



14. Program to add and multiply two complex numbers using class, operator overloading, ADT

#include <iostream.h>

int abs(int input){
if (input>=0){
return input;
}
else{
return (-1*input);
}
}

class complex{
public:
int number[2]; //array to store real and imaginary part
complex(){ //constructor to intialize to zero
number[0]=0;
number[1]=0;
}

void display(){
if (number[1]>0){
cout<<number[0]<<"+"<<number[1]<<"i";
}
if (number[1]<0){
cout<<number[0]<<"-"<<abs(number[1])<<"i";
}
cout<<"\nReal part is: "<<number[0]<<" and imaginary part is: "<<number[1];

}

void getdata(){
cout<<"Enter real part:";
cin>>number[0];
cout<<"Enter imaginary part:";
cin>>number[1];

}

complex operator +(complex target){
complex temp;
temp.number[0] = number[0]+target.number[0];
temp.number[1] = number[1]+target.number[1];
return temp;
}

complex operator *(complex target){
complex temp;
temp.number[0] = (number[0]*target.number[0])-(number[1]*target.number[1]);
temp.number[1] = (number[0]*target.number[1])+(number[1]*target.number[0]);
return temp;
}

};
void main(){
complex a, b, c ,d;
a.getdata();
b.getdata();
c.getdata();
d=a*b+c;
d.display();

}



15. Program to print all ascii printable characters in a file instead of screen

#include <iostream.h>
#include <fstream.h>
int main(){
char i;
char path[255];
cout<<"Enter path:\n";
cin>>path;
ofstream file(path);
for(i=48; i<=122; i++){
file<<i<<"\n";
}
cout<<"Data written. Exiting";

return (0);
}

No comments: