Post your C/C++ Programs Here

Status
Not open for further replies.

Liverpool_fan

Sami Hyypiä, LFC legend
All right.
First of all Bubble Sort is not exactly the most naturally expressed in recursive algorithm. However any iterative algorithm can be made recursive crudely at least. Is the following you desire?

I would still say at least iteratively compare the adjacent items in the function send_max_to_rear, the resulting code stuck with recursion only is very clumsy in my opinion, unless someone does it better.

PHP:
#include <stdio.h>

#define LEN 25

void send_max_to_rear(int *l, int pos, int len); 
void pass(int *l, int len); 

int main(void)	{
	int i;
	int list[LEN];

	for(i = 0; i < LEN; ++i)	{
		printf("Enter for %d: ", i+1);
		scanf("%d", &list[i]);
	}

	pass(list, LEN);

	for(i = 0; i < LEN; ++i)	{
		printf("%d ", list[i]);
	}
	puts(""); // Put a newline at the end of program's stdin

	return 0;
}

void send_max_to_rear(int *l, int pos, int len)	{
	/*
		This function sends the largest member of the sublist to the rear.
	*/
	
	/* Compare the members at `pos` and `pos+1` and swap if necessary */
	if (l[pos] > l[pos+1])	{
		int temp = l[pos];
		l[pos] = l[pos+1];
		l[pos+1] = temp;
	}

	/* As long as there are two elements to compare in the (sub)list, call compare the next two elements. */
	if (pos < len - 2)	{
		send_max_to_rear(l, pos+1, len); // Move to the next position to compare.
	}
}

void pass(int *l, int len)	{
	/*
		This function recursively calls unsorted part of the list, after each pass.
	*/

	/* As long as array is has two unsorted elements, bubble sort it. */
	if (len > 1	)	{
		send_max_to_rear(l, 0, len);
		pass(l, len - 1);
	}
}

P.S.: Divide and Conquer based algorithms are more suitable for recursive implementation, I'll suggest try QuickSort.

P.P.S.: Double test the code just in case, sleepy head you know.
 

clmlbx

Technomancer
Guys, Here is my Programme to calculate Pixel per Inch(ppi) of any screen with Classes in different files.

Code:
// this is main.cpp, actual Programme (Name = main.cpp)

#include <iostream>
#include "PPI.h"

using namespace std;

int main()
{
    PPI ppiobj;
    ppiobj.Welcome();
    ppiobj.GetData();
    ppiobj.Calculateppi();
    ppiobj.Output();

}


Code:
// this is class File/self made header File./ where everything is Declared (Name is = PPI.h)


#ifndef PPI_H
#define PPI_H


class PPI
{
    public:

        PPI();
        void Welcome();
        void GetData();
        void Calculateppi();
        void Output();
        ~PPI();
    protected:
    private:
        int Width;
        int Height;
        int DR;
        float  ppi;
        float root;
        float Size;
};

#endif // PPI_H


Code:
// This is where all class Function is Defined..(Name = PPI.cpp)

#include "PPI.h"
#include <iostream>
#include <math.h>
#include<iomanip>

using namespace std;

PPI::PPI()
{
    //ctor
}

void PPI::Welcome(){

    cout<<setw(40)<<"Welcome"<<endl<<endl;
    cout<<"This Programme Will help you to calculate Pixel Per Inch (PPI) Of your screen"<<endl ;
    cout<<"\n\nThings you should have : "<<endl<<endl<<"1. Resloution"<<endl<<"2. Size of Screen in Inches"<<endl;

}

void PPI::GetData()
{

cout<<"\n\nEnter Resolution in this Format (1280 720) ::  ";
cin>> Width>> Height;

cout<<endl<<"\nEnter sice Of screen ::   ";
cin>>Size;
}


void PPI::Calculateppi(){

DR = (Width * Width) + (Height * Height);

root= sqrt(DR);

ppi = root/Size;


}


void PPI:: Output(){

cout<< "\n\nPPI of your Screen is :: " <<ppi<<endl;
}


PPI::~PPI()
{
    cout<<"\n\n Thank You for using this Programme."<<endl;
    cout <<"\n\nFor any feedback Pls Contact :: feedback@No-one.com";
}


