Click here to Skip to main content
15,915,080 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: GDI+ Pen Alignment Pin
bob1697220-Sep-07 9:21
bob1697220-Sep-07 9:21 
Questiondifference b/w RS232 and RS422 Pin
sudheee20-Sep-07 6:56
sudheee20-Sep-07 6:56 
AnswerRe: difference b/w RS232 and RS422 Pin
xjonil20-Sep-07 7:32
xjonil20-Sep-07 7:32 
AnswerRe: difference b/w RS232 and RS422 Pin
jeron120-Sep-07 9:41
jeron120-Sep-07 9:41 
QuestionDateDiff in C++ Pin
rweworld20-Sep-07 6:23
rweworld20-Sep-07 6:23 
AnswerRe: DateDiff in C++ Pin
nbugalia20-Sep-07 6:44
nbugalia20-Sep-07 6:44 
AnswerRe: DateDiff in C++ Pin
bob1697220-Sep-07 9:23
bob1697220-Sep-07 9:23 
Questionmergesort and insertion sort problem.. Pin
makaveli_0720-Sep-07 6:00
makaveli_0720-Sep-07 6:00 
dear profesional over here...please help me with this problem,i have been trying to solve this problem for pass few hours..and i am still a beginner but the task given to me was too tough for me..

this program has 3 errors..i am halfway stuck please help me..
below is da task given to me..please help and do explain me my mistake.
i want to improve..

--Create several files of integers to be used to test sorting methods. Make files of several sizes, for example, size 20, 200, and 2000. Make files that are in order, in reverse order, in random order, and partially in order. By keeping all this test data in files (rather than generating it with random numbers each time the program is run), the same data can be used to test different sorting methods, and hence it will be easier to compare their performance.

--Write a menu-driven program for testing various sorting methods. One option is to read a file of integers into a list. Other options will be to run one of various sorting methods on the list, to print the unsorted or the sorted list, and to quit. After the list is sorted and (optionally) printed, it should be discarded so that testing a later testing method will start with the same input data. Write the program such that each new sorting method will process the original input file.

--Include code in the program to calculate and print

a. CPU time
b. The number of comparisons of keys.
c. The number of assignments of list entries during sorting a list.

Counting comparisons and assignments requires setting up global variables and inserting code into each sorting procedure to increment one of these global variables each time a key comparison and entry assignment is made.



#include <iostream><br />
#include<iomanip.h><br />
#include<math.h><br />
#include <ctime><br />
#include <cstdlib><br />
#include <math.h><br />
int com,call,move;<br />
#define MAX 10<br />
int array[1000];<br />
int arr[], n;<br />
<br />
<br />
//using namespace std; 	//introduces namespace std<br />
<br />
void insertionsort(int array[], int size);<br />
int count1,count2;<br />
void merge(int array[],int first,int mid,int last);<br />
void mergesort(int array[],int first,int last);<br />
void random(int sizeofarray);<br />
void partial(int sizeofarray);<br />
void sorted(int sizeofarray);<br />
void inverse(int sizeofarray);<br />
void insertion();<br />
	void random(int sizeofarray)<br />
	{<br />
			srand(time(NULL));<br />
		for (int i = 1 ; i <= sizeofarray ; i++)<br />
		{<br />
			array[i]=rand();<br />
			cout<<array[i]<<endl;<br />
	<br />
		}<br />
			<br />
	}<br />
<br />
	void inverse(int sizeofarray)<br />
	{<br />
		for(int i = sizeofarray ; i >= 1 ; --i)<br />
		{<br />
			array[i]=i;<br />
			cout<<array[i]<<endl;<br />
		}<br />
	}<br />
<br />
	void sorted(int sizeofarray)<br />
	{<br />
		for (int i = 1 ; i <= sizeofarray ; i++)<br />
		{<br />
			array[i]=i;<br />
			cout<<array[i]<<endl;<br />
		}<br />
	}<br />
<br />
	void partial(int sizeofarray)<br />
	{<br />
		for (int i = 1 ; i <= sizeofarray-5 ; i++)<br />
		{<br />
			array[i]=i;<br />
			cout<<array[i]<<endl;<br />
		}<br />
		for ( i = 10 ; i >= sizeofarray-5 ; i--)<br />
		{<br />
			array[i]=i;<br />
			cout<<array[i]<<endl;<br />
		}<br />
	}<br />
	<br />
<br />
<br />
<br />
<br />
void merge(int array[],int first,int mid,int last)<br />
{<br />
		int temp[1000];<br />
		int first1 = first;<br />
		int last1  = mid;<br />
		int first2 = mid + 1;<br />
		int last2  = last;<br />
<br />
		int index = first1;<br />
<br />
		for (;(first1 <= last1) && (first2 <= last2); ++index)<br />
		{<br />
			if (array[first1] < array[first2])<br />
			{<br />
				temp[index] = array[first1];<br />
				++first1;<br />
				com++;<br />
			}<br />
			else<br />
			{<br />
				temp[index] = array[first2];<br />
				++first2;<br />
				com++;<br />
			}<br />
		}<br />
	<br />
	<br />
	<br />
		<br />
		for (; first1 <= last1 ; ++first1 , ++index)<br />
		{<br />
			 temp[index] = array[first1];<br />
			 move++;<br />
		}<br />
		for (; first2 <= last2 ; ++first2 , ++index)<br />
		{<br />
			 temp[index] = array[first2];<br />
			 move++;<br />
		}<br />
		<br />
<br />
			for( index = first ; index <= last ; ++index)<br />
			{	<br />
				array[index]=temp[index];<br />
				move++;<br />
			}<br />
		<br />
}<br />
void mergesort(int array[],int first,int last)<br />
<br />
{<br />
		if (first < last)<br />
		{<br />
			int mid= (first + last)/2;<br />
			mergesort(array,first,mid);<br />
			mergesort(array,mid+1,last);<br />
			call++;<br />
			merge(array,first,mid,last);<br />
		}<br />
}<br />
<br />
void insertionSort()<br />
{<br />
	 arr[MAX],n;<br />
	<br />
	void getdata();<br />
	void showdata();<br />
	int sortLogic(int d[], int n2);<br />
};<br />
<br />
void sortLogic(int data[], int n)<br />
{<br />
	for (int i=1, j; i<n; i++)<br />
	{<br />
		int tmp = data[i];<br />
		for (j=i; j>0 && tmp<data[j-1]; j--)<br />
			data[j] = data[j-1];<br />
		data[j] = tmp;<br />
	}<br />
	<br />
}<br />
	<br />
<br />
void getdata()<br />
{<br />
	<br />
	cout<<"How many elements you require: ";<br />
	<br />
	cout<<"Enter the elements of your array: ";<br />
	cin>>n;<br />
	for (int i=0; i<n; i++)<br />
		cin>>arr[i];<br />
	sortLogic(arr,n);<br />
}<br />
<br />
void showdata()<br />
{	<br />
	cout<<"\n--Display--\n";<br />
	for (int i=0; i<n; i++)<br />
		cout<<arr[i]<<" ";<br />
}<br />
<br />
	<br />
<br />
	<br />
