Registrierung Kalender Mitgliederliste Teammitglieder Suche Häufig gestellte Fragen Zur Startseite

Informatiker Board » Themengebiete » Praktische Informatik » Vektoren vergleichen in C » Antwort erstellen » Hallo Gast [Anmelden|Registrieren]

Antwort erstellen
Benutzername: (du bist nicht eingeloggt!)
Thema:
Nachricht:

HTML ist nicht erlaubt
BBCode ist erlaubt
Smilies sind erlaubt
Bilder sind erlaubt

Smilies: 21 von 33
smileWinkDaumen hoch
verwirrtAugenzwinkerngeschockt
Mit ZungeGottunglücklich
Forum Kloppebösegroßes Grinsen
TanzentraurigProst
TeufelSpamWillkommen
LehrerLOL HammerZunge raus
Hilfe 
aktuellen Tag schließen
alle Tags schließen
fettgedruckter Textkursiver Textunterstrichener Text zentrierter Text Hyperlink einfügenE-Mail-Adresse einfügenBild einfügen Zitat einfügenListe erstellen CODE einfügenPHP CODE farbig hervorheben
Spamschutz:
Text aus Bild eingeben
Spamschutz

Die letzten 10 Beiträge
Tergo

Habe jetzt doch mal lieber Double eingesetzt, sicher ist sicher Daumen hoch

& Vielen, vielen Dank für deine Hilfe smile
eulerscheZahl

Sollte passen.
Tergo

habt int bei amountv1 und amount v2 durch float ersetzt und hier und da etwas entschlackt:

#include <stdio.h>
#include <math.h>

int compVecLen(int v1[], int v2[]);
int main()

{

int result;
int V1[3];
int V2[3];

printf("Enter the values of Vector 1");
scanf("%d",&V1[0]);
scanf("%d",&V1[1]);
scanf("%d",&V1[2]);

printf("Enter the values of Vector 2");
scanf("%d",&V2[0]);
scanf("%d",&V2[1]);
scanf("%d",&V2[2]);

result=compVecLen(V1,V2);

if(result == 0)
{
printf("|V1| = |V2|");
}
else if(result == 1)
{
printf("|V1| > |V2|");
}
else
{
printf("|V1| < |V2|");
}

return 0;

}

int compVecLen(int v1[], int v2[])

{

double amountv1=sqrt(v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2]);
double amountv2=sqrt(v2[0]*v2[0] + v2[1]*v2[1] + v2[2]*v2[2]);

if(amountv1<amountv2)
{
return -1;
}

if(amountv1>amountv2)
{
return 1;
}

return 0;

}

Nun erhalte ich auch für 1,2,2 und 2,2,2 das richtige Ergebnis.
eulerscheZahl

Nein.
Nimm mal v1 = (1 2 2) und v2 = (2 2 2) und schau, was rauskommt.
Tergo

Hallo, habe es nun angepasst:

#include <stdio.h>
#include <math.h>

int compVecLen(int v1[], int v2[]);
int result;
int main()

{

int V1[3];
int V2[3];

printf("Enter the values of Vector 1");
scanf("%d",&V1[0]);
scanf("%d",&V1[1]);
scanf("%d",&V1[2]);

printf("Enter the values of Vector 2");
scanf("%d",&V2[0]);
scanf("%d",&V2[1]);
scanf("%d",&V2[2]);

result=compVecLen(V1,V2);

if(result == 0)
{
printf("|V1| = |V2|");
}
else if(result == 1)
{
printf("|V1| > |V2|");
}
else
{
printf("|V1| < |V2|");
}

return 0;

}

int compVecLen(int v1[], int v2[])

{

int amountv1=sqrt(v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2]);
int amountv2=sqrt(v2[0]*v2[0] + v2[1]*v2[1] + v2[2]*v2[2]);

if(amountv1<amountv2)
{
return -1;
}

if(amountv1>amountv2)
{
return 1;
}

if(amountv1==amountv2)
{
return 0;
}

}

Habe das sqrt jetzt doch mit reingenommen, sicher ist sicher, wegen Aufgabenstellung. Das müsste jetzt so passen, oder?
eulerscheZahl

Die Wurzel brauchst du nicht, wenn du nur schauen willst, was größer ist. Die Wurzelfunktion ist nämlich streng monoton und kostet nur unnötig Rechenzeit.

Bei dir liegt einiges im Argen:
Unnötigerweise globale Variablen
X1=V1[0]; gehört genau anders herum, wenn im Vektor die Nutzereingabe stehen soll.
Die Variablen c und d sind nicht explizit initialisiert. Da global, sind sie mit 0 vorbelegt. Das heißt, du gibst in jedem Fall 0 zurück

code:
1:
2:
3:
4:
5:
if(result==c)
{
printf("|V1| < |V2|");
}
else

das else wird auch ausgeführt, wenn result == d ist.
Tergo RE: Vektoren vergleichen in C

Hallo,

vielen Dank, das ist es ja bereits (außer das sqrt bei der Berechnung).
Ich bin selber bis hierhin gekommen:

#include <stdio.h>
#include <math.h>

int compVecLen(int v1[], int v2[]);
char result;
int X1,X2,X3,Y1,Y2,Y3;
int A1,A2,A3,B1,B2,B3;
int c,d;
int amountv1, amountv2;
int main()

