You are not logged in.

  • Login

gast123

Unregistered

1

Sunday, October 2nd 2011, 1:06pm

Strukturen

Hallo,

ich hab ein Programm geschrieben, dass einen sinus ausrechnet.

Ich möchte es in einem Unterprogramm machen und dann die Struct-variable zurückgeben, das ich dann im Hauptprogramm ausgeben.

Zusätzlich sollen x, y in ein Array gespeichert werden.

Irgendwie hats das was, wenn ich gleich im Unterprogramm ausgebe das versteh ich bzw. da klappts.

Schaut es euch bitte mal an.

[cpp]
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#define MAXLEN 10


struct punkt_struct
{
float x;
float y;
};

typedef struct punkt_struct punkt;

punkt getpunkte(punkt point[], int len);
//void fndminimum(punkt point[], int len);

punkt getpunkte(punkt point[], int len)
{
double t=0;



for(t=0.0; t<len; t=t+0.1)
{
point[(int)t].x=(float)t;
point[(int)t].y=sin((float)t);
//printf("sin(%f)=%f\n",point[(int)t].x,point[(int)t].y);
}

return(point[MAXLEN]);
}

//void fndminimum(punkt point[], int len)
//{
//
//}

int main()
{
double t=0;

punkt allpoints[MAXLEN];
allpoints[MAXLEN]=getpunkte(allpoints, MAXLEN);



return(0);
}

Wenn ich mit Einzelschritt durchgehe dann kommt es in der 2. Zeile der for()-schleife net mehr weiter.
[/cpp]

MfG

gast123

Unregistered

2

Sunday, October 2nd 2011, 1:07pm

Sorry.

Hier der schöne Code^^:

C/C++ Quellcode

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
46
47
48
49
50
51
#include <stdlib.h>
 #include <stdio.h>
 #include <math.h>
 #include <string.h>
 
 #define MAXLEN 10
 
 
 struct punkt_struct
 {
 float x;
 float y;
 };
 
 typedef struct punkt_struct punkt;
 
 punkt getpunkte(punkt point[], int len);
 //void fndminimum(punkt point[], int len);
 
 punkt getpunkte(punkt point[], int len)
 {
 double t=0;
 
 
 
 for(t=0.0; t<len; t=t+0.1)
 {
 point[(int)t].x=(float)t;
 point[(int)t].y=sin((float)t);
 //printf("sin(%f)=%f\n",point[(int)t].x,point[(int)t].y);
 }
 
 return(point[MAXLEN]);
 }
 
 //void fndminimum(punkt point[], int len)
 //{
 // 
 //}
 
 int main()
 {
 double t=0;
 
 punkt allpoints[MAXLEN];
 allpoints[MAXLEN]=getpunkte(allpoints, MAXLEN);
 
 
 
 return(0);
 }

3

Monday, October 3rd 2011, 1:59pm

C/C++ Quellcode

1
punkt allpoints[MAXLEN];

Definition einer Struktur Instanze/Variable als Array mit MAXLEN=10 Elementen. -> Ok


C/C++ Quellcode

1
allpoints[MAXLEN]=getpunkte(allpoints, MAXLEN);

Zuweissung des Ruckgabewertes der Funktion an das (MAXLEN=) 10 Element des Arrays allpoints.
-> Fehler, das Array hat zwar 10 Elemente, jedoch zählt man in C++ bei Array mit 0 beginnend, dh. das 10 Element ist in Wirklichkeit das 9.


C/C++ Quellcode

1
return(point[MAXLEN]);

Hier auch, Zugriff auf das 10 Element welches in diesem Fall, aufgrund des Übergabeparameters point <-> allpoints, wiederum nicht zur Verfügung steht.


C/C++ Quellcode

1
2
3
4
5
for(double t=0.0; t<len; t=t+0.1)
{
   point[(int)t].x = (float)t;
   ...
}

Wenn Len=MAXLEN=10 ist, und du in 0.1 Schritten druchläufst, heisst dies du möchtest 100 Elemente in das 10-Elemente-Array quetschen?
Ist dir klar das durch eine Typenkonventierung von double in int die Komastellen abgeschnitten werden? -> (int)0.5 = 0 -> Du schreibst also mehrmals an die selbe Stelle des Arrays einen anderen Wert rein.


Wie wärs mal mit:
- MAXLEN erhöhen um mehr Werte Speichern zu können
- Den Rückgasbetyp der getpunkte Funktion entfernen; dass direkt schreiben in das Array als Parameter bwewirkt so schon, dass die Werte dann automatisch nach Verlassen der Funktion in der Übergebenden Variable allpoints gespeichert sind.
- Entweder nur Englisch oder nur Deutsch verwenden

Mfg Rushh0ur

Similar threads

Tagging

Social bookmarks