Probolem with returning array values from finction.

M

morgan

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#define re 1 //realis
#define im 2 //imaginalis

typedef int complex;

complex F1 (complex x[2],complex y[2]){
cout<<"Dodawanie liczb zespolonych"<<
"("<<x[re]<<"+"<<x[im]<<"i)+"<<"("<<y[re]<<"+"<<y[im]<<"i)=";
complex z[2];
z[re]=x[re]+y[re];
z[im]=x[im]+y[re];
return z;} // <========Here



void main()
{clrscr();
complex x[2],y[2];
cout<<"Podaj liczbe rzeczywista do pierwszej liczby zespolnoej x+yi x= ";
cin>>x[re];
cout<<"Podaj liczbe urojon¡ do pierwszej liczby zespolnoej x+yi y= ";
cin>>x[im];
cout<<"Podaj liczbe rzeczywista do drugiej liczby zespolnoej x+yi x= ";
cin>>y[re];
cout<<"Podaj liczbe urojon¡ do drugiej liczby zespolnoej x+yi y= ";
cin>>y[im];
complex z[2];
z=F1(x,y); // <========And here


cout<<"("<<z[re]<<"+"<<z[im]<<"i)"<<endl;
getchar();
}
 
P

Peter Koch Larsen

morgan said:
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#define re 1 //realis
#define im 2 //imaginalis

typedef int complex;

complex (most surprisingly!) is an int.


complex F1 (complex x[2],complex y[2]){
cout<<"Dodawanie liczb zespolonych"<<
"("<<x[re]<<"+"<<x[im]<<"i)+"<<"("<<y[re]<<"+"<<y[im]<<"i)=";
complex z[2];
z[re]=x[re]+y[re];
z[im]=x[im]+y[re];
return z;} // <========Here
Here you return what effectively is a pointer to int.
Why not use the built-in complex?

/Peter
[snip]
 
T

Tim Love

Your F2 function returns a pointer (though your code makes it look as
if you expect it to return something else) to an area of memory that
is freed when the function finishes. The area of memory will be
re-used sooner or later.

Use C++'s complex numbers, or "new", etc.

complex z[2];
This makes z a const. Maybe you mean "complex *z"? But you need to change
other things too.
 
D

Dave O'Hearn

morgan said:
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#define re 1 //realis
#define im 2 //imaginalis

This part is wrong. Arrays start at 0, not 1, so you need,

#define re 0 //realis
#define im 1 //imaginalis

For the rest, I'd consider using std::complex<int>. You could also make
your own data structure holding a real and imaginary, but std::complex
will do this for you.
 
E

E. Robert Tisdale

Peter said:
complex (most surprisingly!) is an int.

In the context of a computer programming language
for finite precision digital computers,
the terms complex and real simply distinguish subsets
of the real number line and the complex plane respectively.
 
R

Richard Herring

Dave said:
This part is wrong. Arrays start at 0, not 1, so you need,

#define re 0 //realis
#define im 1 //imaginalis

For the rest, I'd consider using std::complex<int>.

I emphatically wouldn't.

26.2/2: The effect of instantiating the template complex for any type
other than float, double or long double is unspecified.
You could also make
your own data structure holding a real and imaginary, but std::complex
will do this for you.
But complex<int> may behave in ways you wouldn't expect.
 
P

Peter Koch Larsen

E. Robert Tisdale said:
In the context of a computer programming language
for finite precision digital computers,
the terms complex and real simply distinguish subsets
of the real number line and the complex plane respectively.

Is that so? My belief was that seeing the typename "complex" in any
programming language was a hint that there would be a real and an imaginary
part.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top