[C] Problem mit Programm(Datenverlust und so...)

C_Boy

Mitglied
Hi leute!

Ich hab wiedermal zur Übung ein Programm geschrieben.
Das mit einer logarithmus Funktion das Array berechnet und dann die anzahl der negativen zahlen ausgibt.
Aber es treten ein paar Fehler wie z.B Index ist kein Ganzahltyp, konventierung von double in float möglicher Datenverlust und soo :(.

Hier das Programm:
Code:
#include <stdio.h>
#include <math.h>

void gensin(double a[], int length); 
int fndmax(double a[], int length);

void gensin(double a[], int length, float x)
{ 
	
  a[length]=log(x); 
}
int fndmax(double a[], int length)
{
  float x;
	int negzahl=0;

	if(a[x] < 0)
	{
	  negzahl++;
	}
	return(negzahl);
}


int main()
{
  double a[3000];
	int length=0;
	float x=0;
	int negzahl=0;
	
	for(x=0; x<=15; x=x+0.5)
	{
	  gensin(a,length,x);
		printf("a[%d]= %lf\n",length,a[length]);
	}

  negzahl=fndmax(a,length);

	printf("Anzahl der negativen Zahlen beträgt:%d",negzahl);

  return(0);
}

LG C_Boy
 
Hi

es wäre hilfreich, wenn du die exakten Fehlermeldungen hier reinkopierst und/oder die betroffenen Zeilen sagst/markierst.

Zum nicht-ganzzahligen Index: Man kann eine Kommazahlen als Arrayindex nehmen. Kein float und kein double.

Zum double in float: Ein double kann größere/genauere Zahlen als float speichern. Wenn du jetzt einen zu großen/zu genuen double-Wert in ein float schreibst, kann es passieren dass was "abgeschnitten" wird.

Was mir sonst noch so auffällt:

In gensin greifst du auf a[length] zu. Ein Array hat die Elemente 0 bis anzahl-1.
Also ein 4 Elemente großes Array hat die Indizes 0-3. Du verwendest aber 4...

Gruß
 
Fehler:
error C2108: Index ist kein Ganzzahltyp(Zeile 17)

Warnungen:
warning C4244: '=': Konvertierung von 'double' in 'float', möglicher Datenverlust(Zeile 32)

warning C4029: Deklarierte Liste formaler Parameter weicht von der Definition ab(Zeile 8)
 
Zuletzt bearbeitet:
Zum Dritten:
C++:
void gensin(double a[], int length); 
...
void gensin(double a[], int length, float x)
{
...
}
Fällt dir was auf? Beim einen schreibst du "float x" dazu, beim anderen nicht.

Zum Ersten: Vermutlich im fndmax diese Zeile:
C++:
if(a[x] < 0)
Ein Array hat Index 0, Index 1, Index 2...aber nicht 1.234
Kommazahlen sind hier falsch, und x ist ein float.

Zum Zweiten: Welche Zeile?
 
Ah gut danke.

Aber jetzt gibt es mir was aus, aber es macht nicht das was es soll.

In Zeile 32 ist ne Warnung:
warning C4244: '=': Konvertierung von 'double' in 'float', möglicher Datenverlust.

Könntest du es vielleicht debuggen oder so?

Programm:
Code:
#include <stdio.h>
#include <math.h>

void gensin(double a[], int length, float x); 
int fndmax(double a[], int length);

void gensin(double a[], int length, float x)
{ 
  a[length]=log(x); 
}
int fndmax(double a[], int length)
{
  int i=0;
	int negzahl=0;

	if(a[i] < 0)
	{
	  negzahl++;
	}
	return(negzahl);
}


int main()
{
  double a[3000];
	int length=0;
	float x=0;
	int i=0;
	int negzahl=0;
	
	for(x=0; x<=15; x=x+0.5)
	{
	  gensin(a,length,x);
		printf("a[%d]= %lf\n",length,a[length]);
	}

  negzahl=fndmax(a,length);

	printf("Anzahl der negativen Zahlen beträgt:%d",negzahl);

  return(0);
}
 
C++:
#include <stdio.h>
#include <math.h>

int main()
{
	double a[100];
	int length=0;
	double x=0;

	int i=0;
	int negzahl=0;

	for(x=0.0; x<=15.00; x+=0.5)
	{
		a[length]=log(x);
        printf("a[%d] = %.2lf = %.2lf\n",length,x,a[length]);
		length++;
    }

	for(i=0;i<length;i++)
	{
		if(a[i]<0.00)negzahl++;
	}

	printf("Anzahl der negativen Zahlen betraegt:%d\n",negzahl);
	return 0;
}
Gruß
 
Ein float ist mindestens 4 Bytes lang, ein double ist mindestens 8 Bytes lang. Logischerweise ist es daher möglich eine Zahl zu finden, die so gross ist, dass sie so viele Bytes an Speicherplatz benötigt, dass sie in ein double noch reinpasst, aber in ein float nicht mehr. Wenn du diese Zahl jetzt einem float zuordnest musst du irgendwie 4 Bytes los werden, damit es in ein float passt. Ich bin mir nicht sicher, ob der Standard etwas darüber aussagt, wie das passiert, aber du solltest grundsätzlich mal davon ausgehen, dass hier einfach irgendwas zufälliges passiert. In jedem Fall findet ein “Datenverlust” statt, wie es dir die Warnung sagt.

Problem ist folgende Zeile:
for(x=0; x<=15; x=x+0.5)

0.5 ist ein Wert, der automatisch in ein double konvertiert wird. Aus diesem Grund wird in der Anweisung (x + 0.5) auch x als double aufgefasst und wir haben (double + double). Das Resultat dieser Anweisung ist wieder ein double, daher ergibt sich folgendes: x = double. x ist aber ein float und es passiert genau das, was ich oben beschrieben habe. Wenn du dein 0.5 nicht als double sondern als float auffassen möchtest, so müsstest du schreiben:
for(x=0; x<=15; x=x+0.5f)
 
Andere Lösung: Konsequent double oder float verwenden.
Du mischt beide Typen wahllos durch. Warum?
 
Jup, stimmt, da habe ich etwas durcheinander gebracht. Nutzlos, aber falls es wen interessiert:
There are three floating point types: float, double, and long double. The type double provides at least as much
precision as float, and the type long double provides at least as much precision as double. The set of values of the
type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set
of values of the type long double. The value representation of floating-point types is implementation-defined. Integral
and floating types are collectively called arithmetic types. Specializations of the standard template std::numeric_-
limits (18.2) shall specify the maximum and minimum values of each arithmetic type for an implementation.

PS:
Wenn ich deine anderen 33 Postings so lese solltest du meiner bescheidenen Meinung nach bisschen an deiner Ausdrucksweise arbeiten. Ein wesentlicher Teil von Sozialkompetenz lässt sich mit den Worten "Man kann recht haben und recht haben." umschreiben.
 
Zurück