Advertisement
VRonin

Coppie Random

Feb 1st, 2012
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. //#include <string.h>
  5. struct coppia{
  6. char L;
  7. int N;
  8. };
  9.  
  10. struct coppia CreaCoppia(char l, int n){
  11. struct coppia result;
  12. result.L='X';
  13. result.N=-1;
  14. if (l<'A' || l>'F' || n<0 || n>9) return result;
  15. result.L=l;
  16. result.N=n;
  17. return result;
  18. }
  19.  
  20. int main(){
  21. struct coppia matrice[4][4];
  22. srand(time(NULL));
  23. char l;
  24. int i,j;
  25. /*punto 1*/
  26. for (i=0;i<4;i++){
  27. for (j=0;j<4;j++){
  28. printf("\nInserisci una lettera tra A e F: "); do{scanf("%c",&l); fflush(stdin);}while(l<'A' || l>'F');
  29. matrice[i][j]=CreaCoppia(l, rand()%9);
  30. }
  31. }
  32. /*punto 2*/
  33. printf("\nMatrice creata:");
  34. for (i=0;i<4;i++){
  35. printf("\n");
  36. for (j=0;j<4;j++){
  37. printf("<%c %d>\t", (*(matrice+i)+j)->L ,(*(matrice+i)+j)->N);
  38. }}
  39. /*punto 3*/
  40. printf("\nConcatenazione e Somma:\n");
  41. char concat[5];
  42. int somma;
  43. concat[4]='\0';
  44. for (i=0;i<4;i++){
  45. somma=0;
  46. for (j=0;j<4;j++){
  47. concat[j]=(*(matrice+j)+i)->L;
  48. somma+=(*(matrice+j)+i)->N;
  49. }
  50. printf("<%s %d>\t", concat, somma);
  51. }
  52. /*punto 4*/
  53. int D=16;
  54. do{
  55. if(D<15 || D>20) printf("\nInput errato!");
  56. printf("\nInserisci un numero tra 15 e 20: ");
  57. scanf("%d",&D); fflush(stdin);
  58. }while (D<15 || D>20);
  59. /*punto 5*/
  60. printf("\nmatrice Filtrata:");
  61. for (i=0;i<4;i++){
  62. printf("\n");
  63. for (j=0;j<4;j++){
  64. if ((11-(int)'A' +(*(matrice+i)+j)->N + (int) (*(matrice+i)+j)->L)<D)
  65. printf("<%c %d>\t", (*(matrice+i)+j)->L ,(*(matrice+i)+j)->N);
  66. else printf("X\t");
  67. }}
  68. printf("\nPremi invio per terminare...");
  69. getchar(); //mette in pausa
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement