duvida em linguagem c [RESOLVIDO]

1. duvida em linguagem c [RESOLVIDO]

Thiago
thiagovasconcelo

(usa Ubuntu)

Enviado em 26/10/2014 - 01:33h

meus amigos, o trecho do codigo abaixo faz parte de um projeto q tow trabalhando, sou novo na programaçao e estou com uma duvida mto dura.

for(i = x; i < x+LINmtzREC; ++i) //Preenchimento da matriz recorte.
{
for(j = y; j < y+COLmtzREC; ++j)
{
for (z = 0; z < (LINmtzREC*COLmtzREC); z++)
{
*(pMtzRec + z) = *(pMtzOrig + (i*(y+5) + j));
//*(pMtzRec + 0) = *(pMtzOrig + (0*(y+5)+0));
//*(pMtzRec + 1) = *(pMtzOrig + (0*(y+5)+1));
//*(pMtzRec + 2) = *(pMtzOrig + (1*(y+5)+0));
//*(pMtzRec + 3) = *(pMtzOrig + (1*(y+5)+1));
}
}
}

preciso que o laço for percorra i e j da msm forma como esta no comentários, pMtzRec e pMtzOri, são ponteiros que apontam para matrizes.

AJUDEM-ME POR FAVOR ! ! !

Obg e Abraços


  


2. Re: duvida em linguagem c [RESOLVIDO]

Luis R. C. Silva
luisrcs

(usa Linux Mint)

Enviado em 26/10/2014 - 09:38h

thiagovasconcelo escreveu:

meus amigos, o trecho do codigo abaixo faz parte de um projeto q tow trabalhando, sou novo na programaçao e estou com uma duvida mto dura.

for(i = x; i < x+LINmtzREC; ++i) //Preenchimento da matriz recorte.
{
	for(j = y; j < y+COLmtzREC; ++j)
	{
		for (z = 0; z < (LINmtzREC*COLmtzREC); z++)
		{
			*(pMtzRec + z) = *(pMtzOrig + (i*(y+5) + j));
			//*(pMtzRec + 0) = *(pMtzOrig + (0*(y+5)+0));
			//*(pMtzRec + 1) = *(pMtzOrig + (0*(y+5)+1));
			//*(pMtzRec + 2) = *(pMtzOrig + (1*(y+5)+0));
			//*(pMtzRec + 3) = *(pMtzOrig + (1*(y+5)+1));
		}
	}
}
 

preciso que o laço for percorra i e j da msm forma como esta no comentários, pMtzRec e pMtzOri, são ponteiros que apontam para matrizes.

AJUDEM-ME POR FAVOR ! ! !

Obg e Abraços


Sinceramente não entendi. Você colocou uma cadeia de três for(). Onde quer acessar as variáveis afinal?


3. Re: duvida em linguagem c [RESOLVIDO]

Thiago Henrique Hüpner
Thihup

(usa Manjaro Linux)

Enviado em 26/10/2014 - 16:56h

Amigo, poderia postar o codigo todo ?

Assim fica mais facil de lhe ajudar

Obs: Coloque o codigo entre ["code"]Aqui vai o seu codigo ...["/code"] (sem aspas)

Então ele ficará assim :


#include <stdio.h>
....
 


[]'s

T+


4. codio completo

Thiago
thiagovasconcelo

(usa Ubuntu)

Enviado em 26/10/2014 - 17:15h

ai vai o codigo todo:


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define RANDMAX 100

//void recorte(int *pOrig, int M, int fx, int fy, int m, int n, int l, int k); //Protótipo da função recorte.

