Post your C/C++ Programs Here

Status
Not open for further replies.

Liverpool_fan

Sami Hyypiä, LFC legend
Re: Post ur C/C++ Programs Here

Old quotes but well...

Easy. Check this out.

Code:
#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 :-

Code:
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 ...
While this solution is impressive, got to say gcc -Wall would give a warning about "undefined behaviour" and that's due to the fact that a is being initialized at the same time and also used between sequence points.
Though it will work well in MOST compilers though. But C Programmers should avoid tricks such as these in real world.

Temptation. It's here :- a^=b^=a^=b ;)
This expression is undefined in C. This code will modify variable a twice between sequence points and hence it falls in undefined domains of behaviour in C. (Though it works well)
The correct method is:
Code:
a ^= b;
b ^= a;
a ^= b;

But that will work ONLY for integers.
 

visu

Right off the assembly line
Re: Post ur C/C++ Programs Here

Hello everyone this is a program for the game "FLAMES"-it tests the relationship between a boy and a girl(people think like that:wink:)

#include<stdio.h>
#include<ctype.h>
#include<string.h>
int commonnum (char s1[20],char s2[20]);
int main()
{
int sp=0,i,m=6,x,j=0,q;
char s[]={'f','l','a','m','e','s'},fate,change_fate,name1[20],name2[20];
printf("\n%s",s);
start:
printf("\nenter a names");
scanf("%s",name1);
printf("\nenter second name");
scanf("%s",name2);

x=commonnum(name1,name2);
if (x==0)
{
printf("\naaaa no cheating!!!!");
goto start;
}
printf("\n%d",x);
y:
for(i=0;i<m;i++)
{if(s!='!')
{
if(j==x-1)
{
s='!';
j=-1;
sp++;
}
j++;
}

if(i==m-1)
goto y;
if(sp==5)
{
for(q=0;s[q]!='\0';q++)
if(s[q]!='!')
{printf("\n%c",fate=toupper(s[q]));

switch(fate)
{case 'F':printf("\nFRIEND! not bad can try later;");break;
case 'L':printf("\nLOVER!!! u r the luckiest being on earth");break;
case 'A':printf("AFFECTION! end up u r life making difference b/w love and affection");break;
case 'M':printf("\nMARRIAGE!!! a famous saying \"wife is a knife that cuts u r life\"");break;
case 'S':printf("SISTER!!!! i pity you ;)");break;
case 'E':printf("ENEMIES lets wage a war!!!!!!!!!");
}}
break;}
}
getchar();
printf ("Wanna change u r love ?\n");

change_fate=getchar();
if(change_fate=='y')
goto start;
return 0;}
int commonnum(char name1[],char name2[])
{int i,j,count=0;
char temps[20];

if(strlen(name2)>strlen(name1))
{ strcpy(temps,name1);
strcpy(name1,name2);
strcpy(name2,temps);
}
for(i=0;name2!='\0';i++)
for(j=0;name1[j]!='\0';j++)
if(name2==name1[j])


count++;
return strlen(name1)+strlen(name2)-2*count;

}

enjoy it and any comments are welcome:-o
 

deb.sasmal

Right off the assembly line
Re: Post ur C/C++ Programs Here

Circle Drawing :

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void circlePlotpoints(int xcenter,int ycenter,int x,int y)

{
putpixel(xcenter+x,ycenter+y , 10 );
putpixel(xcenter-x,ycenter+y , 10 );
putpixel(xcenter+x,ycenter-y , 10 );
putpixel(xcenter-x,ycenter-y , 4 );
putpixel(xcenter+y,ycenter+x , 5 );
putpixel(xcenter-y,ycenter+x , 6 );
putpixel(xcenter+y,ycenter-x , 7 );
putpixel(xcenter-y,ycenter-x , 8 );

}
void circleMidpoint(int xcenter,int ycenter,int radius)
{
int x=0;
int y=radius;
int p=1-radius;
void circlemidpoint(int,int,int,int);
/* Plot first set of points */
circlePlotpoints(xcenter,ycenter,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;

}
circlePlotpoints(xcenter,ycenter,x,y);

}

}
void main()
{
int d=DETECT,c;
initgraph(&d,&c,"");
circleMidpoint(300,300,100);
getch();
}
 

