VUsolutions Transferred to AchiKhasi.com

From December 2011, this blog www.VUsolutions.blogspot.com is transferred to http://achikhasi.com/vu/ . So, you may visit http://achikhasi.com/vu/ for latest study related help.

Back to home VUsolutions

VUsolutions Fans Club [join us for MORE solutions]

VUsolutions on Facebook

CS201 Solutions

Monday, November 16, 2009 Posted In Edit This
#include



using namespace std;
void sort(char array[], int length);

int main()
{
char firstArray[15];
char secondArray[15];
char mergedArray[30];
bool flag = false;

int i;
int mergedIndex;

cout<<"Enter into First Array\n";
fgets(firstArray, sizeof(firstArray), stdin);

cout<<"Enter into Second array\n";
fgets(secondArray, sizeof(secondArray), stdin);

// Copy the first array into the merged array
for(i = 0; firstArray[i] != '\n'; ++i)
{
mergedArray[i] = firstArray[i];
}
mergedIndex = i;

//Copy second array into merged array
for(i = 0; secondArray[i] != '\n'; ++i)
{
mergedArray[mergedIndex++] = secondArray[i];
}

mergedArray[mergedIndex] = 0;
cout<<"\nMerged : ";
for(i = 0; i<>
{
cout<
}
sort(mergedArray, mergedIndex);
cout<<"\nSorted : ";
for(i = 0; i<>
{
cout<
}
system("PAUSE");
return EXIT_SUCCESS;
}
void sort(char array[], int length)
{
int stop = length - 1;
for (int i = 0; i <>
{
for (int j = 0; j <>
{
if (array[j] > array[j+1])
{
char tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
}
}

}
}
------------------------------

-----------------------------

#include
#include
#include
#include

using namespace std;

const size_t N = 10;
const char space = ' ';
void selectionSort(char *, size_t length);
void printArray(const char *);

int main(int argc, char *argv[]) {
string line;
string::iterator i;
size_t j, k;
char *a1 = new char[N],
*a2 = new char[N],
*merged = new char[N*2];

c << "For arrays A1 and A2, enter up to " << N << endl;
c << "characters (not counting spaces)" << endl;
c << "A1 > ";
getline(cin,line);
for (i = line.begin(), j = 0; (i != line.end()) && (j < N); i++) {
if (*i != space) a1[j++] = *i;
}
c << "A2 > ";
getline(cin,line);
for (i = line.begin(), j = 0; (i != line.end()) && (j < N); i++) {
if (*i != space) a2[j++] = *i;
}

// Merge
for (j = 0, k = 0; j < N; j++) {
merged[k++] = a1[j];
if (strchr(a1,a2[j]) == NULL) {
merged[k++] = a2[j];
}
}
c << "Merged : ";
printArray(merged);

// Sort
selectionSort(merged,k);
c<< "Sorted : ";
printArray(merged);

delete [] a1;
delete [] a2;
delete [] merged;

return 0;
}

void printArray(const char *a) {
for (size_t i = 0; i < strlen(a); i++) {
c<< a[i] << " ";
}
c<< endl;
}

//
// Selection sort of a char array, ignoring case.
//
void selectionSort(char *a, size_t n) {
char min, t;

for (size_t i = 0; i < n; i++) {
min = i;
for (size_t j = i+1; j < n; j++) {
if (tolower(a[j]) < tolower(a[min])) {
min = j;
}
}
t = a[min];
a[min] = a[i];
a[i] = t;
}
}

#if 0

Sample run:

For arrays A1 and A2, enter up to 10
characters (not counting spaces)
A1 > a h b c u v I j k e
A2 > y u d f g k I w q a
Merged : a y h b d c f u g v I j w k q e
Sorted : a b c d e f g h I j k q u v w y

#endif

----------------------------------

----------------------------------

#include
using namespace std;

void merge(char[],int, char[],int ,char[],int&);
void fill(char[], int&,char[],int,int&);
void print(char[],int,string);
void input(char[],int,string);
void sort(char[],int);
bool already(char,char[],int);
int main()
{char a[10],b[10],c[20];
int asize=10,csize=0,bsize=10;
input (a,asize,"array 1");
input (b,bsize,"array 2");

print(a,asize,"Original Array 1");
print(b,bsize,"Original Array 2");
merge(a,asize,b,bsize,c,csize);
print(c,csize,"Merged Array");
sort(c,csize);
print(c,csize,"Sorted Merged Array");
system("pause");
return 0;
}
void print(char a[],int n,string mess)
{int i;
cout<
for(i=0;i
cout<
cout<
return;
}
void merge(char a[],int asize,char b[],int bsize,char c[], int& csize)
{int aupto=0,bupto=0,turn=0;
while(aupto+bupto<(asize+bsize))
{
if(turn==0)
{if(!already(a[aupto],c,csize))
c[csize++]=a[aupto++];
else
aupto++;
if(aupto==asize)
fill(c,csize,b,bsize,bupto);
turn=1;
}

else
{if(!already(b[bupto],c,csize))
c[csize++]=b[bupto++];
else
bupto++;
if(bupto==bsize)
fill(c,csize,a,asize,aupto);
turn=0;
}
}
return;
}

void fill(char c[],int &csize,char b[],int bsize,int &bupto)
{while(bupto
if(!already(b[bupto],c,csize))
c[csize++]=b[bupto++];
else
bupto++;
return;
}
void sort(char a[],int size)
{int i,j;
char t;
for(i=0;i
for(j=i;j
if(a[j]
{t=a[i];
a[i]=a[j];
a[j]=t;
}
}
void input(char a[],int n, string mess)
{int i;
cout<<"Enter "<
for(i=0;i
cin>>a[i];
}
bool already(char a,char c[],int csize)
{int i;
for(i=0;i
if(a==c[i])
return true;

return false;
}


Back to home VUsolutions

Shaadi.com: Just create ur account & find ur partner or EARN money, its reall & EASY

VUsolutions Followers (Join NOW and Get Extra Benefits)

Install LATEST toolbar having lot of features - GET solutions on Desktop

toolbar powered by Conduit
Caliplus 300x250 NoFlam VitoLiv 468x60 GlucoLo