Ultima modifica:
Salve. Il mio esercizio mi dice di leggere da tastiera due numeri h e k e di inizializzare h vettori di lunghezza k. poi mi chiede di stampare due coppie di vettori e devono essere le due coppie che hanno il minor numero di elementi diversi nella stessa posizione e di mettere tra parentesi gli elementi diversi. [Esempio: con input h=3, k=6, 1 2 3 2 3 4 1 2 4 2 4 4 1 3 3 1 3 4 dovrebbe stampare
1 2 (3) 2 (3) 4 - 1 2 (4) 2 (4) 4
1 (2) 3 (2) 3 4 - 1 (3) 3 (1) 3 4]
ll problema è che il mio compilatore stampa l'output corretto e anche alcuni compilatori trovati sul web, ma il mio professore dice che con lo stesso input ha quest'output:
gcc -o aaa 1969731.1.c
./aaa
3 6 1 2 3 2 3 4 1 2 4 2 4 4 1 3 3 1 3 4
(49725488) (24576) (3) 2 1 (2) - (1) (2) (4) 2 1 (3)
(49725488) (24576) 3 (2) (1) (2) - (1) (3) 3 (1) (3) (4)
lui dovrebbe usare gcc come compilatore. Qualcuno può aiutarmi?
1 2 (3) 2 (3) 4 - 1 2 (4) 2 (4) 4
1 (2) 3 (2) 3 4 - 1 (3) 3 (1) 3 4]
ll problema è che il mio compilatore stampa l'output corretto e anche alcuni compilatori trovati sul web, ma il mio professore dice che con lo stesso input ha quest'output:
gcc -o aaa 1969731.1.c
./aaa
3 6 1 2 3 2 3 4 1 2 4 2 4 4 1 3 3 1 3 4
(49725488) (24576) (3) 2 1 (2) - (1) (2) (4) 2 1 (3)
(49725488) (24576) 3 (2) (1) (2) - (1) (3) 3 (1) (3) (4)
lui dovrebbe usare gcc come compilatore. Qualcuno può aiutarmi?
C:
/*Homework 3 es 1*/
#include <stdio.h>
#include <stdlib.h>
int leggi_dato (int);
int main ()
{ int h,k,i,j,m,n,p,z1,y1,z2,y2;
h=leggi_dato(h);
k=leggi_dato(k);
int **arrays;
arrays=malloc(k);
for (i=0; i<h; i++) {arrays[i]=malloc(k);
for (j=0; j<k; j++) scanf ("%d", &arrays[i][j]);}
int x=k, t=k;
for (i=0; i<h; i++)
{for (j=i+1; j<h; j++)
{p=0; for (m=0; m<k; m++)
{if (arrays[i][m]!=arrays[j][m]) p++;}
if (p<x) {y2=y1; z2=z1; y1=i; z1=j; x=p;}
else if (p<t){y2=i; z2=j; t=p;}}}
if ((y1==0)&&(y2==0)&&(z1==0)&&(z2==0)) {y1=0; y2=0; z1=1; z2=2;}
for (n=0; n<k; n++) {
if (arrays[y1][n]==arrays [z1][n]) {
printf ("%d\t", arrays[y1][n]);}
else printf ("(%d)\t", arrays[y1][n]);}
printf ("-\t");
for (n=0; n<k; n++) {
if (arrays[y1][n]==arrays [z1][n]) printf ("%d\t", arrays [z1][n]);
else printf ("(%d)\t", arrays[z1][n]);}
printf ("\n\n");
for (n=0; n<k; n++) {
if (arrays[y2][n]==arrays [z2][n]) {
printf ("%d\t", arrays[y2][n]);}
else printf ("(%d)\t", arrays[y2][n]);}
printf ("-\t");
for (n=0; n<k; n++) {
if (arrays[y2][n]==arrays [z2][n]) printf ("%d\t", arrays [z2][n]);
else printf ("(%d)\t", arrays[z2][n]);}
return 0;
}