0 / 0 / 1
Регистрация: 25.04.2013
Сообщений: 67
|
|
1
|
Переделать код так, чтобы использовались диапазоны значений с помощью указателей
10.05.2013, 13:18. Показов 778. Ответов 1
Мне уже стыдно сюда писать ей богу  . Но есть задача переделать Код№1 так чтобы использовались диапазоны значений с помощью указателей. А функция fill_array() должна возвращать следующий адрес после последнего введенного значения, и это значение должны принимать другие функции как количество элементов функций которые они должны обработать. Код№2 это что я пробовал, но там выводятся адреса вместо значений, в общем все неправильно.
Код1
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
| // arrfun3.cpp -- array functions and const
#include <iostream>
const int Max = 5;
// function prototypes
int fill_array(double ar[], int limit);
void show_array(const double ar[], int n); // don't change data
void revalue(double r, double ar[], int n);
int main()
{
using namespace std;
double properties[Max];
int size = fill_array(properties, Max);
show_array(properties, size);
if (size > 0)
{
cout << "Enter revaluation factor: ";
double factor;
while (!(cin >> factor)) // bad input
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; Please enter a number: ";
}
revalue(factor, properties, size);
show_array(properties, size);
}
cout << "Done.\n";
// cin.get();
// cin.get();
return 0;
}
int fill_array(double ar[], int limit)
{
using namespace std;
double temp;
int i;
for (i = 0; i < limit; i++)
{
cout << "Enter value #" << (i + 1) << ": ";
cin >> temp;
if (!cin) // bad input
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated.\n";
break;
}
else if (temp < 0) // signal to terminate
break;
ar[i] = temp;
}
return i;
}
// the following function can use, but not alter,
// the array whose address is ar
void show_array(const double ar[], int n)
{
using namespace std;
for (int i = 0; i < n; i++)
{
cout << "Property #" << (i + 1) << ": $";
cout << ar[i] << endl;
}
}
// multiplies each element of ar[] by r
void revalue(double r, double ar[], int n)
{
for (int i = 0; i < n; i++)
ar[i] *= r;
} |
|
Код2
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
| const int Max = 5;
// function prototypes
int * fill_array(int * begin, int * end);
void show_array(int * begin, int * end); // don't change data
void revalue(int r, int * begin, int * end);
int main()
{
using namespace std;
int properties[Max];
int * size = fill_array(properties, properties + Max);
show_array(properties, size);
if (size > 0)
{
cout << "Enter revaluation factor: ";
int factor;
while (!(cin >> factor)) // bad input
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; Please enter a number: ";
}
revalue(factor, properties, size);
show_array(properties, size);
}
cout << "Done.\n";
// cin.get();
// cin.get();
return 0;
}
int * fill_array(int * begin, int * end)
{
using namespace std;
int * temp;
for (begin; begin < end; begin++)
{
cout << "Enter value #" << begin << ": ";
cin >> begin;
if (!cin) // bad input
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated.\n";
break;
}
else if (temp < 0) // signal to terminate
break;
}
return begin + 1;
}
// the following function can use, but not alter,
// the array whose address is ar
void show_array(int * begin, int * end)
{
using namespace std;
for (begin; begin < end; begin++)
{
cout << "Property #" << (begin + 1) << ": $";
cout << begin << endl;
}
}
// multiplies each element of ar[] by r
void revalue(int r, int * begin, int * end)
{
for (begin; begin<end; begin++)
*begin *= r;
} |
|
Добавлено через 3 часа 36 минут
ап
Добавлено через 15 часов 22 минуты
Так что, это нельзя реализовать данным методом?
0
|