// 两个城镇之间的距离是 380 公里。一辆汽车和一辆货车同时从两个城镇出发。两辆车以什么速度行驶,如果汽车的速度比卡车的速度快5公里,我们知道他们在4小时后相遇?在计算和显示所需信息时使用动态内存分配和指针。
// The distance between two towns is 380km. A car and a lorry started from the two towns at the same time. At what speed drove the two vehicles, if the speed of the car is with 5km\ faster than the speed of the lorry and we know that they met after 4 hours? Use Dynamic Memory Allocation and Pointer in calculating and Displaying the needed info.
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
int main ()
{
int t=4;
int d=380;
int dt=20;
int dd;
int * ptr;
int * ptr2;
ptr = (int*) malloc (500*sizeof(int));
ptr2 = (int*) malloc (500*sizeof(int));
dd=d-dt;
ptr = dd/8;
ptr2 = ptr+5;
cout << " The Speed of the Car is: " << ptr << endl;
cout << " The Speed of the Lorry is: " << ptr2 << endl;
return(0);
}
我怎样才能让这个运行?如何在所有变量中使用动态内存分配?谢谢。