/* -- correction feuille8 -*/
/*LG exos tab2d et chaines*/
/*-----------------------*/

#include <stdio.h>
#include<stdlib.h>
#include<time.h>

#define CMAX 6
#define LMAX 5
#define MAX 100

/* donne un entier au hasard entre binf et bsup comprises*/
int hasard(int binf, int bsup)
{
  int rando = rand();
  return (binf + rando %(bsup - binf +1));
}

/*initialisation d'un tableau 2d*/
void initialise_hasard(int t[LMAX][CMAX])
{
  int i,j;
    for (i=0;i<LMAX;i++)
    {
      for (j=0;j<CMAX;j++)
	{
	  t[i][j]= hasard(0,1);
	}
    }
}

/*impression d'un tableau 2d*/
void imprime_tableau(int t[LMAX][CMAX])
{
    int i,j;
    for (i=0;i<LMAX;i++)
    {
      for (j=0;j<CMAX;j++)
	{
	  printf("%d  ",t[i][j]);
	}
      printf("\n");
    }
}

/* 3 page 43 */
int sommetab(int t[LMAX][CMAX], int ligne[CMAX], int colonne[LMAX])
{
  int i,j,tmp;

  /*initialisation*/
  tmp = 0;
  for (j=0;j<CMAX;j++)
    {
      ligne[j]=0;
    }
  for (i=0;i<LMAX;i++)
    {
      colonne[i]=0;
    }

  /*remplissage des tableaux auxiliaires*/
  for (i=0;i<LMAX;i++)
    {
      for (j=0;j<CMAX;j++)
	{
	  ligne[j] = ligne[j] + t[i][j];
	  colonne[i] = colonne[i] + t[i][j];
	}
    }

  /*calcul de la somme des éléments*/
  for (j=0;j<CMAX;j++)
    {
      tmp = tmp + ligne[j];
    }

  return tmp;
}

/*imprimer une chaîne*/
void imprime_chaine(char u[MAX])
{
  int i=0;
  while ((i<MAX) && (u[i]!='\0'))
  {
    putchar(u[i]);
    i++;
  }
  putchar('\n');
}

/*exo 1c page v <-- ru */
void concatener_chaines(char r[MAX], char u[MAX], char v[MAX] )
{
  int i=0;
  int d;

  while (r[i] != '\0')
    {
      v[i] = r[i];
      i++;
    }
  d = i; /*stocke le décalage*/
  i=0;
  
  while (u[i] != '\0')
    {
      v[d+i] = u[i];
      i++;
    } 
  v[d+i]='\0';
}

int main(void)
{
  int r;int t[LMAX][CMAX]; int l[CMAX]; int c[LMAX];char v[MAX];

  srand(time(NULL));
  initialise_hasard(t);
  r = sommetab(t,l,c);
  printf("Somme : %d\n",r);
  imprime_tableau(t);

  concatener_chaines("hop","la",v);
  imprime_chaine(v);

  return 0;
}

