oop test

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语句。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值