[Basic C++] Increasing array size
- 题目描述
- 答题区域
- 提交记录
- 成绩报告
- 排名情况
- 标准答案
- 相关讨论
出题
邮箱
:
:
郑斯达
zhengsd3@mail2.sysu.edu.cn
评测
截止时间
:
:
实时评测
2018-03-17 23:55
空间限制
时间限制
:
:
32MB
1000ms
Description
Increasing array size
Once an array is created, its size is fixed. Occasionally, you need to add more values to an array, but the array is full. In this case, you may create a new larger array to replace the existing array. Write a function with the following header:
int * doubleCapacity(int *list, int size)
The function create a new array that doubles the size of the parameter list, and copy the values in the list to the new array, the remaining value should to zero.
For example, the following codes outputs: 1 2 3 4 5 0 0 0 0 0
int list[5]={1,2,3,4,5};
int *newlist = doubleCapacity(list,5);
for(int i=0;i < 2*5;i++)
cout << *(newlist+i) << " ";
Hint
Don't submit the main() function.
标准答案:
-
1001.cpp
1
<span style="color:#75715e">// Problem#: 17754</span>
2
<span style="color:#75715e">// Submission#: 4643990</span>
3
<span style="color:#75715e">// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License</span>
4
<span style="color:#75715e">// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/</span>
5
<span style="color:#75715e">// All Copyright reserved by Informatic Lab of Sun Yat-sen University</span>
6
int * doubleCapacity(int *list, int size) {
* doubleCapacity(int *list, int size) {
7
int *list1 = new int[size * 2];
int *list1 = new int[size * 2];
8
for(int i = 0; i < 2 * size; i++) {
for(int i = 0; i < 2 * size; i++) {
9
if(i < size)
if(i < size)
10
*(list1+i) = *(list + i);
*(list1+i) = *(list + i);
11
else
else
12
*(list1+i) = 0;
*(list1+i) = 0;
13
}
14
return list1;
return list1;
15
}
16