Question

here is a working Turbo Pascal example

uses graph, crt;
const xmax=600;
      ymax=400;
type vreal=array[1..xmax] of real;

var
   y:vreal;
   r,h,vmax,vmin,a,b:real;
   i,linie0:integer;
   grDriver: Integer;
   grMode: Integer;
   s:string;
   c:char;
{****************************************}
procedure regimgrafic;
begin
  grDriver := Detect;
  InitGraph(grDriver, grMode,'');
  setcolor(15);
end;
{****************************************}
function f(x:real):real;
 begin
f:=exp(cos(2*x)*ln(x))+3*sin(x);
 end;
{**************************************************************}
procedure calcul(a,b:real;var z:vreal);
begin
  h:=abs(b-a)/(xmax-1);
  for i:=1 to xmax do z[i]:=f(a+(i-1)*h);
end;
{**************************************************************}
procedure normare(var z:vreal; var l0:integer;var vmax,vmin:real);
var
 delta,deplasare:real;
begin
 vmax:=z[1];vmin:=z[1];
 for i:=2 to xmax do
  begin
     if z[i]>vmax then vmax:=z[i];
     if z[i]<vmin then vmin:=z[i];
  end;
      delta:=(vmax - vmin)/(ymax);
      deplasare:=0-vmin;
      l0:=round(ymax-deplasare/delta);
      if vmin>0 then l0:=415;
      if vmax<0 then l0:=10;
  for i:=1 to xmax do
      z[i]:=ymax-(z[i]+deplasare)/delta;
end;
{*******************************************************}
procedure axax(linie0:integer);
begin
 setcolor(11);
 outtextxy(getmaxx-15,linie0-10,'X');
 line(20,linie0, getmaxx-20,linie0);
 h:=(b-a)/2;
 for i:=0 to 2 do
   begin
    fillellipse(20+i*300,linie0,2,2);
    r:=a+i*h;
    str(r:0:2,s);
    outtextxy(20+i*290,linie0+10,s);
   end;
end;

procedure axay;
begin
 setcolor(14);
 outtextxy(10,10,'Y');
  h:=(vmax-vmin);
 for i:=0 to 1 do
   begin
    fillellipse(20,10+i*400,2,2);
    r:=vmax-i*h;
    str(r:0:2,s);
    outtextxy(20,10+i*400,s);
   end;
 line(20,10,20,getmaxy-65);
end;

procedure modeleazagrafic(y:vreal);
begin
 rectangle(1,1,getmaxx,getmaxy);
 setcolor(11);
 axay;
 axax(linie0);
 for i:=1 to xmax do putpixel(i+20,10+round(y[i]),15);
 outtextxy(20,460,'Alt interval (D)a / (N)u');
end;

begin
repeat
clrscr;
write('Introdu extremitatile intervalului: ');
readln(a,b);
calcul(a,b,y);
normare(y,linie0,vmax,vmin);
regimgrafic;
modeleazagrafic(y);
c:=readkey;
closegraph;
until upcase(c)='N';
end.

and here is my c code

#include<graphics.h>
#include<string>
#include<math.h>
#include<iostream>

const int xmax = 600;
      int ymax = 400;

typedef float vreal[600];

using namespace std;

vreal y;
  float  r,h,vmax,vmin,a,b;
  int linie0;
  int  grDriver;
  int grMode;
  string s;
  char c;



void regimgrafic()
{
initwindow(xmax,ymax,"COOL");
setcolor(WHITE);
}


float f(float x)
{
 return exp(cos(2*x)+log(x)+3*sin(x));      
}

void calcul(float a, float b,float* z)
{
      h = fabs(b-a)/(xmax-1);
      for(int i=1;i <=  xmax;i++)
      {
              z[i] = f(a+(i-1)*h);
      }
}

void normare(float *z, int* l0, float *vmin, float *vmax)
{
 float delta, deplasare;

 *vmax = z[1];
 *vmin = z[1];    
     for(int i=2;i<= xmax;i++)
     {
     if(z[i] > *vmax) *vmax = z[i];
     if(z[i] < *vmin) *vmin = z[i];       
     }
   delta = (*vmax - *vmin)/(ymax);
   deplasare = 0-*vmin;
   *l0 = (int)(round(ymax - deplasare/delta));

   //<static_cast><int>()
   if((*vmin)>0) l0 = 415;
   if(*vmax<0) l0 = 10;
   for(int i=1;i<= xmax;i++)
    z[i]=ymax - (z[i]+deplasare)/delta; 
}     

void axax(int linie0)
{
 setcolor(11);
 outtextxy(getmaxx-15,linie0-10,'X');
 line(20,linie0,getmaxx-20,linie0)
 h=(b-a)/2;
 for(int i =0;i<2;i++)
 {
   fillellipse(20,10+i*400,2,2);
   r=vmax-i*h;      
   sprintf(s, "%d", r);
   outtextxy(20+i*290,linie0+10,s);
}     

}


void axay
{
     setcolor(14);
     outtextxy(10,10,'Y');
     h=(vmax-vmin);
     for(int i=0;i<1;i++)
     {
      fillellipse(20,10+i*400,2,2);
      r=vmax-i*h;
      sprintf(s,r);
      outtextxy(20,10+i*400,s);             
     }
}     

void modeleazagrafic(vreal y){
rectangle(1,1,getmaxx,getmaxy);
 setcolor(11);
 axay;
 axax(linie0);
 for(int i=1;i<xmax;i++)
      putpixel(i+20,10+round(y[i]),15);
     outtextxy(20,460,'Alt interval (D)a / (N)u');
}     


int main()
{
do {    
printf("Introduceti extremitatile intervalului: ");
scanf("%f %f",&a,&b);
calcul(a,b,y);
normare(y,linie0,vmax,vmin);
regimgrafic;
modeleazagrafic(y);
c=getch();
closegraph();
}while(toupper(c) == 'N');

}    

I've got a bunch of error, here is some of thementer code here

Line 60:(in normare function) invalid conversion from `int' to `int*' 
line:61 the same error;
line:69 69 pointer to a function used in arithmetic 

please help to fix those error

Was it helpful?

Solution

It's almost like you're not reading the error messages or thinking about the surrounding code.

Those first two relate to these lines:

if((*vmin)>0) l0 = 415;
if(*vmax<0) l0 = 10;

Everywhere else in that function, l0 (which is of type int*) is correctly dereferenced during assignment. But here you are forgetting to. You need to use *l0, not l0. The error is because you are trying to assign an int to a pointer (int*), as the error message quite explicitly states.

The other error is not so obvious, because your code does not show a variable, function or macro named getmaxx... But judging by the error message, it seems that the identifier getmaxx refers to a function. Perhaps you meant getmaxx(), assuming it has no parameters and returns a value. Presumably you have the same problem with getmaxy elsewhere in your code.

Please get in the habit of reading error messages and examining the line of code it refers to. Then think about what it means, and try to take hints from other parts of the code (or even other C programs). If you are not familiar with C, this is a useful way to learn.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top