[C] comment retourner un tab a 2 dim???
Fermé
memo67
Messages postés
5
Date d'inscription
mercredi 19 mars 2008
Statut
Membre
Dernière intervention
22 mars 2008
-
21 mars 2008 à 23:12
mype Messages postés 2435 Date d'inscription jeudi 1 novembre 2007 Statut Membre Dernière intervention 16 août 2010 - 22 mars 2008 à 16:42
mype Messages postés 2435 Date d'inscription jeudi 1 novembre 2007 Statut Membre Dernière intervention 16 août 2010 - 22 mars 2008 à 16:42
A voir également:
- Warning function returns address of local variable
- Retourner ecran windows - Guide
- Shift tab - Forum Windows
- Jdownloader 2 - Télécharger - Téléchargement & Transfert
- 2 comptes whatsapp - Guide
- Comment retourner une video - Guide
3 réponses
mype
Messages postés
2435
Date d'inscription
jeudi 1 novembre 2007
Statut
Membre
Dernière intervention
16 août 2010
435
21 mars 2008 à 23:19
21 mars 2008 à 23:19
en changeant l'en tete sans oublier le return a la fin
int** genMat (int n, int k)
mype
Messages postés
2435
Date d'inscription
jeudi 1 novembre 2007
Statut
Membre
Dernière intervention
16 août 2010
435
22 mars 2008 à 04:32
22 mars 2008 à 04:32
c'est bizarre poste ton code en entier avec le main et l'autre fonction aussi pour voir
memo67
Messages postés
5
Date d'inscription
mercredi 19 mars 2008
Statut
Membre
Dernière intervention
22 mars 2008
22 mars 2008 à 11:30
22 mars 2008 à 11:30
Bonjour mype,
Je voudrais d'abord te remericer pour l'attention que tu portes a mon probleme..;p
je t'envoie mon code en entier :
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int** genMat (int n, int k){
int i, j, nbrli, m;
nbrli=(n/k);
int tabGenMat[nbrli][n];
for (i=0;i<nbrli;i++){
for (j=0;j<n;j++)
{
tabGenMat[i][j]=0;
}
}
for(i=0;i<nbrli;i++){
for(j=0;j<n;j++){
m=(j+1) +k*i;
if (m<=k*(i+1)){
tabGenMat[i][m-1]=1;
}
}
}
for (i=0;i<nbrli;i++){
for (j=0;j<n;j++){
printf ("%d ", tabGenMat[i][j]);
}
printf("\n");
}
return tabGenMat;
}
void randPerm(int min, int n, int *tableau){
int i,j,ok;
if (n >= min){
i = 0;
while(i <= (n - min)){
tableau[i] = (int) (min + ((float) rand() / RAND_MAX * (n - min + 1)));
ok = 1;
if (i > 0){
for (j = 0; j < i; j++){
if (tableau[i] == tableau[j]){
ok = 0;
break;
}
}
}
if(ok){
i++;
}
}
}else{
randPerm(n, min, tableau);
}
for(i = 0; i <= n-min; i++){
printf("%d ",tableau[i]);
}
}
void permCol(int n,int tabGenMat[][n], int tableau[], int j){
int i,k,l;
int tableau2[n];
int tableau3[n];
for(i=0;i<n;i++){
tableau2[i]=tabGenMat[j][i];
}
for(k=0;k<n;k++){
tableau3[k]=tableau2[tableau[k]];
}
for(l = 0; l < n; l++){
printf("%d ",tableau3[l]);
}
}
int main(int argc, char *argv[]) {
srand(time(NULL));
int n,k,min,nbrligne;
min=1;
n=atoi(argv[1]);
k=atoi(argv[2]);
int tableau[n];
nbrligne=(n/k);
genMat(n,k);
randPerm(min, n, tableau);
permCol(n,tabGenMat,tableau,1);
return 1;
}
La fonction genMat me renvoi une matrice de cette forme..
pour genMat(6,2) ->
110000
001100
000011
La fonction randPerm me renvoi un vecteur ligne de cette forme..
pour randPerm(6)-> 5,4,6,1,2,3
La fonction permCol je voudrais qu'il me renvoi..
la permutation de randPerm applliqué a la matrice de genMat pour la ligne j=1 par exemple..
Dans mon cas pour la 1ere ligne ca devrait donner ->0,0,0,1,1,0
Mais le compilateur m'affiche:
ter.c: In function 'genMat':
ter.c:33: warning: return from incompatible pointer type
ter.c:33: warning: function returns address of local variable
ter.c: In function 'main':
ter.c:96: error: 'tabGenMat' undeclared (first use in this function)
ter.c:96: error: (Each undeclared identifier is reported only once
ter.c:96: error: for each function it appears in.)
Voila c'est ca mon probleme, je ne sais pas comment recuperer la matrice créer dans la fonction genMat..
Merci d'avance de bien vouloir me repondre..;p
Je voudrais d'abord te remericer pour l'attention que tu portes a mon probleme..;p
je t'envoie mon code en entier :
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int** genMat (int n, int k){
int i, j, nbrli, m;
nbrli=(n/k);
int tabGenMat[nbrli][n];
for (i=0;i<nbrli;i++){
for (j=0;j<n;j++)
{
tabGenMat[i][j]=0;
}
}
for(i=0;i<nbrli;i++){
for(j=0;j<n;j++){
m=(j+1) +k*i;
if (m<=k*(i+1)){
tabGenMat[i][m-1]=1;
}
}
}
for (i=0;i<nbrli;i++){
for (j=0;j<n;j++){
printf ("%d ", tabGenMat[i][j]);
}
printf("\n");
}
return tabGenMat;
}
void randPerm(int min, int n, int *tableau){
int i,j,ok;
if (n >= min){
i = 0;
while(i <= (n - min)){
tableau[i] = (int) (min + ((float) rand() / RAND_MAX * (n - min + 1)));
ok = 1;
if (i > 0){
for (j = 0; j < i; j++){
if (tableau[i] == tableau[j]){
ok = 0;
break;
}
}
}
if(ok){
i++;
}
}
}else{
randPerm(n, min, tableau);
}
for(i = 0; i <= n-min; i++){
printf("%d ",tableau[i]);
}
}
void permCol(int n,int tabGenMat[][n], int tableau[], int j){
int i,k,l;
int tableau2[n];
int tableau3[n];
for(i=0;i<n;i++){
tableau2[i]=tabGenMat[j][i];
}
for(k=0;k<n;k++){
tableau3[k]=tableau2[tableau[k]];
}
for(l = 0; l < n; l++){
printf("%d ",tableau3[l]);
}
}
int main(int argc, char *argv[]) {
srand(time(NULL));
int n,k,min,nbrligne;
min=1;
n=atoi(argv[1]);
k=atoi(argv[2]);
int tableau[n];
nbrligne=(n/k);
genMat(n,k);
randPerm(min, n, tableau);
permCol(n,tabGenMat,tableau,1);
return 1;
}
La fonction genMat me renvoi une matrice de cette forme..
pour genMat(6,2) ->
110000
001100
000011
La fonction randPerm me renvoi un vecteur ligne de cette forme..
pour randPerm(6)-> 5,4,6,1,2,3
La fonction permCol je voudrais qu'il me renvoi..
la permutation de randPerm applliqué a la matrice de genMat pour la ligne j=1 par exemple..
Dans mon cas pour la 1ere ligne ca devrait donner ->0,0,0,1,1,0
Mais le compilateur m'affiche:
ter.c: In function 'genMat':
ter.c:33: warning: return from incompatible pointer type
ter.c:33: warning: function returns address of local variable
ter.c: In function 'main':
ter.c:96: error: 'tabGenMat' undeclared (first use in this function)
ter.c:96: error: (Each undeclared identifier is reported only once
ter.c:96: error: for each function it appears in.)
Voila c'est ca mon probleme, je ne sais pas comment recuperer la matrice créer dans la fonction genMat..
Merci d'avance de bien vouloir me repondre..;p
mype
Messages postés
2435
Date d'inscription
jeudi 1 novembre 2007
Statut
Membre
Dernière intervention
16 août 2010
435
22 mars 2008 à 16:42
22 mars 2008 à 16:42
le probleme c'est que tu passes a la fonction permCol un tableau que tu n'as pas declaré donc ça te fait une erreur de segmentation...
ce que tu peux faire pour passer le tableau cree dans genMat() a permCol() c'est inclure l'appel directement dans l'autre appel
ce que tu peux faire pour passer le tableau cree dans genMat() a permCol() c'est inclure l'appel directement dans l'autre appel
permCol(n,genMat(n,k),tableau,1);mais bien sur a l'éxécution dans ce cas tu auras la ligne de vecteur de randPerm() avant puis ton tableau de genMat() apres
21 mars 2008 à 23:39
ter.c:33: warning: return from incompatible pointer type
ter.c:33: warning: function returns address of local variable
C'est que des warning mais lors de l'execution :
Erreur de segmentation
J'ai bien mis le type de retour int** et return tabGenMat; a la fin de la fonction..(ligne 33 comme tu peux le voir)
Voili voila ca m'enerve!