Write declarations for the following
a pointer to a character;
an array of 10 integers;
a refernce to an array of 10 integers;
a pointer to an array of character strings;
a pointer to a pointer to a character;
a constant integer;
a pointer to a constant integer;
a constant pointer to an integer.
Initialize each one.
answer
answer:
#include<iostream>
#include<string>
using namespace std;
int main()
{
char c='s';
char *p=&c;
int z[10]={1,2,3,4,5,6,7,8,9,0};
int (&re)[10]=z;
string st[15];
string *str=st;
char **po=&p;
const int i=15;
const int *q=&i;
int *const temp=z;
return 0;
}
Give the function prototype for each of the following:
Function hypotenuse that takes two double-precision, floating-point arguments, side1 and side2, and returns a double-precision, floating-point result.
b) Function smallest that takes three integers, x, y and z, and returns an integer.
c) Function instructions that does not receive any arguments and does not return a value.
d) Function intToDouble that takes an integer argument, number, and return a double-precision, floating-point result.
answer
double hypotenuse(double,double );
int smallest(int ,int ,int );
void instructions (void);
double intToDouble(int );
2. Find the error in each of following program segments, and explain how the error can be corrected.
a) int g(void) {
cout << “Inside function g” << endl;
int h(void) {
cout << “Inside function h” << endl;
}
}
b) int sum(int x, int y) {
int result;
result = x + y;
}
c) int sum(int n) {
if (n == 0)
return 0;
else
n + sum(n -1);
}
d) void f(double a);
{
float a;
cout << a << endl;
}
e) void product(void) {
int a, b, c;
int result;
cout << “Enter three integers: “;
cin >> a >> b >> c;
result = a * b * c;
cout << “Result is “ << result;
return result;
}
answer
a)错误。函数h在函数g里定义,将函数h在函数g外定义。
b)错误,没有返回值。最后加上return (result);
c)错误,函数没有返回n+sum(n-1)。加上return n+=sum(n-1);
d)错误,参数列表右边多了;,函数内重新定义参数a。删除;和float a;。
e)错误,void类型不返回值,删除return语句。