boeing_737

Broken In
Re: Post ur C/C++ Programs Here

Hi guys, this program isn't so clear. I have written comments to explain. Any help would be appreciated!

Code:
#include <stdio.h>

#define PRODUCT(x) (x*x)

int main()
{
	int j,k,i=3;

	j = PRODUCT(i++); // returns i value, 
                                // then increments ie i=3 is returned,
                                // which is sent to PRODUCT to give j = 9. 
                               //Then, i is incremented to 4

	k = PRODUCT(++i); // here, i is first incremented, 
                                 //so i = 5 now, which is sent to
				  // PRODUCT to give 25.
	printf("\nj = %d, k = %d",j,k); // j=9, k=25... INCORRECT ANS!
				// j=9, k=49 is the correct answer on execution.
	return 0;
}
Thanks!
-yogesh
 

Liverpool_fan

Sami Hyypiä, LFC legend
Re: Post ur C/C++ Programs Here

Hi guys, this program isn't so clear. I have written comments to explain. Any help would be appreciated!

Code:
#include <stdio.h>

#define PRODUCT(x) (x*x)

int main()
{
	int j,k,i=3;

	j = PRODUCT(i++); // returns i value, 
                                // then increments ie i=3 is returned,
                                // which is sent to PRODUCT to give j = 9. 
                               //Then, i is incremented to 4

	k = PRODUCT(++i); // here, i is first incremented, 
                                 //so i = 5 now, which is sent to
				  // PRODUCT to give 25.
	printf("\nj = %d, k = %d",j,k); // j=9, k=25... INCORRECT ANS!
				// j=9, k=49 is the correct answer on execution.
	return 0;
}
Thanks!
-yogesh
PRODUCT(i++) is replaced as i++ * i++, where you get the correct answer, increments i later due to postfix but first multiplies, so 9 as you got it. (3*3)
However i is incremented TWICE, so i value becomes 5.
Again PRODUCT(++i) is replaced as ++i * ++i, now prefix operation is performed before multiplication, so before multiplication i is incremented twice that is to 7, and 7 * 7 = 49.

Another thing is that this program will give warnings with -Wall flag in gcc because twice incrementing withing sequence points is an "undefined behaviour" in C.
 

adatapost

Broken In
Re: Post ur C/C++ Programs Here

My Program to calculate Addition

Code:
#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");
}

Hi,

Why you add system("pause")? Are programs like Excel, Word, IE etc pauses after we click on close button?

I think it is bad practice to learn and teach c programming. Sometimes people uses getch() in place of system(). It should be stop.

Thanks,
adatapost
 

adatapost

Broken In
Re: Post ur C/C++ Programs Here

PRODUCT(i++) is replaced as i++ * i++, where you get the correct answer, increments i later due to postfix but first multiplies, so 9 as you got it. (3*3)
However i is incremented TWICE, so i value becomes 5.
Again PRODUCT(++i) is replaced as ++i * ++i, now prefix operation is performed before multiplication, so before multiplication i is incremented twice that is to 7, and 7 * 7 = 49.

Another thing is that this program will give warnings with -Wall flag in gcc because twice incrementing withing sequence points is an "undefined behaviour" in C.

Increment/decrement operators have undefinite behaviour with different languages/compilers.
 

Liverpool_fan

Sami Hyypiä, LFC legend
Re: Post ur C/C++ Programs Here

Increment/decrement operators have undefinite behaviour with different languages/compilers.

And different architectures too. That's why they have to be avoided.
Not sure of languages though, increment decrement operators are pretty well defined in languages like Java or C#.
 

adatapost

Broken In
Re: Post ur C/C++ Programs Here

Hi,

I read number of threads. 90% of programs of c/c++ are written in old-standard (13 years old).

Students and tutors must have to learn something new in c/c++ standard.

*siddhant3s.googlepages.com/how_to_tell_rusted_cpp.html
*home.datacomm.ch/t_wolf/tw/c/ten_commandments.html
*david.tribble.com/text/cdiffs.htm