int main(int argc, char *argv[])
{
	int LINmtzORIG, COLmtzORIG;
	int LINmtzREC, COLmtzREC;
	int x, y;
	LINmtzORIG = atoi(argv[1]);
	COLmtzORIG = atoi(argv[2]);
	x = atoi(argv[3]);
	y = atoi(argv[4]);
	LINmtzREC = atoi(argv[5]);
	COLmtzREC = atoi(argv[6]);
	int matrizORIG[LINmtzORIG][COLmtzORIG], *pMtzOrig = NULL;
	int matrizREC[LINmtzREC][COLmtzREC], *pMtzRec = NULL;
	int i, j, z;
	
	if (LINmtzORIG < x+LINmtzREC || COLmtzORIG < y+COLmtzREC) //Teste para saber se a matriz recorte cabe dentro da matriz original.
	{
		printf("\nMatriz recorte não cabe dentro da matriz original!\n");
		exit(1);
	}
	
	//Ponteiros
	pMtzOrig = &matrizORIG[0][0];
	pMtzRec = &matrizREC[0][0];
	
	//Alocação Dinâmica,
	pMtzOrig = (int*) malloc(LINmtzORIG*COLmtzORIG*sizeof(int));
	pMtzRec = (int*) malloc(LINmtzREC*COLmtzREC*sizeof(int));
	
	if ((!pMtzOrig)) //Teste para saber se há memória para alocar a matriz original.
	{
		printf("Sem memória!\n");
		exit(1);
	}
	if ((!pMtzRec)) //Teste para saber se há memória para alocar a matriz recorte.
	{
		printf("Sem memória!\n");
		exit(1);
	}
	
	srand(time(NULL)); //Semente para gerar a matriz de números aleatórios.
	
	for (i = 0; i < LINmtzORIG * COLmtzORIG; i++) //Preenchimento da matriz original.
	{
		*(pMtzOrig + i) = rand() % RANDMAX;
	}
	
	printf("\nMATRIZ ORIGINAL:\n\n");
	for (i = 0; i < LINmtzORIG; i++) //Impressão da matriz original.
	{
		for (j = 0; j < COLmtzORIG; j++)
		{
			printf("%d\t", *(pMtzOrig + i*COLmtzORIG + j));
		}
		printf("\n");
	}
	
	printf("\nMATRIZ RECORTE:\n\n");

	/*for(i = x; i < x+LINmtzREC; i++) //Preenchimento da matriz recorte sem função. *(Não funciona)
	{ 
		for(j = y; j < y+COLmtzREC; j++)
		{
			*(pMtzRec + (i-x) + (j-y) + (i-x)) = *(pMtzOrig + i*(LINmtzORIG) + j);
		}
	}*/
	
	//recorte(pMtzOrig, LINmtzORIG, x, y, LINmtzREC, COLmtzREC, i, j); //Impressão da matriz recorte com função.
	
	/*for (i = 0; i < LINmtzREC; i++) //impressão da matriz recorte. *
	{
		for (j = 0; j < COLmtzREC; j++)
		{
			printf("%d\t", *(pMtzOrig + i*(LINmtzORIG) + j)); //*(pMtzRec + i*COLmtzREC + j));
		}
		printf("\n");
	}*/
	
//	for (z = 0; z < (LINmtzREC*COLmtzREC); z++) //Preenchimento da matriz recorte.
//	{
		for(i = x; i < x+LINmtzREC; i++)
		{
			for(j = y; j < y+COLmtzREC; j++)
			{
				*(pMtzRec + 0) = *(pMtzOrig + i + j);
				//*(pMtzRec + 0) = *(pMtzOrig + (0*(y+LINmtzREC*COLmtzREC+1)+0));
				//*(pMtzRec + 1) = *(pMtzOrig + (0*(y+LINmtzREC*COLmtzREC+1)+1));
				//*(pMtzRec + 2) = *(pMtzOrig + (1*(y+LINmtzREC*COLmtzREC+1)+0));
				//*(pMtzRec + 3) = *(pMtzOrig + (1*(y+LINmtzREC*COLmtzREC+1)+1));
			}
		}
//	}
	
	for (i = 0; i < LINmtzREC; i++) //impressão da matriz recorte. *
	{
		for (j = 0; j < COLmtzREC; j++)
		{
			printf("%d\t", *(pMtzRec + i*(COLmtzREC) + j));
		}
		printf("\n");
	}
	return 0;
}

/*void recorte(int *pOrig, int M, int fx, int fy, int m, int n, int l, int k) //Função para impressão da matriz recorte.
{
	for (k = fx; k < fx+m; k++)
	{ 
		for (l = fy; l < fy+n; l++)
		{
			printf("%d\t", *(pOrig + k*(M) + l));//*(pRecorte + (k-fx) + (l-fy) + (k-fx)) = *(pOrig + k*(M) + l);
		}
			printf("\n");
	}
}*/
 


PS.: Meu programa tem que gerar uma matriz aleatoria e fazer um "recorte" nessa matriz, os parametros da matriz recorte são dados através de argv e argc.


5. comentários

Thiago
thiagovasconcelo

(usa Ubuntu)

Enviado em 26/10/2014 - 17:18h

é só ter um pouquinho de atenção, pois tem uma boa parte dele comentado, foram alguns teste que fiz.


6. Re: duvida em linguagem c [RESOLVIDO]

Perfil removido
removido

(usa Nenhuma)

Enviado em 26/10/2014 - 17:25h

Tenta primeiro pegar os valores i e j da matriz, aí sim você percorre as linhas e colunas dela (com dois for já dá pra fazer isso);


Exemplo:

A11 A12 A13 A14
A21 A22 A23 A24
A31 A32 A33 A34

A matriz acima é 3x4 (ou seja, i=3; j=4); a partir disso, você percorre ela usando o for;






Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts