I have a question here:
Complete a program that performs following operations by using C++ programming:
a.Enable user to create and manipulate a list of items (must be able to read integers, characters and strings).
b.Enable user to sort the list. (any sorting algorithm except naïve sort)
c.Enable user to search a particular item in the list. (use any searching algorithm)

Actually i have no idea about it.
Izzit we have to ask the user to choose which data type of list that desired to create?Then we pass the list to the template function?
Can anybody tell me the prosedure?
Im so blur about it...

Recommended Answers

All 5 Replies

Don't ask us, ask your teacher.

here is my cpp file.is there anything goes wrong?

#include<iostream>
#include "orderedArrayListType.h"
using namespace std;

int main()
{
	orderedArrayListType<int> list;
	int num,choice;
	char input;
	char string[10];

	cout<<"****************************************"<<endl;
	cout<<"* WELCOME TO INSERTION SORTING PROGRAM *"<<endl;
	cout<<"****************************************"<<endl;
	cout<<endl;
	cout<<"Please select the type of list that you want to create: "<<endl;
	back:
	cout<<"Enter 1 for interger type."<<endl;
	cout<<"ENter 2 for character type."<<endl;
	cout<<"Enter 3 for string type."<<endl;
	cin>>choice;

	if (choice==1)
	{
		cout<<"Please enter numbers ending with -1"<<endl;
		cin>>num;

		while(num!=-1)
		{
			list.insertOrd(num);
			cin>>num;
		}
	}

	else if (choice==2)
	{
		cout<<"Please enter characters ending with "<<endl;
		cin>>input;

		while(num!=-1)
		{
			list.insertOrd(input);
			cin>>input;
		}
	}

	else if (choice==3)
	{
		cout<<"Please enter strings ending with zzz"<<endl;
		cin>>string;

		while(string!="zzz")
		{
			list.insertOrd(string);
			cin>>string;
		}
	}

	else
	{
		cout<<"Wrong input.Please enter again."<<endl;
		goto back;
	}

	cout<<"The list before sorting: "<<endl;
	list.print();
	cout<<endl;

	list.selectionSort();

	cout<<"The list after sorting:"<<endl;
	list.print();
	cout<<endl;

	return 0;

}

And this is my header file.many errors there but i cant fix them out.
anybody can juz take a look ,and give some tips pls?

template<class elemType>
void orderedArrayListType<elemType>::insertOrd(const elemType& newitem)
{
	nodeType<elemType> *current;
	nodeType<elemType> *trailCurrent;
	nodeType<elemType> *newNode;

	bool found;

	newNode=new nodeType<Type>;
	assert(newNode!=NULL);

	newNode->info=newitem;
	newNode->link=NULL;

	if(first==NULL)
	{
		first=newNode;
		count++;
	}
	else
	{current=first;
	found=false;

	while(current!=NULL && !found)
		if(current->info>=newitem)
			found=true;
		else
		{
			trailCurrent=current;
			current=current->link;
		}

		if(current==first)
		{
			newNode->link=first;
			first=newNode;
			count++;
		}
		else
		{
			trailCurrent->link=newNode;
			newNode->link=current;
			count++;
		}
	}
}

template<class elemType>
class orderedArrayListType:public arrayListType <elemType>
{
	public:
		void insertOrd(const elemType&);
		int binarySearch(const elemType& item);
		void selectionSort();

		orderedArrayListType(int size=100);

	private:
		void swap(int first,int second);
		int minLocation(int first,int last);
};

template<class elemType>
int orderedArrayListType<elemType>::minLocation(int first,int last)
{
	int loc,minIndex;
	minIndex=first;

	for(loc=first+1;loc<=last;loc++)
	{
		if(list[loc]<list[minIndex])
			minIndex=loc;
	}
	
	return minIndex;
}

template<class elemType>
void orderedArrayListType<elemType>::swap(int first,int second)
{
	elemType temp;
	temp=list[first];
	list[first]=list[second];
	list[second]=temp;
}

template<class elemType>
void orderedArrayListType<elemType>::selectionSort()
{
	int loc,minIndex;
	for(loc=0;loc<length-1;loc++)
	{
		minIndex=minLocation(loc,length-1);
		swap(loc,minIndex);
	}
}

template<class elemType>
void orderedArrayListType<elemType>::print()
{
  for (int i = 0; i < last; i++)
  {
    cout << list[i] << " ";
  }
  cout << endl;
}

Is there a reason why you have placed the definition of insertOrd() before the class declaration?

Within insertOrd() you need an opening curly brace between these two lines.

while(current!=NULL && !found)
   if(current->info>=newitem)

and you need a closing brace for the while loop as well. Keeping your indentation consistent and placing the opening and closing braces on a single line by themselves consistently will help you find these errors in the future.

There may be additonal errors as well, but I haven't looked.

Is there a reason why you have placed the definition of insertOrd() before the class declaration?

Within insertOrd() you need an opening curly brace between these two lines.

while(current!=NULL && !found)
   if(current->info>=newitem)

and you need a closing brace for the while loop as well. Keeping your indentation consistent and placing the opening and closing braces on a single line by themselves consistently will help you find these errors in the future.

There may be additonal errors as well, but I haven't looked.

thxs for ur tips...
here i motified my code.
but errors occur for my string variable.
i donno how to debug it.
can anyone tell me how it goes wrong?

#include<iostream>
#include<string>
using namespace std;

char string[20];

void swap(int first,int second)
{
	char temp;
	temp=string[first];
	string[first]=string[second];
	string[second]=temp;
}

int minLocation(int first,int last)
{
	int loc,minIndex;
	minIndex=first;

	for(loc=first+1;loc<=last;loc++)
	{
		if(string[loc]<string[minIndex])
		{
			minIndex=loc;
		}
	}
	
	return minIndex;
}

void print(char string[], int size)
{
  for (int i = 0; i < size; i++)
  {
    cout << string[i] << " ";
  }
  cout << endl;
}

void selectionSort(char string[],int size)
{
	int loc,minIndex;
	for(loc=0;loc<size-1;loc++)
	{
		minIndex=minLocation(loc,size-1);
		swap(loc,minIndex);
	}
}

int main()
{
	int length;

	cout<<"****************************************"<<endl;
	cout<<"* WELCOME TO INSERTION SORTING PROGRAM *"<<endl;
	cout<<"****************************************"<<endl;
	cout<<endl;

	cout<<"Please enter strings ending with zzz"<<endl;

	do{
		cin>>string;
	}
	while(string!="zzz");

	length=strlen(string);

	cout<<"The list before sorting: "<<endl;
	print(string,length);
	cout<<endl;

	selectionSort(string,length);

	cout<<"The list after sorting:"<<endl;
	print(string,length);
	cout<<endl;

	return 0;

}

all the 15 errors show that string:ambiguous symbol.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.