Use CodeBlocks, Dev-C++, or Visual Studio 2008 compiles - They are free. Do not use old compilers.

conio.h, graphics.h and some other header files are depreciated. Don't put getch() or system("pause") at the end of program.

We always looking for the best and new things. So why not about C/C++?

Thanks,
adatapost
 

Liverpool_fan

Sami Hyypiä, LFC legend
Re: Post ur C/C++ Programs Here

^ ^ ^
Nice posts.

Do me a favour and mail these links to Digit Admins. Reading their fast track to C++, I guess I know they really need these links... :-|
 

adatapost

Broken In
Re: Post ur C/C++ Programs Here

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

Code:
#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...

Here are some non-standard features in your said c++ program.

1. do not use void with main()

Code:
  int main() {
 
    return 0;
  }
2. Do not use deprecated header files and old-compilers - turboc 3.0 or something
Use standard c/c++ compilers.
CodeBlocks, Dev-C++ and Visual Studio 2008 are free compilers.
3. Read about c99 - It's a standard.
*siddhant3s.googlepages.com/how_to_tell_rusted_cpp.html
*home.datacomm.ch/t_wolf/tw/c/ten_commandments.html
*david.tribble.com/text/cdiffs.htm
 

boeing_737

Broken In
Re: Post ur C/C++ Programs Here

PRODUCT(i++) is replaced as i++ * i++, where you get the correct answer, increments i later due to postfix but first multiplies, so 9 as you got it. (3*3)
However i is incremented TWICE, so i value becomes 5.
Again PRODUCT(++i) is replaced as ++i * ++i, now prefix operation is performed before multiplication, so before multiplication i is incremented twice that is to 7, and 7 * 7 = 49.

Another thing is that this program will give warnings with -Wall flag in gcc because twice incrementing withing sequence points is an "undefined behaviour" in C.

Thanks! That helped a lot. :) btw, Since people are mentioning about IDEs, if you want a C/C++ IDE, try Codelite - *www.codelite.org/ - It's open source, and it looks and works beautifully!
 

Ultimate_Winner

Right off the assembly line
Re: Post ur C/C++ Programs Here

I have just started my c++ programming.

Will surely post here when I will learn some more.

Great Thread.
 

yatinkumar

Right off the assembly line
Re: Post ur C/C++ Programs Here

I need help regarding calloc and malloc. I want to solve this question


The standard library function calloc(n,sz) returns a pointer to n objects of size sz, with the storage initialized to zero. Write calloc, by calling malloc or by modifying it.
So plz can somebody help me out
thanx in advance
 

Manan Saxena

Right off the assembly line
Re: Post ur C/C++ Programs Here

hi ...
this is linkage new on digit
i m a btech student 1st year(cs)without the official background of cs language i.e nt have cs in 11th and 12th..
i m using "Yashwant's Kanetkar" Let u C.....i just want to know is their any better book then this .... and one more thing can i finish C in 6mnths or 1st semister...
 

Liverpool_fan

Sami Hyypiä, LFC legend
Re: Post ur C/C++ Programs Here

^ ^ ^
Yashwant Kanetkar sucks :| It's not a good book IMHO

K & R's The C Programming Language is the best book but may not be for beginner's.
C Programming : A Modern Approach is another good book for C.

C can never be `finished` :p but you can have your basics cleared in 6 months or so. :)
 
J

johnrosswrock

Guest
Re: Post ur C/C++ Programs Here

#include<iostream.h>
void prime( int x)
{
if(x==2)
{ cout<<"2 is a prime number.";
}
else if (x==3)
{
cout<<"3 is a prime number.";
}
else if (x==5)
{
cout<<"5 is a prime number.";
}
else if (x==7)
{
cout<<"7 is a prime number.";
}
else if (x<=0)
{
cout<<x<<" is not prime.";
}
else if (x/2 || x/3 || x/5 || x/7 == 0)
{
cout<<x<<" is not prime.";
}
else
{
cout<<x<<" is prime.";
}
}

int main()
{
int x;
cout<<"Enter any integer:\n";
cin>>x;
cout<<"\n";
prime(x);
return 0;
}
 
Status
Not open for further replies.
Top Bottom