Pseudocode Explanation Needed

scojikl's Avatar, Join Date: Aug 2007
Newbie Member
Hi,

Please look at the below pseudocode and help me understand in as much detail as possible why the value of variable A would be 32. Thanks.

SET A TO 1
SET B TO 3
SET A TO A + B
WHILE A < 20
SET A TO (A*A)/2
END WHILE
jwshepherd's Avatar, Join Date: Aug 2007
Team Leader
a=4
set a=(4*4)/2
a=8
a < 20 true do loop again
set a = (8*8)/2
a=32
scojikl's Avatar, Join Date: Aug 2007
Newbie Member
Very helpful. Thanks!
jwshepherd's Avatar, Join Date: Aug 2007
Team Leader
no problem. Good Luck
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Consider working through such things step by step, with pencil and paper. Microprocessors are not magic. They do precisely what is asked of them.
yutalolap's Avatar, Join Date: Mar 2010
Newbie Member
Code:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class sll
{
	private:
	typedef struct node
	{
		int data;
		struct node *next;
	}s1;
	s1 *head;
	int length;
	public:
		sll();
		void create();
		void display();
		void search();
		void del();
		void add();
		void sort();
};
sll::sll()
{
	head=NULL;
}
void sll::create()
{
	s1 *temp,*new1;
	int num,a,i;
	head=new s1;
	temp=head;   //For not letting the value of head to be lost
	cout<<"\nEnter the number of nodes to be created\n";
	cin>>num;
	for(i=0;i<num;i++)
	{
		new1=new s1;
		cout<<"Enter data:\t";
		cin>>a;
		new1->data=a;
		new1->next=NULL;
		temp->next=new1;
		temp=new1;
	}
}
void sll::display()
{
	s1 *temp=NULL;
	length=0;
	temp=head->next; //To access the 1st node created
	cout<<"\nNode\t\tData\t\tNext\n";
	while(temp!=NULL)
	{
		cout<<"\n"<<temp<<"\t"<<temp->data<<"\t\t"<<temp->next;
		temp=temp->next;
		length++;
	}
	cout<<"\nTotal number of nodes = "<<length<<endl;

}
void sll::search()
{
	s1 *temp=NULL;
	int a,flag=0;
	cout<<"\nEnter the data you want to search:\t";
	cin>>a;
	temp=head->next;
	while(temp!=NULL)
	{
		if(temp->data==a)
		{
			flag=1;
			break;
		}
		else
		{
			temp=temp->next;
		}
	}
		if(flag==1)
		{
			cout<<"Data found at address :"<<temp<<endl;
		}
		else
		{
			cout<<"Data Not found"<<endl;
		}
}
void sll::del()
{
	s1 *temp,*pre;
	int a,flag=0;
	cout<<"Enter the element you want to delete:\t";
	cin>>a;
	temp=head->next;
	pre=head;
	while(temp!=NULL)
	{
		if(temp->data==a)
		{
			flag=1;
			pre->next=temp->next;
			delete temp;
			break;
		}
		else
		{
			pre=temp;
			temp=temp->next;
		}
	}
	if(flag==1)
	{
		cout<<"\nData successfully deleted"<<endl;
	}
	else
	{
		cout<<"\nData not found"<<endl;
	}
}

void sll::add()
{
	s1 *temp=NULL,*new1=NULL;
	int a,pos,i;
	cout<<"Enter the element you want to add:\t";
	cin>>a;
	cout<<"\nEnter position where you want to add the data:\n";
	cin>>pos;
	new1=new s1;
	if(pos==1)
	{
		new1->data=a;
		new1->next=head->next;
		head->next=new1;
	}
	else
	{
		temp=head->next;
		for(i=0;i<pos-2;i++)
		{
			if(temp==NULL)
			{
				cout<<"Improper position"<<endl;
				break;
			}
			else
			{
				temp=temp->next;
			}
		 }
			new1->data=a;
			new1->next=temp->next;
			temp->next=new1;
	}
	cout<<"\nAddition operation completed"<<endl;


}

void sll::sort()
{
	s1 *temp,*temp1;
	int a,i,j;
	for(i=0;i<length-1;i++)
	{
		temp=head->next;
		temp1=temp->next;
		for(j=0;j<length-1;j++)
		{
			if(temp->data>temp1->data)
			{
				a=temp->data;
				temp->data=temp1->data;
				temp1->data=a;
			}
			temp=temp->next;
			temp1=temp->next;

		}
	}
	cout<<"Data successfully sorted"<<endl;
  //	length=0;
}



void main()
{
	int ch;
	sll s;
	clrscr();
      //	head=new s1;
	while(1)

	{
		cout<<"\nYou can perform the following operations:\n";
		cout<<"\n1)Create\n2)Display\n3)Add\n4)Delete\n5)Search\n6)sort\n7)Exit\n";
		cout<<"Which operation you would like to perform?\n";
		cin>>ch;
		switch(ch)
		{
			case 7:
			exit(0);
			case 1:
			s.create();
			break;
			case 2:
			s.display();
			break;
			case 5:
			s.search();
			break;
			case 4:
			s.del();
			break;
			case 3:
			s.add();
			break;
			case 6:
			s.sort();
		}
	}


}
i need to rite this entire thing in a pseudo code format... pls can sum one help me! I am not sure about the rules.

Last edited by shabbir; 1Mar2010 at 11:44.. Reason: Code blocks