{

int V1[3]={X1,X2,X3};
int V2[3]={Y1,Y2,Y3};

printf("Enter the values of Vector 1");
scanf("%d",&X1);
scanf("%d",&X2);
scanf("%d",&X3);

printf("Enter the values of Vector 2");
scanf("%d",&Y1);
scanf("%d",&Y2);
scanf("%d",&Y3);

X1=V1[0];
X2=V1[1];
X3=V1[2];
Y1=V2[0];
Y2=V2[1];
Y3=V2[2];

result=compVecLen(V1,V2);

if(result==d)
{
printf("|V1| > |V2|");
}
if(result==c)
{
printf("|V1| < |V2|");
}
else
{
printf("|V1| = |V2|");
}

return 0;

}

int compVecLen(int v1[3], int v2[3])

{

int amountv1=sqrt(X1*X1+X2*X2+X3*X3);
int amountv2=sqrt(Y1*Y1+Y2*Y2+Y3*Y3);

if(amountv1 < amountv2)
{
return c;
}

if(amountv1 > amountv2)
{
return d;
}

else
{
return 0;
}

}


Kannst du mir verraten, was hier im Gegensatz zu deinem Ergebnis falsch lief?
eulerscheZahl

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
#include <stdio.h>

int compVecLen(int v1[], int v2[]);
int main()
{
	int v1[3];
	int v2[3];
	int result;

	printf("Enter the values of Vector 1: ");
	scanf("%d", &v1[0]);
	scanf("%d", &v1[1]);
	scanf("%d", &v1[2]);

	printf("Enter the values of Vector 2: ");
	scanf("%d", &v2[0]);
	scanf("%d", &v2[1]);
	scanf("%d", &v2[2]);

	result=compVecLen(v1, v2);

	if (result == 0) printf("Both vectors have the same length\n");
	else if (result == 1) printf("The larger vector is v1\n");
	else printf("The larger vector is v2\n");
	return 0;

}

int compVecLen(int v1[], int v2[])
{
	int len1Sqr = v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2];
	int len2Sqr = v2[0]*v2[0] + v2[1]*v2[1] + v2[2]*v2[2];
	if(len1Sqr < len2Sqr)
	{
		return -1;
	}
	if(len1Sqr > len2Sqr)
	{
		return 1;
	}
	if(len1Sqr == len2Sqr)
	{
		return 0;
	}
}
Tergo

Bin jetzt (glaube ich) noch ein bisschen weitergekommen, allerdings erhalte ich eine Fehlermeldung bezüglich eines Syntax-Errors verwirrt

#include <stdio.h>
#include <math.h>

int compVecLen(int v1[], int v2[]); char result;
int X1,X2,X3,Y1,Y2,Y3;
int A1,A2,A3,B1,B2,B3;
int V1,V2;
int v1,v2;
int c,d;
int amountv1, amountv2;
int main()

{

int V1[3]={X1,X2,X3};
int V2[3]={Y1,Y2,Y3};

printf("Enter the values of Vector 1");
scanf("%d",&X1);
scanf("%d",&X2);
scanf("%d",&X3);

printf("Enter the values of Vector 2");
scanf("%d",&Y1);
scanf("%d",&Y2);
scanf("%d",&Y3);


result=compVecLen(V1,V2);

if(result=d)
{
printf("|V1| > |V2|");
}
if(result=c)
{
printf("|V1| < |V2|");
}
else
{
printf("|V1| = |V2|");
}

return 0;

}

int compVecLen(int v1,v2)

{

int v1[3]={A1,A2,A3};
int v2[3]={B1,B2,B3};

amountv1=sqrt(A1*A1+A2*A2+A3*A3);
amountv2=sqrt(B1*B1+B2*B2+B3*B3);

if(amountv1<amountv2)
{
return c;
}

if(amountv1>amountv2)
{
return d;
}

else
{
return 0;
}

}
Tergo

Hallo eulerscheZahl,

vielen Dank für die schnelle Rückmeldung!

Habe jetzt den Math-Fehler schon mal beseitigt und die eigentliche Rechnung mit in die compVecLen Funktion geschrieben. In der main habe ich jetzt noch if-Statements hinzugefügt, damit die Aufgabenstellung besser passt.

Habe die Variablen X1,X2,... als Integer oben deklariert, genauso wie v1 und v2.

Wenn aber im Array keine Variable eingesetzt werden kann, wie gehe ich dann vor?

#include <stdio.h>
#include <math.h>

int compVecLen(int v1[], int v2[]); char result;
int X1,X2,X3,Y1,Y2,Y3;
int v1,v2;
int main()

{

int v1[3]={X1,X2,X3};
int v2[3]={Y1,Y2,Y3};

printf("Enter the values of Vector 1");
scanf("%d",&X1);
scanf("%d",&X2);
scanf("%d",&X3);

printf("Enter the values of Vector 2");
scanf("%d",&Y1);
scanf("%d",&Y2);
scanf("%d",&Y3);


result=compVecLen();

if(result=v2)
{
printf("|V1| > |V2|");
}
if(result=v1)
{
printf("|V1| < |V2|");
}
else
{
printf("|V1| = |V2|");
}

return 0;

}

int compVecLen(int v1[3]={X1,X2,X3}, int v2[3]={Y1,Y2,Y3})

{

amountv1=sqrt(X1*X1+X2*X2+X3*X3);
amountv2=sqrt(Y1*Y1+Y2*Y2+Y3*Y3);

if(amountv1<amountv2)
{
return v1;
}

if(amountv1>amountv2)
{
return v2;
}

else
{
return 0;
}

}
Es sind weitere Beiträge zu diesem Thema vorhanden. Klicken Sie hier, um sich alle Beiträge anzusehen.