[C] comment retourner un tab a 2 dim???
memo67
Messages postés
5
Date d'inscription
Statut
Membre
Dernière intervention
-
mype Messages postés 2435 Date d'inscription Statut Membre Dernière intervention -
mype Messages postés 2435 Date d'inscription Statut Membre Dernière intervention -
Bonsoir,
J'ai un petit probleme en C je voudrais retourné un tableau a 2 dimensions en apellant par exemple cette fonction:
void 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");
}
}
Bien sur avec le main qui va avec, il n'y a pas de probleme..
Mais je voudrais bien utiliser cette meme fonction dans une autre or dans mon cas ce que je fais c'est seulement afficher le tableau tabGenMat obtenu..
Please aidez moi.. merci d'avance..;p
J'ai un petit probleme en C je voudrais retourné un tableau a 2 dimensions en apellant par exemple cette fonction:
void 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");
}
}
Bien sur avec le main qui va avec, il n'y a pas de probleme..
Mais je voudrais bien utiliser cette meme fonction dans une autre or dans mon cas ce que je fais c'est seulement afficher le tableau tabGenMat obtenu..
Please aidez moi.. merci d'avance..;p
A voir également:
- Warning function returns address of local variable
- Retourner ecran pc - Guide
- Supercopier 2 - Télécharger - Gestion de fichiers
- Avis sur samsung galaxy tab a9+ - Accueil - Tablettes
- Comment retourner une video - Guide
- 2 ecran pc - Guide
3 réponses
c'est bizarre poste ton code en entier avec le main et l'autre fonction aussi pour voir
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
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
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!