Edit: oops I find out My own mistake.. I made constructor and that Welcome Data was supposed to be in Constructor function.. but while Writing I code I made another Function for it (welcome Function):sad:
 
Last edited:

somulesnar

Journeyman
hey guys here's a c program to merge two single linked lists.... i ve used switch case to get on over wid this prob can u suggest me an easier way....

#include<stdio.h>
#include<conio.h>
#define null 0
void create();
void display();
void insert();
void delet();
void erase();
void create2();
void display2();
void insert2();
void delet2();
void erase2();
void link1();
void link2();
void merge();
struct node
{
int info;
struct node *next;
}*start,*ptr,*temp,*prev;
struct node2
{
int info;
struct node2 *nex;
}*start2,*ptr2,*temp2,*prev2;
void main()
{
int op;
clrscr();
do
{
printf("\n1.link1\n2.link2\n3.merge\n4.exit");
printf("\nEnter your option");
scanf("%d",&op);
switch(op)
{
case 1:
link1();
break;
case 2:
link2();
break;
case 3:
merge();
break;
case 4:
break;
default:
printf("\nEnter correct option");
}
}while(op!=4);
getch();
}
void create()
{
int data;
printf("\nEnter the data");
scanf("%d",&data);
do
{
ptr=malloc(sizeof(struct node));
ptr->info=data;
ptr->next=NULL;
if(start==NULL)
start=ptr;
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=ptr;
}
printf("\nEnter the data & Press 0 to terminate");
scanf("%d",&data);
}while(data!=0);
}
void display()
{
temp=start;
if(start==NULL)
printf("\nList is empty");
else
{
printf("\nElements of list:\n");
while(temp->next!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
printf("%d\t",temp->info);
}
}
void insert()
{
int data,pos;
printf("\nEnter data & position to insert the element");
scanf("d",&data,&pos);
temp=start;
ptr=malloc(sizeof(struct node));
ptr->info=data;
while(temp->next!=NULL)
{
if(temp->info==pos)
{
ptr->next=temp->next;
temp->next=ptr;
break;
}
else
temp=temp->next;
}
if(temp->next==NULL)
{
temp->next=ptr;
ptr->next=NULL;
}
}
void delet()
{
int pos,flag=0;
if(start==NULL)
printf("List is empty");
else
{
printf("Enter element to be deleted");
scanf("%d",&pos);
temp=start;
if(start->info==pos)
{
flag=1;
start=start->next;
free(temp);
}
else
{
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
if(temp->info==pos)
{
flag=1;
prev->next=temp->next;
free(temp);
break;
}
}
}
if(flag==0)
printf("\nElement is not present in list");
}
}
void erase()
{
if(start==NULL)
printf("\nList is empty");
else
{
while(start->next!=NULL)
{
temp=start;
start=start->next;
free(temp);
}
temp=start;
start=start->next;
free(temp);
printf("list is erased");
}
}
void create2()
{
int data;
printf("\nEnter the data");
scanf("%d",&data);
do
{
ptr2=malloc(sizeof(struct node2));
ptr2->info=data;
ptr2->nex=NULL;
if(start2==NULL)
start2=ptr2;
else
{
temp2=start2;
while(temp2->nex!=NULL)
{
temp2=temp2->nex;
}
temp2->nex=ptr2;
}
printf("\nEnter the data & Press 0 to terminate");
scanf("%d",&data);
}while(data!=0);
}
void display2()
{
temp2=start2;
if(start2==NULL)
printf("\nList is empty");
else
{
printf("\nElements of list:\n");
while(temp2->nex!=NULL)
{
printf("%d\t",temp2->info);
temp2=temp2->nex;
}
printf("%d\t",temp2->info);
}
}
void insert2()
{
int data,pos;
printf("\nEnter data & position to insert the element");
scanf("d",&data,&pos);
temp2=start2;
ptr2=malloc(sizeof(struct node2));
ptr2->info=data;
while(temp2->nex!=NULL)
{
if(temp2->info==pos)
{
ptr2->nex=temp2->nex;
temp2->nex=ptr2;
break;
}
else
temp2=temp2->nex;
}
if(temp2->nex==NULL)
{
temp2->nex=ptr2;
ptr2->nex=NULL;
}
}
void delet2()
{
int pos,flag=0;
if(start2==NULL)
printf("List is empty");
else
{
printf("Enter element to be deleted");
scanf("%d",&pos);
temp2=start2;
if(start2->info==pos)
{
flag=1;
start2=start2->nex;
free(temp2);
}
else
{
while(temp2->nex!=NULL)
{
prev2=temp2;
temp2=temp2->nex;
if(temp2->info==pos)
{
flag=1;
prev2->nex=temp2->nex;
free(temp2);
break;
}
}
}
if(flag==0)
printf("\nElement is not present in list");
}
}
void erase2()
{
if(start2==NULL)
printf("\nList is empty");
else
{
while(start2->nex!=NULL)
{
temp2=start2;
start2=start2->nex;
free(temp2);
}
temp2=start2;
start2=start2->nex;
free(temp2);
printf("list is erased");
}
}
void link1()
{
int op;
start=NULL;
do
{
printf("\n1.create\n2.display\n3.insert\n4.delete\n5.erase\n6.exit");
printf("\nEnter your option");
scanf("%d",&op);
switch(op)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert();
break;
case 4:
delet();
break;
case 5:
erase();
break;
case 6:
break;
default:
printf("\nEnter correct option");
}
}while(op!=6);
getch();
}
void link2()
{
int op;
start2=NULL;
do
{
printf("\n1.create\n2.display\n3.insert\n4.delete\n5.erase\n6.exit");
printf("\nEnter your option");
scanf("%d",&op);
switch(op)
{
case 1:
create2();
break;
case 2:
display2();
break;
case 3:
insert2();
break;
case 4:
delet2();
break;
case 5:
erase2();
break;
case 6:
break;
default:
printf("\nEnter correct option");
}
}while(op!=6);
getch();
}
void merge()
{
if(start==NULL)
{
start=start2;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=start2;
}
temp=start;
if(start==NULL)
printf("\nList is empty");
else
{
printf("\nElements of list:\n");
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
}
getch();
}
 

