viernes, 16 de septiembre de 2011

Combinar vectores ordenados

Combina 2 vectores ordenados de tal manera que la combinación también quede ordenada


#include "stdafx.h"
#include "iostream"
#include "conio.h"

using namespace std;

int n;

void leerVector(int vector[])
{
for(int i=0;i<n;i++)
{
cout<<"["<<i+1<<"] = ";
cin>>vector[i];
}
}

void intercambiar(int vector[],int i,int j)
{
int temp;
temp=vector[i];
vector[i]=vector[j];
vector[j]=temp;
}

void ordenarVector(int vector[])
{
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(vector[i]>vector[j])
intercambiar(vector,i,j);
}
}
}

void mostrarVector(int vector[])
{
for(int i=0;i<n*2;i++)
cout<<endl<<"["<<i+1<<"] = "<<vector[i];
}

void combinarVector(int vector1[],int vector2[],int vector3[])
{
int index1=0,index2=0;

for(int i=0;i<n*2;i++)
{
if(index1==n)//¿Ya se compararon todos los elementos del vector 1?
{
vector3[i]=vector2[index2];
index2++;
}
else
{
if(index2==n)//¿Ya se compararon todos los elementos del vector 2?
{
vector3[i]=vector1[index1];
index1++;
}
else
{
if(vector1[index1]<=vector2[index2])
{
vector3[i]=vector1[index1];
index1++;
}
else
{
vector3[i]=vector2[index2];
index2++;
}
}
}
}
}

void main()
{
int *V1,*V2,*V3;

cout<<"Elementos de AMBOS arreglos: ";cin>>n;

V1 = new int [n];
V2 = new int [n];

V3 = new int [n*2];//Este vector será la combinación de los elementos de los otros dos vectores!

cout<<endl<<"1er arreglo: "<<endl<<endl;

leerVector(V1);

cout<<endl<<"2do arreglo: "<<endl<<endl;

leerVector(V2);

ordenarVector(V1);
ordenarVector(V2);

combinarVector(V1,V2,V3);

cout<<endl<<endl<<"Combinacion de los arreglos: "<<endl<<endl;

mostrarVector(V3);

_getch();
}

1 comentario:

  1. Before the CAD designed model may be changed into machine language, the CAM software determines the chopping paths for the tools for the removal of the excess materials from the workpiece. CAD and CAM work CNC machining collectively to supply the CNC machine with the correct and correct directions to perform the mandatory chopping operations. In lathe machines, items are minimize in a circular course with indexable tools. With CNC technology, the cuts employed by lathes are carried out with precision and high velocity.

    ResponderEliminar