Question

#include <stdlib.h>
#include  <stdio.h>
/*Dados dois vetores de inteiros pegue o maior elemento do primeiro vetor,
troque o o menor do segundo. Devolva os impares do segundo vetor
*/
	int MaiorVetor1(int v1[],int n1){
		int i,IndMaior=0;
		for(i=0;i<n1;i++){
			if(v1[i]>v1[IndMaior]){
				IndMaior=i;
			}
		}
		return IndMaior;
	}
	int MenorVetor2(int v2[],int n2){
		int i,IndMenor=0;
		for(i=0;i<n2;i++){
			if(v2[i]>v2[IndMenor]){
				IndMenor=i;
			}
		}
		return IndMenor;
	}
	void Troca(int v1[],int v2[],int ima ,int ime){
		int aux;
		aux=v1[ima];
		v1[ima]=v2[ime];
		v2[ime]=aux;
	}	
   int ExtraiImpar(int v2[],int n2,int vimpar[]){
	   int i,ni;
	   for(i=0;i<n2;i++){
		   if(v2[i]%2!=0){
			   vimpar[ni]=v2[i];
			   ni++;
            }
	   }
	   return ni;
   }
   	int Concatena(int vimpar[],int ni,int vres[]){
		int i,j,nvr=0;
		for(i=0;i<ni;i++){
			vres[nvr]=vimpar[i];
			nvr++;
		}
		while(i<ni) {
    	vres[nvr] = vimpar[i];
		nvr++;	
		i++;
        return nvr;
		}
	}
   void printa_vetor(int v[], int n){
   int i;
   for(i = 0;i <n;i++) {
      printf("%d ",v[i]); 
 	 }
   printf("\n");
}
	/*int Concatena(int v1[],int v2[],int n1,int n2){
		int i,j,nvr=0,vres[nvr];
		for(i=0;i<n1;i++){
			vres[nvr]=v1[i];
			nvr++;
		}
		for(j=0;j<n2;j++){
			vres[nvr]=v2[j];
			nvr++;
			}
		for(nvr=0;nvr<(n1+n2);nvr++){
			printf(" %i",vres[nvr]);
		}
	}*/
   int main(){
   int n1,n2,i,ima,ime,*v1,*v2,*vres,*vimpar,ni,co;
   printf("Digite o tamanho do vetor 1:\n");
   scanf("%i",&n1);
   printf("Digite o tamanho do vetor 2:\n");
   scanf("%i",&n2);
	v1=(int*)malloc(n1*sizeof(int));
    v2=(int*)malloc(n2*sizeof(int));
    vimpar=(int*)malloc(n2*sizeof(int));
    vres=(int*)malloc((n2)*sizeof(int));
   printf("Digite os elementos do vetor 1:\n");
   for(i=0;i<n1;i++){
	   scanf("%d",&v1[i]);
   }
   printf("Digite os elementos do vetor 2:\n");
   for(i=0;i<n2;i++){
	   scanf("%d",&v2[i]);
   }
   ima=MaiorVetor1(v1,n1);
   ime=MenorVetor2(v2,n2);
   ni=ExtraiImpar(v2,n2,vimpar); 
   Troca(v1,v2,ima,ime);
   ni=ExtraiImpar(v2,n2,vimpar); 	
   Troca(v1,v2,ima,ime);
   co=Concatena(vimpar,ni,vres);
   printa_vetor(vimpar, ni);
   printa_vetor(vres,co);
   }

 

Share this post


Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this