raj100

Right off the assembly line
Armstrong value program

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int save,number,digit,count=0,rev=0,m,n,save2;
clrscr();
printf("Enter number to print=");
scanf("%d",&number);
save=number;
save2=number;
while(number!=0)
{
m=number%10;
digit=m/m;
count=count+digit;
number=number/10;
}
printf("\n %d",count);
while(save!=0)
{
n=save%10;
rev=rev+pow(n,count);
save=save/10;
}
printf("\n %d",rev);
if(rev==save2)
{
printf("\nEnter number is Armstrong");
}
else
{
printf("\nEnter number is not armstrong");
}
getch();
}
 

nims11

BIOS Terminator
Re: Armstrong value program

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int save,number,digit,count=0,rev=0,m,n,save2;
clrscr();
printf("Enter number to print=");
scanf("%d",&number);
save=number;
save2=number;
while(number!=0)
{
m=number%10;
digit=m/m;
count=count+digit;
number=number/10;
}
printf("\n %d",count);
while(save!=0)
{
n=save%10;
rev=rev+pow(n,count);
save=save/10;
}
printf("\n %d",rev);
if(rev==save2)
{
printf("\nEnter number is Armstrong");
}
else
{
printf("\nEnter number is not armstrong");
}
getch();
}

live in present buddy!
 

priyaranjan

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

scanf("%d%d%d",&a,&b,&c);
New to c++.......Whta those this line means

scanf() takes input from standard input(key board) and stores in some variables.

%d is called format specifier for integer variable, it tells compiler that coming input from the keyboard is an integer .

next is &a , this is the address of the variable "a" which needs to be passed to the scanf()
to store the input data in variable "a".

so by combing all compiler now knows that input data needs to be stored in an integer variable and the variable is a.

now in your question there are three inputs coming from your key board so you have 3 %d and the three address of the variables that is &a,&b,&c.
 
Status
Not open for further replies.
Top Bottom