(eine neuere Version meines Programms ist direkt am Ende)Vergleicht man zwei Arrays von ganzen Zahlen und Rückgabe des Index (C)
Ich brauche ein Programm mit einer Funktion zu erstellen, die zwei Arrays von ganzen Zahlen vergleichen und gibt die Index der ersten Stelle unterscheiden sie sich.
Ich muss einen Sentinel-Wert verwenden, um das Ende der gültigen Eingabe anstelle von EOF anzuzeigen, da eine Datei nicht zwei Dateiendungen haben kann.
Beispiel meiner Eingabe und Ausgabe für mein Programm gewünscht:
Eingang 1:
3 4 5
3 4 6
Ausgang 1:
2
Erläuterung: Die dritten Werte von zwei Arrays verschieden sind, so druckt es den dritten Index (zählt als 0,1,2).
Ich schaute über ähnliche Probleme auf Stackoverflow. Viele Fehler wurden behoben und jetzt kann ich ein Programm kompilieren, ohne mir irgendwelche Fehler oder Warnungen zu geben. Aber es blendet mich, weil ich nicht verstehe, was genau nicht funktioniert.
Mein Problem, dass ich feststellen:
- Wenn ich drücke Taste nach dem ersten Wert eingeben, stoppt das Programm zu arbeiten.
Ich nehme an, dass meine TableDiff-Funktion falsch ist, aber ich habe es aus den Notizen des Lehrers kopiert. Wenn ich keinen Tippfehler mache, sollte es keinen Unterschied geben.
Hier ist meine eingereichten Version:
/*
* A simple program to sort numbers in the correct order
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 10 //max elements in array
#define SENTINEL -999 //indicate the end of valid input
int main() {
int tableFill(int a[], int max);
int tableDiff (const int a[], const int b[], int n, int m);
void tablePrint (const int a[], const int b[], int n, int m);
int a[MAX];
int b[MAX];
int m,n,index;
m = tableFill(a, MAX);
n = tableFill(b, MAX);
tablePrint(a,b,n,m);
index = tableDiff(a,b,m,n);
if(index==-1)
printf("Index is the same");
else
printf("Index is the different");
return 0;
}
// read values from stdin into array up to 'max' values
int tableFill(int a[], int max) {
int r; // input from scanf
int next; // next input value
int cnt = 0; // count of values read
int *ptr; //pointer
printf("Enter the numbers! \n");
while ((r=scanf("%i", &next)) == 1 && next != SENTINEL)
{
if (r == 0) //invalid input data
{
printf("Nonnumeric data entered. Please enter a number. \n");
while (getchar()!= '\n'); // flush invalid data
}
else
*ptr++=cnt;
}
if(r==1) //another value was read but the array is full
printf("Error - too many values. Array size %i.\n", max);
return ptr-a; //(ptrb - b) should return the same value
}
int tableDiff (const int a[], const int b[], int n, int m)
{
const int *ptra = a; //start for 1st array
const int *ptrb = b; //start for 2nd array
const int *endptra = a+m; //end for 1st array
const int *endptrb = b+n; //end for 2nd array
while(ptra<endptra && ptrb<endptrb && *ptra==*ptrb)
{
ptra++;
ptrb++;
}
if(ptra==endptra && ptrb==endptrb)
{
return -1;
}
else
return ptra -a; //(ptrb - b) should return the same value
}
//print all elements in array.
void tablePrint (const int a[], const int b[], int n, int m)
{
int i; //varriable to print
for (i = 0; i < n; i++)
printf ("%d ", a[i]);
printf ("\n");
}
Hier ist meine neuere Version:
Das Programm setzt nun zu arbeiten, bis zweiten Sentinel erreicht (richtig funktioniert).
/*
* A simple program to sort numbers in the correct order
*/
#include <stdio.h>
#define MAX 10 //max elements in array
#define SENTINEL -999 //indicate the end of valid input
int main() {
// read values from stdin into array up to 'max' values
int tableFill(int a[], int max);
//compare two arrays and returns the first subscript they differ
int tableDiff (const int a[], const int b[], int n, int m);
//print all elements in array
void tablePrint (const int a[], int n);
int a[MAX];
int b[MAX];
int m,n,index;
m = tableFill(a, MAX);
n = tableFill(b, MAX);
tablePrint(a,m);
tablePrint(b,n);
index = tableDiff(a,b,m,n);
if(index==-1)
printf("-1. Arrays are the same.");
else
printf ("\n The arrays differ at index '%d'.\n", index);
return 0;
}
// read values from stdin into array up to 'max' values
int tableFill(int a[], int max) {
int r; // input from scanf
int next; // next input value
int cnt = 0; // count of values read
int *ptr = a; //pointer
printf("Enter the numbers! \n");
while ((r=scanf("%i", &next))==0 || (next != SENTINEL))
{
if (r == 0) //invalid input data
{
printf("Nonnumeric data entered. Please enter a number. \n");
while (getchar()!= '\n'); // flush invalid data
}
else if (cnt == max) //another value was read but the array is full
printf("Error - too many values. Array size %i.\n", max);
else {
*ptr++ = next;
++cnt;
}
}
return ptr-a; //(ptrb - b) should return the same value
}
//compare two arrays and returns the first subscript they differ
int tableDiff (const int a[], const int b[], int n, int m)
{
const int *ptra = a; //start for 1st array
const int *ptrb = b; //start for 2nd array
const int *endptra = a+m; //end for 1st array
const int *endptrb = b+n; //end for 2nd array
while(ptra<endptra && ptrb<endptrb && *ptra==*ptrb)
{
ptra++;
ptrb++;
}
if(ptra==endptra && ptrb==endptrb)
{
return -1;
}
else
return ptra -a; //(ptrb - b) should return the same value
}
//print all elements in array
void tablePrint (const int a[], int n)
{
int i; //loop counter
for (i = 0; i < n; i++)
printf ("%d ", a[i]);
printf ("\n");
}
'max! = SENTINEL' soll das' n! = SENTINEL' sein? – kaylum
Ich habe keine Variable "n" in der Funktion tableFill, aber ich habe sie in ** next! = SENTINEL ** geändert. Es würde mehr Sinn machen, da ich den "nächsten" Wert für Sentinel überprüfen möchte. –
Ja, tut mir leid, das war ein Tippfehler. Gemeint "nächstes" nicht "n". – kaylum