<br />
<br />
<br />
<br />
<br />
void main()<br />
<br />
{<br />
	int i;<br />
	int n=0;<br />
	double number;<br />
	int level;<br />
	int sizeofarray;<br />
	int operation;<br />
	int function;<br />
	int type;<br />
	int choices;<br />
	int choices1;<br />
	int choices2;<br />
//	int first, mid, last;<br />
<br />
	top:<br />
<br />
	cout<<"Welcome to my program assignment 1"<<endl;<br />
	cout<<""<<endl;<br />
	cout<<"Please choose the type of sorting you would like to use:"<<endl;<br />
	cout<<""<<endl;<br />
	cin>>choices;<br />
<br />
	switch(choices)<br />
	{<br />
	case 1:<br />
	insertionSort();<br />
	break;<br />
	case 2:<br />
	mergesort(array,0,n);<br />
	break;<br />
<br />
	default:<br />
	cout<<"Please enter either one of the number only"<<endl<<endl;<br />
	goto top;<br />
<br />
<br />
	cout<<"Enter the number of array size you wish to create : ";<br />
	cin>>sizeofarray;<br />
	cout<<"Your array size now is "<<sizeofarray<<endl<<endl;<br />
	cout<<"Now select which one fuction you want to sort !"<<endl;<br />
	cout<<"1) for using Random Array."<<endl;<br />
	cout<<"2) for using Partially Sorted Array."<<endl;<br />
	cout<<"3) for using Sorted Array."<<endl;<br />
	cout<<"4) for using Inversely Sorted Array."<<endl<<endl;<br />
	cout<<"Your choice is ";<br />
	cin>>function;<br />
	cout<<endl;<br />
<br />
	switch(function)<br />
	{<br />
	case 1:<br />
	random(sizeofarray);<br />
		break;<br />
	case 2:<br />
	partial(sizeofarray);<br />
		break;	<br />
	case 3:<br />
	sorted(sizeofarray);<br />
		break;<br />
	case 4:<br />
	inverse(sizeofarray);<br />
		break;<br />
		<br />
	default:<br />
		cout<<"Please enter the any above number only!"<<endl<<endl;<br />
		goto top;<br />
		break;<br />
	}<br />
<br />
	int first=0;<br />
	int last=sizeofarray-1;<br />
	mergesort( array,first,last);<br />
	cout<<endl<<"The above array is Unsorted Array :"<<endl;<br />
	<br />
	cout<<endl<<"The Sorted Array is show as below :"<<endl<<endl;<br />
	for ( i=1 ; i <= sizeofarray ; i++)<br />
	{<br />
		cout<<array[i]<<endl;	<br />
	}<br />
<br />
		for (n=0;n<20 && number<=sizeofarray ;n++)<br />
		{<br />
			number=pow(2,n);<br />
		}<br />
	level=n-2;<br />
	operation=3*count1-1;<br />
	cout<<endl<<"The total level is : "<<level<<endl;<br />
	cout<<"The total number of comparisons is : "<<com<<endl;<br />
	cout<<"The total number of move is : "<<move<<endl;<br />
	}<br />
<br />
}<br />
<br />
<br />
<br />

AnswerRe: mergesort and insertion sort problem.. Pin
Iain Clarke, Warrior Programmer20-Sep-07 6:40
Iain Clarke, Warrior Programmer20-Sep-07 6:40 
GeneralRe: mergesort and insertion sort problem.. Pin
makaveli_0720-Sep-07 7:04
makaveli_0720-Sep-07 7:04 
AnswerRe: mergesort and insertion sort problem.. Pin
David Crow20-Sep-07 10:33
David Crow20-Sep-07 10:33 
AnswerRe: mergesort and insertion sort problem.. Pin
John R. Shaw20-Sep-07 21:17
John R. Shaw20-Sep-07 21:17 
QuestionHow to change color of STATIC control in MFC ? Pin
Yanshof20-Sep-07 4:34
Yanshof20-Sep-07 4:34 
AnswerRe: How to change color of STATIC control in MFC ? Pin
Maximilien20-Sep-07 4:51
Maximilien20-Sep-07 4:51 
AnswerRe: How to change color of STATIC control in MFC ? Pin
jhwurmbach20-Sep-07 4:54
jhwurmbach20-Sep-07 4:54 
AnswerRe: How to change color of STATIC control in MFC ? Pin
Mark Salsbery20-Sep-07 6:08
Mark Salsbery20-Sep-07 6:08 
QuestionHow can i throw event from ATL Composite control to some dialog ? Pin
Yanshof20-Sep-07 4:33
Yanshof20-Sep-07 4:33 
AnswerRe: How can i throw event from ATL Composite control to some dialog ? Pin
Nathan Holt at EMOM20-Sep-07 9:21
Nathan Holt at EMOM20-Sep-07 9:21 
QuestionHow to create file in this program and make it generate Pin
snoop8220-Sep-07 3:29
snoop8220-Sep-07 3:29 
AnswerRe: How to create file in this program and make it generate Pin
toxcct20-Sep-07 3:38
toxcct20-Sep-07 3:38 
QuestionCurioisity Pin
Nelek20-Sep-07 4:18
protectorNelek20-Sep-07 4:18 
AnswerRe: Curioisity Pin
toxcct20-Sep-07 6:47
toxcct20-Sep-07 6:47 
AnswerRe: How to create file in this program and make it generate Pin
Programm3r20-Sep-07 3:40
Programm3r20-Sep-07 3:40 
GeneralRe: How to create file in this program and make it generate Pin
snoop8220-Sep-07 4:17
snoop8220-Sep-07 4:17 
GeneralRe: How to create file in this program and make it generate Pin
Nelek20-Sep-07 4:19
protectorNelek20-Sep-07 4:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.