PDA

View Full Version : Post ur C/C++ Programs Here


Pages : [1] 2

Gigacore
02-09-2007, 04:34 PM
Hi guys n gals,

If you are a good at C/C++ Programming or if you are a programmer or just know this language then post you Programs here. By this way it helps learners a lot. Members can post their programs and get suggestions if there is anything wrong it......

Use this Pattern:

A Program to find the largest of three numbers using nexted if

#include <stdio.h>
main()
{
int a,b,c,big;
printf("Enter Three numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(a>c)
big=a;
else
big=c;
else
if(b>c)
big=b;
else
big=c;
printf("Largest of %d,%d and %d = %d",a,b,c,big);
return 0;
}

==================================================
List of all the C Programs posted in this thread..
==================================================

● A Program to find the largest of three numbers using nexted if. (http://www.thinkdigit.com/forum/showpost.php?p=592920&postcount=11)
● A program to findcalc area, surface area, volume, total surface area etc. of numerous 2D and 3D figures using functions. (http://www.thinkdigit.com/forum/showpost.php?p=592791&postcount=9)
● A Program to perform basic calculations. (http://www.thinkdigit.com/forum/showpost.php?p=592754&postcount=3)
● Program To Reverse a Number (http://www.thinkdigit.com/forum/showpost.php?p=592989&postcount=21)
● What is this? (http://www.thinkdigit.com/forum/showpost.php?p=593594&postcount=42)
● A Program that Binary, Decimal, Octal Inter-Conversions (http://www.thinkdigit.com/forum/showpost.php?p=596801&postcount=47)
● A bulls and cows game (http://www.thinkdigit.com/forum/showpost.php?p=596844&postcount=48)
● A program that selects a number which is of 4 digits and all are unique numbers. (http://www.thinkdigit.com/forum/showpost.php?p=597483&postcount=57)
● Write a C Program to sort numbers of an Array using Bubble Sort Method. (http://www.thinkdigit.com/forum/showpost.php?p=600004&postcount=70)
● Reusable java class to send email through pop3 (http://www.thinkdigit.com/forum/showpost.php?p=600349&postcount=79)
● Simple Switch-Case Example. (http://www.thinkdigit.com/forum/showpost.php?p=600609&postcount=87)
● Swapping of two numbers without a Temporary variable in 1 line. (http://www.thinkdigit.com/forum/showpost.php?p=600134&postcount=75)
● A program that creates a vB list out of a file given in a particular format. (http://www.thinkdigit.com/forum/showpost.php?p=600702&postcount=92)
● Digit Forum URL Bulleted List Generator (http://www.thinkdigit.com/forum/showpost.php?p=600721&postcount=93)
● Write a C program to find an element from an Array using Binary Search Method. (http://www.thinkdigit.com/forum/showpost.php?p=600793&postcount=100)
● TOWER OF HANOI Simulator (http://www.thinkdigit.com/forum/showpost.php?p=603727&postcount=127)
● A Simple C Program to read and print a one-dimensional array (http://www.thinkdigit.com/forum/showpost.php?p=604496&postcount=136)
● C Program to find the largest element in an array and the position of its occurrence. (http://www.thinkdigit.com/forum/showpost.php?p=605115&postcount=139)
● A C program to find the maximum and minimum elements in an array having N elements (http://www.thinkdigit.com/forum/showpost.php?p=605531&postcount=144)
● Program which compares two strings, concatenate them and then gives the length of string. (http://www.thinkdigit.com/forum/showpost.php?p=607880&postcount=164)
● Program to kill a software /demo version expire in C (http://www.thinkdigit.com/forum/showpost.php?p=608341&postcount=170)
● Program To arrange numbers in Pyramid Pattern (http://www.thinkdigit.com/forum/showpost.php?p=609383&postcount=184)
● Program: a simple shop using string, integers, float, & algebraic operators (http://www.thinkdigit.com/forum/showpost.php?p=610678&postcount=187)
● Character Eater (http://www.thinkdigit.com/forum/showpost.php?p=616479&postcount=220)
● Program to insert a node in a single linked list. (http://www.thinkdigit.com/forum/showpost.php?p=617643&postcount=247)
● A C Program to convert decimal number to its Binary Number Equivalent (http://www.thinkdigit.com/forum/showpost.php?p=618560&postcount=262)
● Introduction to C++ - Stanford Video Tutorials and Other Lectures (http://www.thinkdigit.com/forum/showpost.php?p=622122&postcount=299) (Not a program)
● A Program to remind that u r using other account (http://www.thinkdigit.com/forum/showpost.php?p=623390&postcount=306)
● Typedef Example Program (http://www.thinkdigit.com/forum/showpost.php?p=624837&postcount=312)
● A C Program to check whether a given word is Palindrome or not. (http://www.thinkdigit.com/forum/showpost.php?p=645292&postcount=361)
● A Simple C Program to Display the number and its square from 1 to 10 using register variable. (http://www.thinkdigit.com/forum/showpost.php?p=646765&postcount=367)
● A C Program to find the sum of two matrix using two dimensional array.
(http://www.thinkdigit.com/forum/showpost.php?p=652794&postcount=375)
================================================== =======

Last Update: 06 November 2007

xbonez
02-09-2007, 04:47 PM
here's a prog to calc area, surface area, volume, total surface area etc. of numerous 2D and 3D figures using functions

i had made this one for a friend - it was his project


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

void line(void);
void cuboid(void);
void cube(void);
void cylinder(void);
void cone(void);
void sphere(void);
void hemisphere(void);
void triangle(void);
void rectangle(void);
void square(void);
void circle(void);
void parallelogram(void);

void main()
{
clrscr();
int o1,o2;

menu:
do{
clrscr();
cout<<"\n\n1. 3-D Figures"
<<"\n\n2. Plane Figures"
<<"\n\n3. Exit"
<<"\n\nEnter your choice -->\t";
cin>>o1;

switch(o1)
{
case 1: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\t3-D Figures\n\n";
line();
cout<<"\n\n1. Cuboid"
<<"\n\n2. Cube"
<<"\n\n3. Cylinder"
<<"\n\n4. Cone"
<<"\n\n5. Sphere"
<<"\n\n6. Hemisphere"
<<"\n\n7. Main Menu"
<<"\n\nEnter your choice -->\t";
cin>>o2;

switch(o2)
{
case 1: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCuboid\n\n";
line();
cuboid();
getch();
break;

case 2: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCube\n\n";
line();
cube();
getch();
break;

case 3: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCylinder\n\n";
line();
cylinder();
getch();
break;

case 4: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCone\n\n";
line();
cone();
getch();
break;

case 5: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tSphere\n\n";
line();
sphere();
getch();
break;

case 6: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tHemisphere\n\n";
line();
hemisphere();
getch();
break;

case 7: goto menu;


default: cout<<"\n\nInvalid Input\a\a\a";
goto menu;
}
getch();
break;

case 2: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tPlane Figures\n\n";
line();
cout<<"\n\n1. Traingle"
<<"\n\n2. Rectangle"
<<"\n\n3. Sqaure"
<<"\n\n4. Circle"
<<"\n\n5. Parallelogram"
<<"\n\n6. Main Menu"
<<"\n\nEnter your choice -->\t";
cin>>o2;

switch(o2)
{
case 1: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tTraingle\n\n";
line();
triangle();
getch();
break;

case 2: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tRectangle\n\n";
line();
rectangle();
getch();
break;

case 3: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tSquare\n\n";
line();
square();
getch();
break;

case 4: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCircle\n\n";
line();
circle();
getch();
break;

case 5: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tParallelogram\n\n";
line();
parallelogram();
getch();
break;

case 6: goto menu;


default: cout<<"\n\nInvalid Input\a\a\a";
goto menu;
}

getch();
break;

case 3: cout<<"\n\nPress any key to exit...\a";
getch();
break;

default: cout<<"\n\nInvalid Input\a\a\a\a";
goto menu;



}

}while(o1!=3);

}


void line()
{
int i;
for (i=0;i<80;i++)
cout<<"*";
}

void cuboid()
{
float l,b,h,a,v;
cout<<"\n\nEnter length of cuboid -->\t";
cin>>l;
cout<<"\n\nEnter breadth of cuboid -->\t";
cin>>b;
cout<<"\n\nEnter height of cuboid -->\t";
cin>>h;
v=l*b*h;
a=2*(l*b+b*h+l*h);
cout<<"\n\nVolume of cuboid is "<<v;
cout<<"\n\nSurface area of cuboid is "<<a;
}

void cube()
{
float l,a,v;
cout<<"\n\nEnter length of side of cube -->\t";
cin>>l;
v=l*l*l;
a=6*l*l;
cout<<"\n\nVolume of cube is "<<v;
cout<<"\n\nSurface area of cube is "<<a;
}

void cylinder()
{
float r,h,v,c,t;
cout<<"\n\nEnter radius of cylinder -->\t";
cin>>r;
cout<<"\n\nEnter height of cylinder -->\t";
cin>>h;
v=3.14*r*r*h;
c=2*3.14*r*h;
t=3.14*r*(2*h+r);
cout<<"\n\nVolume of cylinder is "<<v;
cout<<"\n\nCurved Surface Area of cylinder is "<<c;
cout<<"\n\nTotal Surface Area of cylinder is "<<t;
}

void cone()
{
float r,h,l,v,c,t;
cout<<"\n\nEnter radius of cone -->\t";
cin>>r;
cout<<"\n\nEnter height of cone -->\t";
cin>>h;
l=sqrt(r*r+h*h);
v=(3.14*r*r*h)/3;
c=3.14*r*l;
t=3.14*r*(l+r);
cout<<"\n\nSlant height of cone is "<<l;
cout<<"\n\nVolume of cone is "<<v;
cout<<"\n\nCurved Surface Area of cone is "<<c;
cout<<"\n\nTotal Surface Area of cone is "<<t;
}

void sphere()
{
float r,v,a;
cout<<"\n\nEnter radius of sphere -->\t";
cin>>r;
v=(4*3.14*r*r*r)/3;
a=4+3.14*r*r;
cout<<"\n\nVolume of sphere is "<<v;
cout<<"\n\nSurface Area of sphere is "<<a;
}

void hemisphere()
{
float r,v,c,t;
cout<<"\n\nEnter radius of hemisphere -->\t";
cin>>r;
v=(2*3.14*r*r*r)/3;
c=2*3.14*r*r;
t=3*3.14*r*r;
cout<<"\n\nVolume of hemisphere is "<<v;
cout<<"\n\nCurved Surface Area of hemisphere is "<<c;
cout<<"\n\nTotal Surface Area of hemisphere is "<<t;
}

void triangle()
{
float b,h,a;

cout<<"\n\nEnter base of triangle -->\t";
cin>>b;
cout<<"\n\nEnter height of triangle -->\t";
cin>>h;

a=(b*h)/2;
cout<<"\n\nArea of the triangle is "<<a;

}

void rectangle()
{
float l,b,a,p;
cout<<"\n\nEnter length of rectangle -->\t";
cin>>l;
cout<<"\n\nEnter breadth of rectangle -->\t";
cin>>b;
a=l*b;
p=2*(l+b);
cout<<"\n\nArea of rectangle is "<<a;
cout<<"\n\nPerimeter of rectangle is "<<p;
}

void square()
{
float s,a,p;
cout<<"\n\nEnter length of side of square -->\t";
cin>>s;
a=s*s;
p=4*s;
cout<<"\n\nArea of square is "<<a;
cout<<"\n\nPerimeter of square is "<<p;
}

void circle()
{
float r,a,c;
cout<<"\n\nEnter radius of circle -->\t";
cin>>r;
a=3.14*r*r;
c=2*3.14*r;
cout<<"\n\nArea of circle is "<<a;
cout<<"\n\nCircumference of circle is "<<c;
}

void parallelogram()
{
float b,h,a,p;
cout<<"\n\nEnter base of parallelogram -->\t";
cin>>b;
cout<<"\n\nEnter height of parallelogram -->\t";
cin>>h;
a=b*h;
p=2*(b+h);
cout<<"\n\nArea of parallelogram is "<<a;
cout<<"\n\nPerimeter of parallelogram is "<<p;
}


i've put up a lot more progs at my forum. classmates really find it useful since none of them is interested in computers. its so sad...

max_demon
02-09-2007, 04:49 PM
My Program to calculate Addition

#include <stdlib.h>
main()
{
float a,b,c,total,multiply,add,input;
printf("Welcome to Demon Calculator \n\n");
printf("Enter the values-\n\n");
printf("a:");
scanf("%f",&a);
printf("b:");
scanf("%f",&b);
printf("c:");
scanf("%f",&c);

total=a*b*c;
if (total>80000)
printf("Meri Marzi , tu khud solve kar.\a\a\a\a\a\a\a\a\a\n\n");
else
printf("The answer is %f\n\n\a\a",total);
system("pause");
}

Ron
02-09-2007, 04:52 PM
scanf("%d%d%d",&a,&b,&c);
New to c++.......Whta those this line means

Yamaraj
02-09-2007, 04:58 PM
#include <stdio.h>
main()
{
int a,b,c,big;
printf("Enter Three numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(a>c)
big=a;
else
big=c;
else
if(b>c)
big=b;
else
big=c;
printf("Largest of %d,%d and %d = %d",a,b,c,big);
}
Your program lacks proper braces and a return statement.

here's a prog to calc area, surface area, volume, total surface area etc. of numerous 2D and 3D figures using functions

i had made this one for a friend - it was his project

...


i've put up a lot more progs at my forum. classmates really find it useful since none of them is interested in computers. its so sad...
Your code doesn't conform to either ISO C or C++ standards.

scanf("%d%d%d",&a,&b,&c);
New to c++.......Whta those this line means
scanf() is a C input function, which is considered unsafe except for formatted data. I advise learning C first, before opting for a much larger, bloated and complex language as C++.

fun2sh
02-09-2007, 04:59 PM
+no the program is correct dude

Gigacore
02-09-2007, 05:07 PM
i added the return statement, is it rite now?

Pathik
02-09-2007, 05:11 PM
scanf("%d%d%d",&a,&b,&c);
New to c++.......Whta those this line means
y r u using scanf in c++???

QwertyManiac
02-09-2007, 05:30 PM
Your code doesn't conform to either ISO C or C++ standards.

Don't be so hard on him :D He doesn't know TC++'s evils :D

Anyway, here's xbonez's program in proper code :)

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

using namespace std;

void line(void);
void cuboid(void);
void cube(void);
void cylinder(void);
void cone(void);
void sphere(void);
void hemisphere(void);
void triangle(void);
void rectangle(void);
void square(void);
void circle(void);
void parallelogram(void);

int main()
{
int o1,o2;
do{

cout<<"\n\n1. 3-D Figures"
<<"\n\n2. Plane Figures"
<<"\n\n3. Exit"
<<"\n\nEnter your choice -->\t";
cin>>o1;

switch(o1)
{
case 1: cout<<"\n\n";
line();
cout<<"\n\t\t\t\t3-D Figures\n\n";
line();
cout<<"\n\n1. Cuboid"
<<"\n\n2. Cube"
<<"\n\n3. Cylinder"
<<"\n\n4. Cone"
<<"\n\n5. Sphere"
<<"\n\n6. Hemisphere"
<<"\n\n7. Main Menu"
<<"\n\nEnter your choice -->\t";
cin>>o2;
switch(o2)
{
case 1: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCuboid\n\n";
line();
cuboid();

break;

case 2: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCube\n\n";
line();
cube();

break;

case 3: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCylinder\n\n";
line();
cylinder();

break;

case 4: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCone\n\n";
line();
cone();
break;

case 5: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tSphere\n\n";
line();
sphere();
break;

case 6: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tHemisphere\n\n";
line();
hemisphere();
break;

case 7: break;


default: cout<<"\n\nInvalid Input\a\a\a";
}

break;

case 2:
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tPlane Figures\n\n";
line();
cout<<"\n\n1. Traingle"
<<"\n\n2. Rectangle"
<<"\n\n3. Sqaure"
<<"\n\n4. Circle"
<<"\n\n5. Parallelogram"
<<"\n\n6. Main Menu"
<<"\n\nEnter your choice -->\t";
cin>>o2;

switch(o2)
{
case 1: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tTraingle\n\n";
line();
triangle();

break;

case 2: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tRectangle\n\n";
line();
rectangle();

break;

case 3: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tSquare\n\n";
line();
square();

break;

case 4: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCircle\n\n";
line();
circle();
break;

case 5: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tParallelogram\n\n";
line();
parallelogram();
break;

case 6: break;


default: cout<<"\n\nInvalid Input\a\a\a";
}

break;

case 3: cout<<"\n\nPress any key to exit...\a";
break;

default: cout<<"\n\nInvalid Input\a\a\a\a";



}

}while(o1!=3);
return 0;
}


void line()
{
int i;
for (i=0;i<80;i++)
cout<<"*";
}

void cuboid()
{
float l,b,h,a,v;
cout<<"\n\nEnter length of cuboid -->\t";
cin>>l;
cout<<"\n\nEnter breadth of cuboid -->\t";
cin>>b;
cout<<"\n\nEnter height of cuboid -->\t";
cin>>h;
v=l*b*h;
a=2*(l*b+b*h+l*h);
cout<<"\n\nVolume of cuboid is "<<v;
cout<<"\n\nSurface area of cuboid is "<<a;
}

void cube()
{
float l,a,v;
cout<<"\n\nEnter length of side of cube -->\t";
cin>>l;
v=l*l*l;
a=6*l*l;
cout<<"\n\nVolume of cube is "<<v;
cout<<"\n\nSurface area of cube is "<<a;
}

void cylinder()
{
float r,h,v,c,t;
cout<<"\n\nEnter radius of cylinder -->\t";
cin>>r;
cout<<"\n\nEnter height of cylinder -->\t";
cin>>h;
v=3.14*r*r*h;
c=2*3.14*r*h;
t=3.14*r*(2*h+r);
cout<<"\n\nVolume of cylinder is "<<v;
cout<<"\n\nCurved Surface Area of cylinder is "<<c;
cout<<"\n\nTotal Surface Area of cylinder is "<<t;
}

void cone()
{
float r,h,l,v,c,t;
cout<<"\n\nEnter radius of cone -->\t";
cin>>r;
cout<<"\n\nEnter height of cone -->\t";
cin>>h;
l=sqrt(r*r+h*h);
v=(3.14*r*r*h)/3;
c=3.14*r*l;
t=3.14*r*(l+r);
cout<<"\n\nSlant height of cone is "<<l;
cout<<"\n\nVolume of cone is "<<v;
cout<<"\n\nCurved Surface Area of cone is "<<c;
cout<<"\n\nTotal Surface Area of cone is "<<t;
}

void sphere()
{
float r,v,a;
cout<<"\n\nEnter radius of sphere -->\t";
cin>>r;
v=(4*3.14*r*r*r)/3;
a=4+3.14*r*r;
cout<<"\n\nVolume of sphere is "<<v;
cout<<"\n\nSurface Area of sphere is "<<a;
}

void hemisphere()
{
float r,v,c,t;
cout<<"\n\nEnter radius of hemisphere -->\t";
cin>>r;
v=(2*3.14*r*r*r)/3;
c=2*3.14*r*r;
t=3*3.14*r*r;
cout<<"\n\nVolume of hemisphere is "<<v;
cout<<"\n\nCurved Surface Area of hemisphere is "<<c;
cout<<"\n\nTotal Surface Area of hemisphere is "<<t;
}

void triangle()
{
float b,h,a;

cout<<"\n\nEnter base of triangle -->\t";
cin>>b;
cout<<"\n\nEnter height of triangle -->\t";
cin>>h;

a=(b*h)/2;
cout<<"\n\nArea of the triangle is "<<a;

}

void rectangle()
{
float l,b,a,p;
cout<<"\n\nEnter length of rectangle -->\t";
cin>>l;
cout<<"\n\nEnter breadth of rectangle -->\t";
cin>>b;
a=l*b;
p=2*(l+b);
cout<<"\n\nArea of rectangle is "<<a;
cout<<"\n\nPerimeter of rectangle is "<<p;
}

void square()
{
float s,a,p;
cout<<"\n\nEnter length of side of square -->\t";
cin>>s;
a=s*s;
p=4*s;
cout<<"\n\nArea of square is "<<a;
cout<<"\n\nPerimeter of square is "<<p;
}

void circle()
{
float r,a,c;
cout<<"\n\nEnter radius of circle -->\t";
cin>>r;
a=3.14*r*r;
c=2*3.14*r;
cout<<"\n\nArea of circle is "<<a;
cout<<"\n\nCircumference of circle is "<<c;
}

void parallelogram()
{
float b,h,a,p;
cout<<"\n\nEnter base of parallelogram -->\t";
cin>>b;
cout<<"\n\nEnter height of parallelogram -->\t";
cin>>h;
a=b*h;
p=2*(b+h);
cout<<"\n\nArea of parallelogram is "<<a;
cout<<"\n\nPerimeter of parallelogram is "<<p;
}

Gigacore
02-09-2007, 05:46 PM
Guys... is my program rite now?

Yamaraj
02-09-2007, 07:35 PM
Guys... is my program rite now?
Try this:

#include <stdio.h>

int main(void)
{
int a = 0,
b = 0,
c = 0,
greatest = 0;

printf("Enter three numbers:\n");
scanf("%d %d %d", &a, &b, &c);

greatest = a;
if(b > greatest)
greatest = b;
if(c > greatest)
greatest = c;

printf("greatest of %d, %d and %d = %d", a, b, c, greatest);
return 0;
}

Gigacore
02-09-2007, 07:42 PM
^ thanks buddy......

utsav
02-09-2007, 07:43 PM
this is a program in which u will hav 2 enter any 5 nos between 1 to 49 and if the number matches with the numbers chosen by the computer, u will win the lottery




#include <cstdlib>
#include <iostream>
using namespace std;
void lotto();
void calc(int * a, int * b);
void menu();
// Main function. Seeds random function
// and calls menu function.
int main()


{
srand((unsigned)time(0)); // Seed the random function......
menu();
}
// This function prompts the user to pic
// k five numbers between 1 and 49.
// The function makes sure that the user
// picks numbers between 1 and 49,
// and that the user does not choose dup
// licate numbers. Then the computer
// chooses five random numbers. Error co
// ntrol is used to check if the
// computer chooses the same number twic
// e. Then the numbers are displayed
// to the screen. Finally the calc funct
// ion is called to calculate and
// output the results of the game...
void lotto()


{
int i, k, j, b, x, y;// variables for all the for loops....
int s, t, user_error;
int user[5], comp[5];// arrays for user and computers numbers....
int user_error_check, comp_error_check; // to find if numbers are duplicates....
// Prompt user to choose numbers
cout << "Please choose five numbers from 0 to 49" << endl;
for (i=0; i<5; i++)


{
cout << "Enter number " << i + 1 << ":" ;
cin >> user[i];
if (user[i] > 49 || user[i] < 0)


{
cout << "You did not enter a number from 0 to 49" << endl;
i--;
}

}
// ERROR CONTROL, to make sure that the
// user does not choose the
// same number twice.................
for(s=0; s<5; s++)
for(t=0; t<s; t++)


{
user_error_check = user[s];
if(user_error_check == user[t])


{
cout << "You have chosen duplicate numbers, please choose another number: " << endl;
cin >> user_error;
user[s] = user_error;
}
}

// Now the computer chooses numbers.....
// ........
for (k=0; k<5; k++)


{
comp[k] = (rand( ) % 49) + 1; // get comp to randomly generate 1 -49
}
// ERROR CONTROL, to make sure that the
// computer does not pick
// the same number twice.......

for(x=0; x<5; x++)
for(y=0; y<x; y++)


{
comp_error_check = comp[x];
if (comp_error_check == comp[y])


{
comp[x] = (rand() % 49) + 1;
}
} // no guarentee that the computer will not choose another number
// already in the array, but it's the be
// st i can do?????

cout << "You chose: " ;// output users numbers...
for (j=0; j<5; j++)


{
cout << user[j] << ", " ;
}

cout << endl;

cout << "The computer chose: "; // output computers numbers...
for (b=0; b<5; b++)


{
cout << comp[b] << ", " ;
}
cout << endl << endl;

calc(user, comp);// Obtain the results of the game......
}
// This function compares the computers
// random numbers to the numbers
// chosen by the user and lets the user
// know how many (if any) they
// have matched..............
void calc(int * a, int * b)


{
int i, k, match =0;
for (i=0; i<5; i++)
for (k=0; k<5; k++)
if ( a[i] == b[k] )


{
match += 1;
cout << "You have matched: " << a[i] << endl;
}
if (match == 0)


{
cout << "Sorry you have not matched any of the computers numbers..."
<< endl << endl;
}
if (match == 5)


{
cout << "CONGRATULATIONS, YOU HAVE WON THE LOTTERY!!!!!!!" << endl << endl;
}
}
// This function informs the user how to
// play the game and then allows
// the user to play the game until they
// enter 'q' or 'Q' at the prompt.
void menu()


{
char choice;
cout << endl;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$" << endl;
cout << "$(C)$" << endl;
cout << "$ $" << endl;
cout << "$ *The computer will randomly choose five numbers $" << endl;
cout << "$ between 1 and 49.$" << endl;
cout << "$ $" << endl;
cout << "$ *You can choose five numbers and try to match$" << endl;
cout << "$ the computers numbers...... $" << endl;
cout << "$ $" << endl;
cout << "$ **Do not pick the same number twice or pick a$" << endl;
cout << "$number less than 1 or greater than 49, o.k.?$" << endl;
cout << "$ $" << endl;
cout << "$ $" << endl;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$" << endl;
cout << endl;
while (choice != 'q' || choice != 'Q')


{

cout << "Make a selection from the menu: " << endl;
cout << "[P]- Play the clotto against the computer." << endl;
cout << "[Q]- Exit this program." << endl;
cin >> choice;
switch(choice)


{
case 'p':
case 'P':
lotto();
break;
case 'q':
case 'Q':
cout << "No lotto for you!" << endl;
exit(0);
break;
}
}
}

ratedrsuperstar
02-09-2007, 07:47 PM
^^this is totally wrong what if b>c>gr8test there's no condition to test that

use this thread to request programs and then pm to the requester rather than posting so much code over here it really looks a mess and these are quite basic too

Gigacore
02-09-2007, 07:49 PM
@ utsav... did it execute buddy?

Yamaraj
02-09-2007, 07:52 PM
^^this is totally wrong what if b>c>gr8test there's no condition to test that
Rules of programming:
1. Read the fscking code. And mine is pretty readable.
2. Test it before complaining.
3. If you still don't get it, it's time to buy a decent C book.

xbonez
02-09-2007, 07:56 PM
@yamraj : what was wrong with my prog?? :( it seemed to run perfectly fine

Yamaraj
02-09-2007, 08:00 PM
@yamraj : what was wrong with my prog?? :( it seemed to run perfectly fine
Using implementation dependent libraries and functions like clrscr(), and the nonstandard "void main()" violates the rules of the ISO C++ standard. Your executable may run fine on a system, but your code needs more love.

I recommend a decent and recent C++ book, like C++: How To Program, C++ Primer, 4th/e or C++ Primer Plus.

utsav
02-09-2007, 08:32 PM
@ gigacore it executed nicely on devc++. tell me the problm with my program

Sykora
02-09-2007, 08:35 PM
And for heaven's sake indent the code like the first post. It's unreadable otherwise.

Gigacore
02-09-2007, 08:40 PM
Program To Reverse a Number

#include <stdio.h>
main()
{
long rev, n, num;
int digit;
printf("\n Enter a number");
scanf("%d", &num);
rev=0;
n=num;
while (num>0)
{
digit = num % 10;
num = num / 10
rev = rev * 10 + digit;
}
printf("\n The number is %d", n);
printf("\n The reverse is %d", rev);
}

mehulved
02-09-2007, 08:49 PM
And how about using comments in the code so people like me could understand it faster and better.

utsav
02-09-2007, 08:52 PM
^^ i hav used comments in my program

Gigacore
02-09-2007, 08:52 PM
use /* before the coment

shady_inc
02-09-2007, 08:58 PM
use /* before the coment

....and */ after the comment.

can someone tell me what's wrong in this program.Using Dev C++

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{ /*Use of Function*/
float a,b,sum;
float calsum(float a,float b);
cout<<"Enter the two nos.:\n";
cin>>a>>b;
sum=calsum(a,b);
cout<<"The sum is :"<<sum<<"";
}
calsum(float a,float b)
{ float sum;
sum=a+b;
return (sum);
}

ilugd
02-09-2007, 09:03 PM
lol... why doesn't thinkdigit have a programming section?

xbonez
02-09-2007, 09:04 PM
Using implementation dependent libraries and functions like clrscr(), and the nonstandard "void main()" violates the rules of the ISO C++ standard. Your executable may run fine on a system, but your code needs more love.

I recommend a decent and recent C++ book, like C++: How To Program, C++ Primer, 4th/e or C++ Primer Plus.

hmm, u use Oxford and Sumita Arora's textbook for C++. they're our course books in XI and XII

shady_inc
02-09-2007, 09:10 PM
hmm, u use Oxford and Sumita Arora's textbook for C++. they're our course books in XI and XII

I find books by Yashavant Kanetkar the best ones for C/C++.

Sykora
02-09-2007, 09:11 PM
@shady_inc :
conio.h is non-standard.
Your calsum() doesn't have a forward declaration. Put main() after calsum() or put the line :

int calsum(float, float);

before main().

Gigacore
02-09-2007, 09:27 PM
does any know how to write this one?

Write a program using functions to find whether the given number is a prime number?

ilugd
02-09-2007, 09:33 PM
do an iteration from 2 to int(n/2) and if it isn't divisible then it is prime.

Yamaraj
02-09-2007, 09:35 PM
hmm, u use Oxford and Sumita Arora's textbook for C++.
Who told you that? I recommend what I use - Lafore's, Prata's, Lippman's and Stroustrup's.

Gigacore
02-09-2007, 09:36 PM
thanks ilugd i'll try it

xbonez
02-09-2007, 09:37 PM
sorry, i meant to say I use those books...typo http://83.149.99.14/forums/style_emoticons/DarksideRG/blush2.gif

QwertyManiac
02-09-2007, 11:10 PM
....and */ after the comment.

can someone tell me what's wrong in this program.Using Dev C++
Either Sykora's solution or simply add a float to your function definition line:

#include <iostream>
using namespace std;
int main()
{
/*Use of Function*/
float a,b,sum;
float calsum(float a,float b);

cout<<"Enter the two nos.:\n";
cin>>a>>b;
sum=calsum(a,b);
cout<<"The sum is :"<<sum<<"";

return 0;
}

float calsum(float a,float b)
{
float sum;

sum=a+b;

return (sum);
}

Nav11aug
03-09-2007, 03:20 AM
do an iteration from 2 to int(n/2) and if it isn't divisible then it is prime.

bad math funda :D .. iterate from 2 to (int)sqrt(n) ..if it isnt divisible then itz prime

Who told you that? I recommend what I use - Lafore's, Prata's, Lippman's and Stroustrup's.

How to Program in C++ by Deitel and Deitel is good too


I find books by Yashavant Kanetkar the best ones for C/C++.

And as far as Kanetkar is concerned ... when I was inmy 11/12th I thought so too. But now, :D u jst mature. Those books feel like being fed with a spoon.

They do contain a LOT of funda though

ilugd
03-09-2007, 04:22 AM
i got 33% in my math exams during my bachelor's degree. Don't know how i lost it. Got 98% in +2 though. Come to think of it, I was socializing, eating and drinking the whole 3 years in BCA (if you know what I mean). :-D

Nav11aug
03-09-2007, 04:37 AM
lol yeah... socializing => lookin fr bandis??

QwertyManiac
03-09-2007, 09:00 AM
Here's a simple Stack program I wrote and documented a bit in C long time ago. Surprisingly found it in my programs directory ..

/* Stack Implementation using Linked Lists */

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

/* Structure Declaration:
Structure for a Stack (Linked List Structure)
Requires two elements.

One Element member, holding the value that the stack is to be made of
One Self-Referring Pointer member for making it a Linked List - Stack. */

typedef struct stacklink

{
int element;
struct stacklink *next;
} top ;

typedef top stack;

/* Create:
Initializes the stack with a NULL value. */

void create(stack *s)
{
s=NULL;
}

/* Push:
Pushes (Inserts) a node into the Stack, using a new temporary node.
Stack's Push operation is done only on the top of the stack */

stack *push(stack *s)
{
int a=0;
stack *temp;

printf("\nEnter the element to push into the stack: ");
scanf("%d",&a);

temp = (stack *)malloc(sizeof(stack));

temp->element = a;
temp->next = s;

return temp;
}

/* Pop:
Pops (Deletes) the top-most element out of the Stack.
Also checks if the stack is empty or not and pops only when there is an element available. */

stack *pop(stack *s)

{
stack *temp;

if(s->next!=NULL)
{
temp = s;
printf("\nPopped element is: %d\n",temp->element);
s = s->next;
free(temp);
}

else
{
printf("\nNothing to Pop from the stack.\n");
}

return s;
}

/* Display:
Displays the Stack in a pictorial fashion.
A sample output of the stack via this would look like:
The List is:
4 -> 5 -> 6 -> NULL
Where, NULL represents the END of stack. For an empty stack too, it shows NULL. */

void display(stack *s)

{
int temp=1;

printf("\nThe Stack is:");

if(s->next==NULL)
{
printf("\b empty");
}

while(s->next!=NULL)
{
if(temp)
{
printf("\n");
temp=0;
}

printf("%d --> ",s->element);
s=s->next;

if(s->next==NULL)
{
printf("NULL");
break;
}
}

printf("\n");
}

/* Main Menu:
Displays a menu-based access system for various operations on the stack.
The menu looks like:
1. Create
2. Push
3. Pop
4. Display
5. Exit
Each item in the menu calls the appropriate functions of the Stack. */

int main()

{
int choice, key, loc,throw=0;
stack *temp;

while(1)
{
printf("\nStacks\n-------\n");
printf("1. Create\n2. Push\n3. Pop\n4. Display\n5. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1:

temp=(stack *)malloc(sizeof(stack));

create(temp);

printf("\nNULL Stack created\n");
throw=1;

break;

case 2:
if(throw==1)
{
temp=push(temp);

display(temp);
}

else
{
printf("\nCreate a stack first ..\n");
}

break;

case 3:

if(throw==1)
{
temp=pop(temp);

display(temp);
}

else
{
printf("\nCreate a stack first ..\n");
}

break;

case 4:

if(throw==1)
display(temp);

else
printf("\nCreate the stack first ..\n");

break;

case 5:

return 0;

default:

printf("\nInvalid choice ...\n");

}
}
}

Gigacore
03-09-2007, 09:23 AM
^^ cool, working :)

swap_too_fast
03-09-2007, 12:34 PM
Here's a simple Stack program I wrote and documented a bit in C long time ago. Surprisingly found it in my programs directory ..

/* Stack Implementation using Linked Lists */

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

/* Structure Declaration:
Structure for a Stack (Linked List Structure)
Requires two elements.

One Element member, holding the value that the stack is to be made of
One Self-Referring Pointer member for making it a Linked List - Stack. */

typedef struct stacklink

{
int element;
struct stacklink *next;
} top ;

typedef top stack;

/* Create:
Initializes the stack with a NULL value. */

void create(stack *s)
{
s=NULL;
}

/* Push:
Pushes (Inserts) a node into the Stack, using a new temporary node.
Stack's Push operation is done only on the top of the stack */

stack *push(stack *s)
{
int a=0;
stack *temp;

printf("\nEnter the element to push into the stack: ");
scanf("%d",&a);

temp = (stack *)malloc(sizeof(stack));

temp->element = a;
temp->next = s;

return temp;
}

/* Pop:
Pops (Deletes) the top-most element out of the Stack.
Also checks if the stack is empty or not and pops only when there is an element available. */

stack *pop(stack *s)

{
stack *temp;

if(s->next!=NULL)
{
temp = s;
printf("\nPopped element is: %d\n",temp->element);
s = s->next;
free(temp);
}

else
{
printf("\nNothing to Pop from the stack.\n");
}

return s;
}

/* Display:
Displays the Stack in a pictorial fashion.
A sample output of the stack via this would look like:
The List is:
4 -> 5 -> 6 -> NULL
Where, NULL represents the END of stack. For an empty stack too, it shows NULL. */

void display(stack *s)

{
int temp=1;

printf("\nThe Stack is:");

if(s->next==NULL)
{
printf("\b empty");
}

while(s->next!=NULL)
{
if(temp)
{
printf("\n");
temp=0;
}

printf("%d --> ",s->element);
s=s->next;

if(s->next==NULL)
{
printf("NULL");
break;
}
}

printf("\n");
}

/* Main Menu:
Displays a menu-based access system for various operations on the stack.
The menu looks like:
1. Create
2. Push
3. Pop
4. Display
5. Exit
Each item in the menu calls the appropriate functions of the Stack. */

int main()

{
int choice, key, loc,throw=0;
stack *temp;

while(1)
{
printf("\nStacks\n-------\n");
printf("1. Create\n2. Push\n3. Pop\n4. Display\n5. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1:

temp=(stack *)malloc(sizeof(stack));

create(temp);

printf("\nNULL Stack created\n");
throw=1;

break;

case 2:
if(throw==1)
{
temp=push(temp);

display(temp);
}

else
{
printf("\nCreate a stack first ..\n");
}

break;

case 3:

if(throw==1)
{
temp=pop(temp);

display(temp);
}

else
{
printf("\nCreate a stack first ..\n");
}

break;

case 4:

if(throw==1)
display(temp);

else
printf("\nCreate the stack first ..\n");

break;

case 5:

return 0;

default:

printf("\nInvalid choice ...\n");

}
}
}

PLz tell me what about this program and what it will do, i know stack but plz explain

I have new idea..!!!!

What about some challenges any one can raised problem, before 2 days my teacher challenge me to do a program to convert given number into binary , octal and hexadecimal. I have found solution but you have to try.

for exercise:

do a program like U have to hide the typed string on screen by" * "

like we do password: **********

nice one haa..

QwertyManiac
03-09-2007, 02:08 PM
Stack is a simple Last-In First-Out data structure .. Its like this:

http://upload.wikimedia.org/wikipedia/commons/2/29/Data_stack.svg|20|
|30|
|40|
----

To add an element to it, we can only push from top and not insert anywhere we like:

So after pushing 10 into the stack, it looks like:

|10| <-- (Pushed on top)
|20|
|30|
|40|
----

Similarly, as its a LIFO system, Popping the stack removes the top most element.

Thus, 10 would be removed on popping:

|20| --> |10| (Popped out and deleted)
|30|
|40|
----

Stack's best application would be the function call. Recursion takes place in stacks format. (And are thus usually avoided)

Read more (http://en.wikipedia.org/wiki/Stack_%28data_structure%29)

Zeeshan Quireshi
03-09-2007, 04:56 PM
hmm, u use Oxford and Sumita Arora's textbook for C++. they're our course books in XI and XII
Mate i suggest you re-learn C++ using Addison Wesley's Accelerated C++

One of the best Standard C++ books out there that actually teach you how to use STL practically rather than just theory.

btw , i read this in class XI(i'm currently in XII) so ditch your textbook(seriously) and use this .

king khan
06-09-2007, 04:41 PM
does any body had a c++ code to shut down the system

QwertyManiac
06-09-2007, 06:22 PM
Isn't that as simple as calling the DOS shutdown function via the system function?

Like system("shutdown /?"); for example.

Why would you wanna do it though ? I smell a prank :D

The_Devil_Himself
06-09-2007, 06:37 PM
I have new idea..!!!!

What about some challenges any one can raised problem, before 2 days my teacher challenge me to do a program to convert given number into binary , octal and hexadecimal. I have found solution but you have to try.


That is one helluva program which I made 2-3 months back but my program was not perfect as I used a lot of arrays which took up a lot of memory and more often than not they were never used.I seem to have lost my original program code but will write it and post it in a couple of days or so.

YOu can actually display the given no. in octal,hexadecimal,and binary by just using %o,%x,and(I forgot for binary) instead of %d while displaying.

QwertyManiac
06-09-2007, 06:59 PM
I don't have a program to do Oct, Hex, Bin, etc in C/C++ but here is my module in Python. It inter-converts between Strings-Binary-Hexadecimal-Octal-Decimals :)

#/usr/bin/env python

"Base-n tools with String support (Base-n not complete yet, sorry)"

"String to Binary [Returns a spaced out Binary value string]"
def strtobin(a):
b,c='',0
for each in a:
c = ord(each)
b+=dectobin(c)+' '
return b[:-1]

"String to Decimal [Returns a spaced out Decimal value string]"
def strtodec(a):
b=''
for each in a:
b+=str(ord(each))+' '
return b[:-1]

"String to Octal [Returns a spaced out Octal value string]"
def strtooct(a):
b=''
for each in a:
b+=dectooct(strtodec(each))+' '
return b[:-1]

"String to Hexadecimal [Returns a spaced out Hexadecimal value string]"
def strtohex(a):
b=''
l=strtodec(a).split(' ')
for each in l:
b += hex(int(each)) + ' '
return b[:-1]

"Hexadecimal to Binary [Returns a Binary value string]"
def hextobin(a):
return dectobin(hextodec(a))

"Hexadecimal to Decimal [Returns a Decimal value]"
def hextodec(a):
return int(a,16)

"Hexadecimal to Octal [Returns an Octal value string]"
def hextooct(a):
return oct(hextodec(a))

"Hexadecimal to String [Returns a string corresponding to each Decimal ASCII value of the Hexadecimal value]"
def hextostr(a):
b=""
l=[a.split(" ")]
for each in l[0]:
b+=chr(hextodec(each))
return b

"Octal to Binary [Returns an Octal value string]"
def octtobin(a):
return dectobin(octtodec(a))

"Octal to Decimal [Returns a Decimal value]"
def octtodec(a):
return str(int(str(a),8))

"Octal to Hexadecimal [Returns a Hexadecimal value string]"
def octtohex(a):
a = int(a,8)
return dectohex(octtodec(a))

"Octal to String [Returns a string based on the Decimal value of the Octal number]"
def octtostr(a):
return dectostr(octtodec(a))

"Decimal to Binary [Returns a Binary valued string]"
def dectobin(a):
a,b,c=int(a),'',0
while(a>0):
c = a%2
b+=str(c)
a=a/2
return b[::-1]

"Decimal to Octal [Returns a Decimal]"
def dectooct(a):
return oct(int(a))

"Decimal to Hexadecimal [Returns a Hexadecimal value string]"
def dectohex(a):
return hex(int(a))

"Decimal to String [Returns a string]"
def dectostr(a):
if int(a)<256:
return chr(int(a))
return None

"Binary to Decimal [Returns a Binary valued string]"
def bintodec(a):
c=i=0
a=a[::-1]
for each in a:
if each!=None:
c+=int(each)*pow(2,i)
i+=1
return c

"Binary to Octal [Returns an Octal value string]"
def bintooct(a):
return dectooct(bintodec(a))

"Binary to Hexadecimal [Returns a Binary valued string]"
def bintohex(a):
return dectohex(bintodec(a))

"Binary to String [Returns a string]"
def bintostr(a):
b=''
for each in a.split(' '):
if dectostr(bintodec(each))!=None:
b+=dectostr(bintodec(each))+' '
return b[:-1]

if __name__=='__main__':
print 'Try calling any function here'

aditya.shevade
06-09-2007, 07:37 PM
A bulls and cows game.I am still working on this. Trying to add some more functionality.

I know the variable names are quite big. But I think that makes it easier to understand the program without comments.

I am new to C++. So, if you can suggest any improvements, then please do so.

One more thing, from my experience, Indian authors tend to provide knowledge based on TurboC which sucks. So, anyone going for a C++ course, please get a book suggested above, and then be sure to read professional C++ by Solter and Kleper (Wrox international). It rocks.

Aditya

/* Project Name :- Bulls and Cows.
Project Version :- 2.1.0.
Project Author :- Aditya Shevade.
Time Started :- 5th of June 2007, 08:07 pm.
Time Finished :- 5th of June 2007, 09:21 pm.
Built Using :- Anjuta 1.2.4a.
*/

#include<iostream>
#include<cstdio>
#include<cstdlib>

using namespace std;

class BullsAndCows
{
private:

int ComputerNumber, UserNumber;
int ComputerReminderUnits, ComputerReminderTens, ComputerReminderHundreds, ComputerReminderThousands;
int UserReminderUnits, UserReminderTens, UserReminderHundreds, UserReminderThousands;
int PositionsCorrect, DigitsCorrect;

public:

void NumberInitialisation();
void InitialiseComputerValues();
void InitialiseUserValues();
void UserInput();
void Calculations();
void NoOfPositionsCorrect();
void NoOfDigitsCorrect();
void CheckWin();

int RandomNumber();

};

BullsAndCows x;

main()
{
cout << "\n\n\t\tWelcome to Bulls and Cows.";
cout << "\n\t\t\tVersion 2.1.0";
cout << "\n\n\t\tPress Enter to continue";

getchar();

x.NumberInitialisation();

return 0;

}

void BullsAndCows::NumberInitialisation()
{
do
{
srand(time(NULL));
ComputerNumber = (rand () % 10000);

}while (ComputerNumber > 10000 || ComputerNumber < 1000);

x.InitialiseComputerValues();

}

void BullsAndCows::InitialiseComputerValues()
{
ComputerReminderUnits = ComputerNumber % 10;
ComputerReminderTens = ((ComputerNumber - ComputerReminderUnits) / 10) % 10;
ComputerReminderHundreds = ((ComputerNumber - (ComputerNumber % 100)) / 100) % 10;
ComputerReminderThousands = ((ComputerNumber - ComputerNumber % 1000) / 1000);

if (ComputerReminderUnits == ComputerReminderTens || ComputerReminderUnits == ComputerReminderHundreds || ComputerReminderUnits == ComputerReminderThousands || ComputerReminderTens == ComputerReminderHundreds || ComputerReminderTens == ComputerReminderThousands || ComputerReminderHundreds == ComputerReminderThousands)
x.NumberInitialisation();

else
x.UserInput();

}

void BullsAndCows::UserInput()
{
cout << "\n\n\t\tPlease Enter Your Choise.\n\n\t\t\t";
cin >> UserNumber;

while (UserNumber > 10000 || UserNumber < 1000)
{
cout << "\n\n\t\tPlease Enter Valid Values.\n\n\t\t\t";
cin >> UserNumber;
}

x.InitialiseUserValues();
x.Calculations();

}

void BullsAndCows::InitialiseUserValues()
{
PositionsCorrect = 0, DigitsCorrect = 0;

UserReminderUnits = UserNumber % 10;
UserReminderTens = ((UserNumber - UserReminderUnits) / 10) % 10;
UserReminderHundreds = ((UserNumber - (UserNumber % 100)) / 100) % 10;
UserReminderThousands = ((UserNumber - UserNumber % 1000) / 1000);

}

void BullsAndCows::Calculations()
{
x.NoOfPositionsCorrect();
x.NoOfDigitsCorrect();

cout << "\n\n\t\tNumber of Digits Correct :-" << DigitsCorrect;
cout << "\n\n\t\tNumber of Positions Correct :-" << PositionsCorrect;

x.CheckWin();

}

void BullsAndCows::NoOfPositionsCorrect()
{
if(UserReminderUnits == ComputerReminderUnits)
PositionsCorrect++;
if(UserReminderTens == ComputerReminderTens)
PositionsCorrect++;
if(UserReminderHundreds == ComputerReminderHundreds)
PositionsCorrect++;
if(UserReminderThousands == ComputerReminderThousands)
PositionsCorrect++;

}

void BullsAndCows::NoOfDigitsCorrect()
{
if(ComputerReminderUnits == UserReminderUnits || ComputerReminderUnits == UserReminderTens || ComputerReminderUnits == UserReminderHundreds || ComputerReminderUnits == UserReminderThousands)
DigitsCorrect++;
if(ComputerReminderTens == UserReminderUnits || ComputerReminderTens == UserReminderTens || ComputerReminderTens == UserReminderHundreds || ComputerReminderTens == UserReminderThousands)
DigitsCorrect++;
if(ComputerReminderHundreds == UserReminderUnits || ComputerReminderHundreds == UserReminderTens || ComputerReminderHundreds == UserReminderHundreds || ComputerReminderHundreds == UserReminderThousands)
DigitsCorrect++;
if(ComputerReminderThousands == UserReminderUnits || ComputerReminderThousands == UserReminderTens || ComputerReminderThousands == UserReminderHundreds || ComputerReminderThousands == UserReminderThousands)
DigitsCorrect++;

}

void BullsAndCows::CheckWin()
{
if(PositionsCorrect == 4 && DigitsCorrect == 4)
{
char Answer;

cout << "\n\n\t\tCongeratulations, You won!";
cout << "\n\n\t\tDo you wish to play again?(y/n)";
cin >> Answer;

while (Answer != 'Y' && Answer != 'y' && Answer != 'N' && Answer != 'n')
{
cout << "\n\n\t\tDo you wish to play again?(y/n)";
cin >> Answer;
}

if (Answer == 'Y' || Answer == 'y')
x.NumberInitialisation();

if (Answer == 'N' || Answer == 'n')
{
cout << "\n\n\t\tThank You for playing this Game.";
exit(0);
}

}

else
x.UserInput();

}

The_Devil_Himself
06-09-2007, 07:45 PM
I don't know python but dude the program seems to just display the no. in corresponding number system.But here we were talking about actually converting the no. so that we can even use them in arithmetic operations.

The c equivalent of your program is(I am just writing the necessary part):

Printf("Enter the decimal no.:");
scanf("%d",&n);
printf("The equivalent octal no. is:%o\n\n The equivalent hexadecimal no. is %x",n,n);
getch();


The trick is just to replace %d by %o and %x while displaying.
I didn't check it but hope this works.

@qwerty please pardon me if I was unable to interpret your python program properly.BTW I am also planning to learn python very soon.So how is compared to C(I am good at c).

Sykora
06-09-2007, 08:23 PM
@qwerty : A few observations,
-- Use ''.join or (' '.join in your case) instead of string += something. It's faster.
For example,

def strtodec(a) :
return ' '.join((ord(i) for i in a))

and likewise.
-- Put your docstrings below the function def, not above them.
That's all I can get from the first few of them.

Why do you find converting ASCII to hex and octal necessary?

@The_Devil_Himself :
The internal representation of numbers in most languages is in decimal. In C/C++ you can perform arithmetic in octal and hex by prefixing a 0 or 0x to an int declared variable. However, for other bases, you will have to define your own class and overload your own operators to make it work as seamlessly as you'd like.

Python is a much more elegant language than C. It is a dynamically typed language, and heavily object oriented. It also has one of the largest standard libraries. It pays for all this in a severe lack of speed on most number crunching applications.

QwertyManiac
06-09-2007, 09:07 PM
@qwerty : A few observations,
-- Use ''.join or (' '.join in your case) instead of string += something. It's faster.
For example,

def strtodec(a) :
return ' '.join((ord(i) for i in a))
and likewise.
-- Put your docstrings below the function def, not above them.
That's all I can get from the first few of them.

Why do you find converting ASCII to hex and octal necessary?

Oh ok, I've just started off actually, am not so strong in it. I'll keep that point in mind. And about the usefulness, I don't know, I just wrote something huge .. practising stuff.

Sykora
06-09-2007, 09:20 PM
And about the usefulness, I don't know, I just wrote something huge .. practising stuff.
Oh, that's ok then. :D

xbonez
06-09-2007, 09:43 PM
What about some challenges any one can raised problem, before 2 days my teacher challenge me to do a program to convert given number into binary , octal and hexadecimal. I have found solution but you have to try.



here's the prog. had created it for my class XI practicals


#include <iostream.h>
#include <conio.h>
#include <math.h>
int BtoD(long int n);
int BtoO(long int n);
long int DtoB(long int n);
int DtoO(long int n);
long int OtoB(long int n);
int OtoD(long int n);
void main()
{
clrscr();
int ch;
long int n;
cout<<"\n1.Binary to Decimal";
cout<<"\n2.Binary to Octal";
cout<<"\n3.Decimal to Binary";
cout<<"\n4.Decimal to Octal";
cout<<"\n5.Octal to Binary";
cout<<"\n6.Octal to Decimal";
cout<<"\nEnter the choice --> ";
cin>>ch;

cout<<"\nEnter the number --> ";
cin>>n;

switch (ch)
{
case 1:
cout<<"\nDecimal = ";
cout<<BtoD(n);
break;
case 2:
cout<<"\nOctal = ";
cout<<BtoO(n);
break;
case 3:
cout<<"\nBinary = ";
cout<<DtoB(n);
break;
case 4:
cout<<"\nOctal = ";
cout<<DtoO(n);
break;
case 5:
cout<<"\nBinary = ";
cout<<OtoB(n);
break;
case 6:
cout<<"\nDecimal = ";
cout<<OtoD(n);
break;
default:
cout<<"You have entered the wrong choice";
}
getch();
}

int BtoD(long int n)
{
int deci=0;
int b,p=0;

while (n>0)
{
b=n%10;
n=n/10;
deci=deci+b*pow(2,p++);
}
return deci;
}

int BtoO(long int n)
{
int deci=0,oct=0;
int b,c,p=0,r=0;

while (n>0)
{
b=n%10;
n=n/10;
deci=deci+b*pow(2,p++);
}

while (deci>0)
{
c=deci%8;
deci=deci/8;
oct=oct+c*pow(10,r++);
}
return oct;
}

long int DtoB(long int n)
{
long int bin=0;
int b,p=0;

while (n>0)
{
b=n%2;
n=n/2;
bin=bin+b*pow(10,p++);
}
return bin;
}

int DtoO(long int n)
{
int oct=0;
int b,p=0;

while (n>0)
{
b=n%8;
n=n/8;
oct=oct+b*pow(10,p++);
}
return oct;
}



long int OtoB(long int n)
{
long int bin=0;
int deci=0,b,c,p=0,r=0;

while (n>0)
{
b=n%10;
n=n/10;
deci=deci+b*pow(8,p++);
}

while (deci>0)
{
c=deci%2;
deci=deci/2;
bin=bin+c*pow(10,r++);
}
return bin;
}

int OtoD(long int n)
{
int deci=0;
int b,p=0;

while (n>0)
{
b=n%10;
n=n/10;
deci=deci+b*pow(8,p++);
}
return deci;
}

Desi-Tek.com
06-09-2007, 09:50 PM
My java programs
license (desi! download it, use it, change it sale it no restriction)
Mini web server
http://thakur.dheeraj.googlepages.com/MiniWebServer.zip
with source code and binary.

check ur gmail email
http://thakur.dheeraj.googlepages.com/TriggerMail.zip

send email through gmail smtp in jsp
http://desi-tek.org/blog/2007/09/01/jsp-code-to-send-email-through-google-gmail-smtp.html

reusable postgresql database connection class

/**
*
*/
package ocricket.bean;

import java.sql.Connection;
import java.sql.DriverManager;

import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
* @author Dheeraj
*
*/
public class Postgre_Connection {
public Connection conn = null;

public Postgre_Connection() {
try {
System.setProperty("file.encoding", "ISO8859-1");
//InitialContext ic = new InitialContext();
String host = "localhost"; // server ip
String port = "5432"; // port
String database = "ocricket"; // database name
String user = "dheeraj"; // database user
String pass = "123456"; // database password
start("org.postgresql.Driver", "jdbc:postgresql://" + host + ":" // postgresql database connection class
+ port + "/" + database + "?autoReconnect=true", user, pass); // Context-Params
} catch (NamingException e) {
e.printStackTrace();
}

}

public void start(String dbdriver, String dburl, String name,
String password) {
try {
Class.forName(dbdriver).newInstance();
conn = DriverManager.getConnection(dburl, name, password);
}

catch (Exception e) {
e.printStackTrace();
System.err.println(" Error: " + e);
}

}
}



how to reuse it?

Postgre_Connection post = new Postgre_Connection();
PreparedStatement ps = post.conn.prepareStatement("your sql querie");
ResultSet rs = ps.executeQuery();
you can use it with any database by doing small modification :)

praka123
06-09-2007, 10:22 PM
what abt licenses of all these codes ;) :D :)) GPL? Open Source License with credit given to Original authors.I'd love to post,but what am weak in Math :(

shady_inc
06-09-2007, 10:35 PM
A bulls and cows game.I am still working on this. Trying to add some more functionality.

I know the variable names are quite big. But I think that makes it easier to understand the program without comments.

I am new to C++. So, if you can suggest any improvements, then please do so.

One more thing, from my experience, Indian authors tend to provide knowledge based on TurboC which sucks. So, anyone going for a C++ course, please get a book suggested above, and then be sure to read professional C++ by Solter and Kleper (Wrox international). It rocks.

Aditya

/* Project Name :- Bulls and Cows.
Project Version :- 2.1.0.
Project Author :- Aditya Shevade.
Time Started :- 5th of June 2007, 08:07 pm.
Time Finished :- 5th of June 2007, 09:21 pm.
Built Using :- Anjuta 1.2.4a.
*/

#include<iostream>
#include<cstdio>
#include<cstdlib>

using namespace std;

class BullsAndCows
{
private:

int ComputerNumber, UserNumber;
int ComputerReminderUnits, ComputerReminderTens, ComputerReminderHundreds, ComputerReminderThousands;
int UserReminderUnits, UserReminderTens, UserReminderHundreds, UserReminderThousands;
int PositionsCorrect, DigitsCorrect;

public:

void NumberInitialisation();
void InitialiseComputerValues();
void InitialiseUserValues();
void UserInput();
void Calculations();
void NoOfPositionsCorrect();
void NoOfDigitsCorrect();
void CheckWin();

int RandomNumber();

};

BullsAndCows x;

main()
{
cout << "\n\n\t\tWelcome to Bulls and Cows.";
cout << "\n\t\t\tVersion 2.1.0";
cout << "\n\n\t\tPress Enter to continue";

getchar();

x.NumberInitialisation();

return 0;

}

void BullsAndCows::NumberInitialisation()
{
do
{
srand(time(NULL));
ComputerNumber = (rand () % 10000);

}while (ComputerNumber > 10000 || ComputerNumber < 1000);

x.InitialiseComputerValues();

}

void BullsAndCows::InitialiseComputerValues()
{
ComputerReminderUnits = ComputerNumber % 10;
ComputerReminderTens = ((ComputerNumber - ComputerReminderUnits) / 10) % 10;
ComputerReminderHundreds = ((ComputerNumber - (ComputerNumber % 100)) / 100) % 10;
ComputerReminderThousands = ((ComputerNumber - ComputerNumber % 1000) / 1000);

if (ComputerReminderUnits == ComputerReminderTens || ComputerReminderUnits == ComputerReminderHundreds || ComputerReminderUnits == ComputerReminderThousands || ComputerReminderTens == ComputerReminderHundreds || ComputerReminderTens == ComputerReminderThousands || ComputerReminderHundreds == ComputerReminderThousands)
x.NumberInitialisation();

else
x.UserInput();

}

void BullsAndCows::UserInput()
{
cout << "\n\n\t\tPlease Enter Your Choise.\n\n\t\t\t";
cin >> UserNumber;

while (UserNumber > 10000 || UserNumber < 1000)
{
cout << "\n\n\t\tPlease Enter Valid Values.\n\n\t\t\t";
cin >> UserNumber;
}

x.InitialiseUserValues();
x.Calculations();

}

void BullsAndCows::InitialiseUserValues()
{
PositionsCorrect = 0, DigitsCorrect = 0;

UserReminderUnits = UserNumber % 10;
UserReminderTens = ((UserNumber - UserReminderUnits) / 10) % 10;
UserReminderHundreds = ((UserNumber - (UserNumber % 100)) / 100) % 10;
UserReminderThousands = ((UserNumber - UserNumber % 1000) / 1000);

}

void BullsAndCows::Calculations()
{
x.NoOfPositionsCorrect();
x.NoOfDigitsCorrect();

cout << "\n\n\t\tNumber of Digits Correct :-" << DigitsCorrect;
cout << "\n\n\t\tNumber of Positions Correct :-" << PositionsCorrect;

x.CheckWin();

}

void BullsAndCows::NoOfPositionsCorrect()
{
if(UserReminderUnits == ComputerReminderUnits)
PositionsCorrect++;
if(UserReminderTens == ComputerReminderTens)
PositionsCorrect++;
if(UserReminderHundreds == ComputerReminderHundreds)
PositionsCorrect++;
if(UserReminderThousands == ComputerReminderThousands)
PositionsCorrect++;

}

void BullsAndCows::NoOfDigitsCorrect()
{
if(ComputerReminderUnits == UserReminderUnits || ComputerReminderUnits == UserReminderTens || ComputerReminderUnits == UserReminderHundreds || ComputerReminderUnits == UserReminderThousands)
DigitsCorrect++;
if(ComputerReminderTens == UserReminderUnits || ComputerReminderTens == UserReminderTens || ComputerReminderTens == UserReminderHundreds || ComputerReminderTens == UserReminderThousands)
DigitsCorrect++;
if(ComputerReminderHundreds == UserReminderUnits || ComputerReminderHundreds == UserReminderTens || ComputerReminderHundreds == UserReminderHundreds || ComputerReminderHundreds == UserReminderThousands)
DigitsCorrect++;
if(ComputerReminderThousands == UserReminderUnits || ComputerReminderThousands == UserReminderTens || ComputerReminderThousands == UserReminderHundreds || ComputerReminderThousands == UserReminderThousands)
DigitsCorrect++;

}

void BullsAndCows::CheckWin()
{
if(PositionsCorrect == 4 && DigitsCorrect == 4)
{
char Answer;

cout << "\n\n\t\tCongeratulations, You won!";
cout << "\n\n\t\tDo you wish to play again?(y/n)";
cin >> Answer;

while (Answer != 'Y' && Answer != 'y' && Answer != 'N' && Answer != 'n')
{
cout << "\n\n\t\tDo you wish to play again?(y/n)";
cin >> Answer;
}

if (Answer == 'Y' || Answer == 'y')
x.NumberInitialisation();

if (Answer == 'N' || Answer == 'n')
{
cout << "\n\n\t\tThank You for playing this Game.";
exit(0);
}

}

else
x.UserInput();

}


How do i play this game.What do I do after pressing Enter the first time??

aditya.shevade
07-09-2007, 12:43 PM
^^It is a standard bulls and cows game. The computer selects a number. A 4 digit number. There are no repeated digits in the number. Each digit is unique.

here This is a single player game. The computer selects a 4 digit number at random and then you have to guess that number. Remember that each digit in the number selected by the computer will be unique. Then when you enter your choice, the computer will give an output which will be telling you the total number of digits and positions of those which are correct.

Suppose the computer has selected the number 2156, and you enter your guess as 1234. Here the numbers 1 and 2 are correct. The positions of those numbers are, however, not correct. So the output will be:-

2 Numbers Correct.

0 Positions Correct.

Now if you enter 2134 then the output will be:-

2 Numbers Correct.

2 Positions Correct.

Now if you enter 2156 as your number, then all four numbers as well as their positions are correct, so the output will be:-

The number Selected By the computer was 2156

Abhishek Dwivedi
08-09-2007, 02:45 PM
i've heard ders a program to curupt da HDD....its a 3 line code....and same program can make HDD workin again...does ne one know it??

The_Devil_Himself
08-09-2007, 03:57 PM
Abhishek nothing can corrupt your hard disk what can a program can maximally do is format your hard disk.

QwertyManiac
08-09-2007, 04:21 PM
You can write programs to corrupt drives, but I don't know of a 3 line method. It sounds ineffective and lazy. I've heard of virii damaging the firmware of your HDD rather than doing something to the HDD itself, that's clever. Though accessing the firmware is a big job!

Gigacore
09-09-2007, 12:15 PM
^ Ya its very difficult

mavihs
09-09-2007, 05:11 PM
can u guys give me a site where i can reffere about c++ & get some solution. & i got this a prob with a question i hav. if u guys can solve it or give a hint or anything.
here it is:-

WAP to display th following format on the screen.(using loop)

1
1 1 1
1 1 1 1 1
1 1 1 1 1 1 1



plzzzzzzz give the ans or atleast some hint or somthing. I got an exam on monday. i hav 2 prepare for it. i can't understand these question how 2 do. plzzzzz help! i've been trying 2 figure it out for a week. it may come for my exam. plzzzz help!!!!!!

Gigacore
09-09-2007, 05:29 PM
Try this:

#include <stdio.h>
main()
{
int row, col, t, i;
int tab=40;
for(row = 1; row <= 7; row++)
{
for (t = 1; t <= tab; t++) printf(" ");
/* number if blank spaces from left edge of the screen */
for(col = 1;col <= row; col++)
printf("%d ",row);
printf("\n");
tab--;
}
return 0;
}

For C++ tutors...: http://www.cprogramming.com/tutorial.html

Projjwal
09-09-2007, 09:47 PM
do u know wht's the output of following prog
:)) :))

#include<stdio.h>
#include<conio.h>
void main()
{
int c;
c=printf("abc");
printf("\n%d",c);
getch();
}
Don't run it. Just guess it's output . & why?

QwertyManiac
09-09-2007, 09:51 PM
Try this:

#include <stdio.h>
main()
{
int row, col, t, i;
int tab=40;
for(row = 1; row <= 7; row++)
{
for (t = 1; t <= tab; t++) printf(" ");
/* number if blank spaces from left edge of the screen */
for(col = 1;col <= row; col++)
printf("%d ",row);
printf("\n");
tab--;
}
return 0;
} He needed just 1s and in odd quantities only. :p Good write up though :)

ilugd
09-09-2007, 09:53 PM
what does printf return by the way? a return value or some pointer? It would take a minute to google that but I guess that would be cheating. :-)

QwertyManiac
09-09-2007, 09:59 PM
what does printf return by the way? a return value or some pointer? It would take a minute to google that but I guess that would be cheating.

I think it returns an int. I've seen int in its prototype for sure. But I got no clue what the output of Projjwal's program's gotta be.

Projjwal
09-09-2007, 10:14 PM
hint:





try it bro....u r partially correct ..It's the value which printf return..But wht is that value.? I give up the ans is
abc
3

Gigacore
09-09-2007, 10:19 PM
^ theres nothing in Hint

Quiz_Master
09-09-2007, 10:23 PM
Honestly Speaking this is going to be my fevorite thread ever on Digit forum...
BookMarked...


OK, let me post my Today's Homework here..hehe...

Write a [I]C Program to sort numbers of an Array using Bubble Sort Method.

#include <stdio.h>
#define MAX 10
void swap(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void bsort(int list[], int n)
{
int i,j;
for(i=0;i<(n-1);i++)
for(j=0;j<(n-(i+1));j++)
if(list[j] > list[j+1])
swap(&list[j],&list[j+1]);
}
void readlist(int list[],int n)
{
int i;
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
}

void printlist(int list[],int n)
{
int i;
printf("The elements of the list are: \n");
for(i=0;i<n;i++)
printf("%d\t",list[i]);
}

void main()
{
int list[MAX], n;
printf("Enter the number of elements in the list max = 10\n");
scanf("%d",&n);
readlist(list,n);
printf("The list before sorting is:\n");
printlist(list,n);
bsort(list,n);
printf("The list after sorting is:\n");
printlist(list,n);
}



^ theres nothing in Hint

There IS..
Just Highlight the post.

Projjwal
09-09-2007, 10:31 PM
yea just highlight my previous post or just press "ctrl+ A ".Actually printf is a function with return type integer .Just open stdio.h file from include directory frm there u will got that. And it returns the length of string which it print.so as a result
c=printf("abc")
means printf return value 3 Which saved in C integer variable thats all.

Gigacore
09-09-2007, 10:38 PM
Cool Projjwal, thanks Quiz Master :D

Projjwal
09-09-2007, 10:47 PM
So,everybody of the forum know swapping using 3rd variable & without 3rd variable .now write a swapping program in c without 3rd variable & de vital point is use one line function only .
means
b=a+b;
a=b-a;
b=b-a;
---------------de above program has 3 functional lines now write it in one functional line.

The_Devil_Himself
09-09-2007, 11:04 PM
^^eh..how do we do that?

aditya.shevade
10-09-2007, 12:47 AM
Easy. Check this out.

#include<iostream>

using namespace std;

main()
{
int a, b;

cout << "Enter A and B" << endl;
cout << "a = ";
cin >> a;
cout << endl << "b = ";
cin >> b;

a = ((b - a) + (b = a));

cout << endl << "a = " << a << endl;
cout << "b = " << b;

return 0;

}

EDIT : Output :-

EXECUTING:
/home/aditya/test
----------------------------------------------
Enter A and B
a = 12

b = 23

a = 23
b = 12
----------------------------------------------
Program exited successfully with errcode (0)
Press the Enter key to close this terminal ...

Nav11aug
10-09-2007, 03:27 AM
^^ better algo than the one given..

11 @aditya.shevade

Gigacore
10-09-2007, 10:14 AM
I'm using borland Turbo C++, whenever i execute a program, it runs and closes suddenly... it happens in Dev C++ also.. please help

Quiz_Master
10-09-2007, 10:35 AM
^^ You can press Alt+F5 to view results after the execution of program.

Desi-Tek.com
10-09-2007, 12:08 PM
reusable java class to send email through pop3

/*
* Mail.java
*
* Created on January 1, 2003, 1:58 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package ocricket.bean;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
*
* @author Dheeraj
*/
public class Mail {

public void sendMail(String toemail, String subject, String body)
throws MessagingException {
String host = "mail.ofindia.in";
String user = "ocricket@ofindia.in";
String pass = "86qx3@i48fg6";
// Create properties, get Session
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol.", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.", "true");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.socketFactory.fallback", "false");
Session mailSession = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(user));
InternetAddress[] address = { new InternetAddress(toemail) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(body);
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, pass);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

}

}

QwertyManiac
10-09-2007, 02:36 PM
I'm using borland Turbo C++, whenever i execute a program, it runs and closes suddenly... it happens in Dev C++ also.. please help

Dev C++ too? Doesn't the output remain in the console box? Anyway for TC++ you need to call getch(); just before the end of the main function ( Before } ) but I guess you'd definitely know that :confused:

You mean to say that you get the output but not the final result or does it end abruptly in middle (Segmentation fault?)

Nav11aug
10-09-2007, 02:49 PM
I think he means that he cant see the output, nt terminating in the middle of smthin. Program completes execution and closes window

Projjwal
10-09-2007, 03:38 PM
@aditya.shevade It's very cool ans.Gr8 job ...I was just looking for this ans.
Now do the same thing without using "()" .

use Bitwise Operators
--------------------------------------------------------------
#include<stdio.h>

#include<conio.h>
void main()
{
int a,b;
a=12;
b=25;
a=((b-a)+(b=a));
printf("\na:%d\nb:%d",a,b);
getch();
}

This is de C version of aditya.shevade run it.
-----------------------------------------------------------

Batistabomb
10-09-2007, 04:08 PM
does anybody had c++ code for converting text to speech

Quiz_Master
10-09-2007, 04:35 PM
^^^^ WHAT????
I dont think anyone here can do that.....

It will require a whole app to be coded not just a single C++ program.

Projjwal
10-09-2007, 04:44 PM
@Batistabomb i think it's not so easy . U have to develop a C++ program with directX .So u have to know directX programming in other language like VB.NET,C#.net DirectX programming r easier then C++.

Batistabomb
10-09-2007, 04:50 PM
dude what's the need for directx here i want code in windows environment through any API in c++,is any api's located for c++

quan chi
10-09-2007, 04:51 PM
just fooling around with a simple program.:D
#include<iostream.h>
#include<conio.h>
void main()
{getch();
int n;
cout<<"enter a no between 1-3."<<endl;
cin>>n;
switch(n)
{case 1:
cout<<"you won!!"<<endl;
break;
case2:
cout<<"you lost!!"<<endl;
break;
case3:
cout<<"theres a tie "<<endl;
break;
default<<"please follow the above instructions."
break;
}
}

Batistabomb
10-09-2007, 04:56 PM
guys i had a nice c++ turorial in a simple english if anyone needs i can send for your mails

QwertyManiac
10-09-2007, 05:08 PM
That swapping thing cost me 6 marks in an exam last year (In C). The teacher refused to believe it works, and when I showed it in the lab, she said its not the right way to do it :| People ..

@Batistabomb - Projjwal is right I guess, .Net would be the easiest way, using Microsoft's APIs. Anyway check these links, I got no idea how to use them though.

http://www.microsoft.com/speech/speech2007/speechdevarticle.mspx

Same in wiki's language:
http://en.wikipedia.org/wiki/Speech_Application_Programming_Interface

Nav11aug
10-09-2007, 05:11 PM
That swapping thing cost me 6 marks in an exam last year (In C). The teacher refused to believe it works, and when I showed it in the lab, she said its not the right way to do it :| People ..


nthn surprisin.. people(especially teachers) dnt appreciate the smart ways of doin things.. got many marks cut when in school fr using KeyB shrtcuts everywhere in Office :((

The_Devil_Himself
10-09-2007, 06:04 PM
I couldn't agree more.In many a cases students are way smarter than teachers.Today my Data structure was asking "hey how do we change resolution in linux(red hat)?" and I was laughing like hell.

Hey guys(and gals of course) don't you think this thread should be made a sticky?The starter please start a poll man--sticky or not sticky.

QwertyManiac
10-09-2007, 06:29 PM
Here's something useful with respect to this thread.

This program creates a vB list out of a file given in a particular format.

The format should be:

Title 1
URL 1
Title 2
URL 2

And the output file's content will be the vB code for the following:


List Title

:arrow: Title 1 (http://URL%201)
:arrow: Title 2 (http://URL%202)Would be helpful for those maintaining List threads. And it'd be good if Intel_Gigacore maintains it for this thread too.

Am uploading an example file along, which contains the list for this thread.

And here's the code. Briefly Documented.

Edit: Code removed as the tags inside were getting converted.
Code's attached as digit.txt.

Download digit.txt below to view code.
You can rename it to digit.cpp.
Sorry for the inconvenience
Note: You might find an extra LIST tag at the end of the file but that's cause of the editors. The editors place an extra line at EOF, so can't help it.
I can add a lot of BACKSPACE characters but that's just stupid. So till I find another workaround, this is the code.

title.txt is the input test file.
digitout.txt is the test output file.
digit.txt is the code file. Rename it to digit.cpp to get syntax highlighting.

digitout.txt when pasted here, will look like:

Programs

:arrow: Biggest of 3 Numbers. (http://www.thinkdigit.com/forum/showpost.php?p=592920&postcount=11)
:arrow: Area, Surface Area (SA), Volume, Total SA, etc. of Various 2D and 3D Figures. (http://www.thinkdigit.com/forum/showpost.php?p=592791&postcount=9)
:arrow: Simple Random Number Lottery. (http://www.thinkdigit.com/forum/showpost.php?p=592926&postcount=13)
:arrow: Reverse a Number. (http://www.thinkdigit.com/forum/showpost.php?p=592989&postcount=21)
:arrow: Simple Stack Implementation using Linked Lists. (http://www.thinkdigit.com/forum/showpost.php?p=593382&postcount=39)
:arrow: Simple Bulls and Cows game. (http://www.thinkdigit.com/forum/showpost.php?p=596844&postcount=48)
:arrow: Binary, Decimal, Octal Interconversion. (http://www.thinkdigit.com/forum/showpost.php?p=596990&postcount=53)
:arrow: Simple Incremental Pyramid. (http://www.thinkdigit.com/forum/showpost.php?p=599757&postcount=63)
:arrow: Sorting of Numbers using Bubble Sort. (http://www.thinkdigit.com/forum/showpost.php?p=600004&postcount=70)
:arrow: Swapping of two numbers without a Temporary variable in 1 line. (http://www.thinkdigit.com/forum/showpost.php?p=600134&postcount=75)
:arrow: Simple Switch-Case Example. (http://www.thinkdigit.com/forum/showpost.php?p=600609&postcount=87)Attachments below:

QwertyManiac
10-09-2007, 06:45 PM
And here's a sample run. Forgot to add it to the earlier post.


harsh@qwerty-workstation:~$ ./digit.out

Digit Forum URL Bulleted List Generator
-------------------------------------

Version 1.0

The Input file must be in the format of Title first and URL next.

Example:
Title1
http://URL 1
Title 2
http://URL 2

And so on..

Enter the input file's location and name to convert: title
Enter the output file's location and name to store into: digitout
Enter the list name: Programs
harsh@qwerty-workstation:~$


This'd hopefully work in Windows as well. As I haven't added any OS-only code. :)

aditya.shevade
10-09-2007, 06:48 PM
^^ better algo than the one given..

11 @aditya.shevade

Uhh... better than one given where? Please explain what do you mean.

@aditya.shevade It's very cool ans.Gr8 job ...I was just looking for this ans.
Now do the same thing without using "()" .

use Bitwise Operators
--------------------------------------------------------------
#include<stdio.h>

#include<conio.h>
void main()
{
int a,b;
a=12;
b=25;
a=((b-a)+(b=a));
printf("\na:%d\nb:%d",a,b);
getch();
}

This is de C version of aditya.shevade run it.
-----------------------------------------------------------

Alright... Will do that... Right now, gotta run for keyboard class (music).


This is de C version of aditya.shevade run it.

Oh.... I have a C version of myself now... Didn't know that one :-D

By the way. Had my C orals today. Mid Semester exams. Scored 23/25. Topped the batch of 20 :-D. Orals of remaining 3 batches are still to be held. :-D

Aditya

QwertyManiac
10-09-2007, 06:53 PM
Orals? :p What are we supposed to do in Orals? :?

Btw, swapping in Python is deadly easy :))

a,b=b,a

That's it, swapped. :))

Here's a running example:

harsh@qwerty-workstation:~$ python
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=25
>>> b=10
>>> a,b=b,a
>>> a
10
>>> b
25
>>> exit()
harsh@qwerty-workstation:~$

Nav11aug
10-09-2007, 07:21 PM
Uhh... better than one given where? Please explain what do you mean.

better than b=a+b;
a=b-a;
b=b-a;
---------------de above program has 3 functional lines now write it in one functional line.
the implementation did not use the line b=a+b.thz why i said "better"

Quiz_Master
10-09-2007, 07:25 PM
Hey QWERTY.. That program in Post #92 is awesome man...
ThankQ Very Much for that....

Nav11aug
10-09-2007, 07:38 PM
Hey guys(and gals of course) don't you think this thread should be made a sticky?The starter please start a poll man--sticky or not sticky.

Sticky now ..sweet :)

QwertyManiac
10-09-2007, 07:45 PM
It isn't sticky yet :?

I don't want this as sticky, cause no one would look at it then, as is the case with most stickies. I'd rather vote for a new Programming section :)

Quiz_Master
10-09-2007, 07:58 PM
This was today's homework.

Write a C program to find an element from an Array using Binary Search Method.


#include <stdio.h>
#define M 10

void bsearch(int list[],int n,int element)
{
int l,u,m, flag = 0;
l = 0;
u = n-1;
while(l <= u)
{
m = (l+u)/2;
if( list[m] == element)
{
printf("The element whose value is %d is present at position %d in list\n",element,m);
flag =1;
break;
}
else
if(list[m] < element)
l = m+1;
else
u = m-1;
}
if( flag == 0)
printf("The element whose value is %d is not present in the list\n",element);
}

void readlist(int list[],int n)
{
int i;
printf(&