Post your C/C++ Programs Here

Status
Not open for further replies.

Nav11aug

In the zone
Re: Post ur C/C++ Programs Here

Projjwal said:
@Nav11aug
Not the look of program/algo says this program/algo is good this bad.
the complexity (time,space) of the program/algo decide which one is good & which one is bad.
:)

i do knw tht.. but in such a small program as this , the aesthetics cms in. jst look at the beauty of coding in tht single line MASTERY
 

Ankur Gupta

Wandering in time...
Re: Post ur C/C++ Programs Here

Hey does anyone have a program to sort an character array alphabetically...
please post it asap :D
 

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

Wrote this now:

Code:
#include<stdio.h>

int main()
    {
        char a[10]={'a','g','m','j','f','t','p','w','r','h'}, t;
        int i,j;
        
        for(i=0;i<10;i++)
        for(j=i+1;j<10;j++)
        if(a[i]>a[j])
            {
                t=a[i];
                a[i]=a[j];
                a[j]=t;
            }
        
        printf("Sorted array is: \n");
        for(i=0;i<10;i++)
        printf("%c",a[i]);
        
        return 0;
    }
You can modify the input as required.
 

Zeeshan Quireshi

C# Be Sharp !
Re: Post ur C/C++ Programs Here

ankurgupta.me said:
Hey does anyone have a program to sort an character array alphabetically...
please post it asap :D
It's supposed to be the same algorithm you use to sort an Integer Array , as Characters can Implicitly be cast into integers and compared :)
 

Ankur Gupta

Wandering in time...
Re: Post ur C/C++ Programs Here

^^Yeah I too realized that and wrote the program myself and it works like a charm!!
 

jal_desai

In the zone
Re: Post ur C/C++ Programs Here

hello ppl, check out this program... it simulates the TOWER OF HANOI problem... just give the number of rings as the input and it will give u the TOTAL ALGORITHM about how to transfer all of them from one rod to the other using an intermediate rod...

CODE:

#include<conio.h>
#include<iostream.h>
class tower
{
int nodisk;
char frmtwr,totwr,auxtwr;
public:
void hanoi(int,char,char,char);
};

void tower::hanoi(int nodisk,char frmtwr,char totwr,char auxtwr)
{
if (nodisk==1)
{
cout<<"\nMove disk 1 from tower "<<frmtwr<<" to tower "<<totwr;
return;
}
hanoi(nodisk-1,frmtwr,auxtwr,totwr);
cout<<"\nMove disk "<<nodisk<<" from tower "<<frmtwr<<" to tower "<<totwr;
hanoi(nodisk-1,auxtwr,totwr,frmtwr);
return;
}

void main()
{
int no;
tower ob;
clrscr();
cout<<"\n\t\t\t\t--- Tower Of Hanoi ---\n";
cout<<"\n\t\t\t--- (assuming towers X, Y & Z) ---\n";
cout<<"\n\nEnter the number of disks: ";
cin>>no;
ob.hanoi(no,'X','Y','Z');
cout<<"\n\nPress any key to continue...";
getch();
}


Enjoy...
 

Nav11aug

In the zone
Re: Post ur C/C++ Programs Here

we had as our assignment to write a program to transfer n disks when there are p pegs ... not just 3
 
OP
Gigacore

Gigacore

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

I did it... its not working.... here i've captured a video... i didnt use the getch() and didnt execute the program using Alt+F5 in the video... but even after using it, its not working :(

please download the attachment.. its a 2 MB file... but compressed to 33 KB :D
 

The_Devil_Himself

die blizzard die! D3?
Re: Post ur C/C++ Programs Here

@Gigacore I don't know buddy what are you looking for but the following program worked for me.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{int a[10];
int n;
printf("\n Enter the no. of elements in the array:");
scanf("%d",&n);
printf("\nEnter the elements\n");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
                  }
clrscr();                  
printf("\nArray elements are:\n");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
                     }
printf("\n\nPrees any key to exit...");
getch();
}

Checked in turbo c++ IDE(ya ya ya I know how bad it is).
 
OP
Gigacore

Gigacore

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

Its ok buddy... now its working fine on 98 machine....


A Simple C Program to read and print a one-dimensional array

Code:
#include <stdio.h>
#include<conio.h>
void main()
{
	int a[10];
	int i,n;
	printf("\nEnter number of elements in the array:");
	scanf("%d",&n);
	printf("\nEnter the array elements\n");
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	printf("\nArray elements are\n");
	for(i=0;i<n;i++)
		printf("\t%d",a[i]);
return 0;
Getch()
}
 
Last edited:

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

Gigacore said:
Its ok buddy... now its working fine on 98 machine....
Hey, checked your video, your program would obviously close after the output cause of the return 0; specified there :)

In case getch()/getchar() doesn't work out for you, simply put a lame scanf or cin function to halt the execution.
 

Nav11aug

In the zone
Re: Post ur C/C++ Programs Here

QwertyManiac said:
Hey, checked your video, your program would obviously close after the output cause of the return 0; specified there :)

In case getch()/getchar() doesn't work out for you, simply put a lame scanf or cin function to halt the execution.
It is NOT because of the return 0; statement . The second statement works perfectly though... cmon guyz ..u shudve suggested tht already by now!!!A dumb scanf() is the way to go
 
OP
Gigacore

Gigacore

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

C Program to find the largest element in an array and the position of its occurrence.

Code:
#include <stdio.h>
#include <conip.h>
void main()
{
	int a[100];
	int largest,position,num,index;
	scanf("%d",&num);
	for(index=0;index<num;index++)
		scanf("%d",&a[index]);
	largest=a[0];
	position=0;
	for(index=1;index<num;index++)
	{
		if(largest<a[index])
		{
		largest = a[index];
		position=index;
		}
	}
	printf("Largest element in the array is %d \n", largest);
	printf("Largest element's position in the array %d\n", position+1);
	return 0;
Getch();
}
 
Last edited:

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

Nav11aug said:
It is NOT because of the return 0; statement . The second statement works perfectly though... cmon guyz ..u shudve suggested tht already by now!!!A dumb scanf() is the way to go
What I meant was the program returns and exits successfully, so if he executes it in its real environment - the terminal, it'll run just fine. :)
 
Status
Not open for further replies.
Top Bottom