有人可以帮我解决C ++中关于动态数组的问题吗? [英] Can someone help me figure out this problem about dynamic array in C++?

查看:60
本文介绍了有人可以帮我解决C ++中关于动态数组的问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回答有关动态数组的以下问题:



/ *从输入读取整数序列的函数(序列长度,b

例如,如果长度为3,且数字为

123 345 99则返回的数组大小为3,并存储值123,345和99

@param长度:返回时,存储数组的长度/大小

@return指向数组的指针

* /



int * ReadNumberSequence(int& size)

{

int * array = NULL; //初始化指向NULL的指示

do {

cout<<输入数字序列的长度:;

cin>>大小;

} while(size< = 0);



// Todo:编写一个语句来为数组分配内存。

//注意:我们只知道运行时大小的值,所以我们需要DYNAMICALLY



//为这个数组分配内存



// Todo:写一个循环从输入中读取int的大小#,并将它们保存到//数组





返回阵列;

}



int main()

{

// Todo:delcare必需品变量





// Todo:调用ReadNumberSequence函数读取一系列数字





// Todo:写一个循环到< br $>




显示返回的数组中的元素...

// Todo:释放返回的数组ReadNumberSequence。



}



我的尝试:



这是我到目前为止所做的:



int * ReadNumberSequence(int&尺寸)

{

int * array = NULL; //将指针初始化为NULL

do {

cout<<输入数字序列的长度:;

cin>>大小;

} while(size< = 0);



// Todo:编写一个语句来为数组分配内存。

array = new int [size];



for(int i = 0; i< size; i ++)

{

//我在这里写什么

}



int main()

{



}

解决方案

引用:

//我在这里写什么



嗯......你可以试试这个:

< pre lang =c ++> cin>> array [i];

但我可能会添加一些错误检查以确保用户输入有效数字。您可以非常轻松地检查有效输入:用户输入的整数 - 错误处理 [ ^ ]


Griff为您提供了解决方案。我会给你一个更现代的一瞥 C ++

  #include   <   iostream  >  
#include < 实用程序 >

使用 命名空间标准;

对< int *,size_t> read_sequence()
{
size_t size;
cout<< 请输入数组大小:\ n;
cin>>尺寸;

int * array = new int [size];

for size_t n = 0 ; n< size; ++ n)
{
cout<< 请输入项目<< n<< \ n;
cin>> array [n];
}
return make_pair( array ,size);
}

int main()
{
auto [ array ,size] = read_sequence();
for size_t n = 0 ; n< size; ++ n)
cout<< array [<< n<< ] =<< array [n]<< \ n;

delete [] array ;
}


Answer the following questions about dynamic arrays:

/* A function that reads a sequence of integers from input (with the length of sequence, followed b
For example, if the length is 3, and the numbers are
123 345 99 then the array returned will be of size 3, and stores values 123, 345, and 99
@param length: upon return, stores the length/size of the array
@return the pointer pointing to the array
*/

int * ReadNumberSequence (int & size)
{
int * array = NULL; // initialize the pointer to NULL
do {
cout <<"Enter the length of the number sequence:";
cin >> size;
} while (size<=0);

// Todo: Write a statement to allocate memory for the array.
// Note: we only know the value of size at run time, so we need to DYNAMICALLY

// allocate memory for this array

// Todo: write a loop to read size # of int from input, and save them to the //array


return array;
}

int main()
{
// Todo: delcare necesssary variables


//Todo: call the ReadNumberSequence function to read a sequence of numbers


//Todo: write a loop to


display the elements in the array returned ...
//Todo: free the array returned by ReadNumberSequence.

}

What I have tried:

this is what I did so far:

int * ReadNumberSequence (int & size)
{
int * array = NULL; // initialize the pointer to NULL
do {
cout <<"Enter the length of the number sequence:";
cin >> size;
} while (size<=0);

// Todo: Write a statement to allocate memory for the array.
array = new int [size];

for (int i = 0; i < size; i++)
{
// what do i write here
}

int main ()
{

}

解决方案

Quote:

// what do i write here


Well ... you could try this:

cin>> array[i];

But I'd probably add some error checking to ensure that the user typed a valid number. You can check for valid input very easily: User Input of Integers - Error Handling[^]


Griff gave you the solution. I'll give you a glimpse of more modern C++

#include <iostream>
#include <utility>

using namespace std;

pair< int * , size_t> read_sequence()
{
  size_t size;
  cout << "please enter array size:\n";
  cin >> size;

  int * array = new int[size];

  for (size_t n=0; n<size; ++n)
  {
    cout << "please enter item " << n << "\n";
    cin >> array[n];
  }
  return make_pair( array, size);
}

int main()
{
  auto [ array, size] = read_sequence();
  for ( size_t n = 0; n<size; ++n)
    cout << "array[" << n << "] = " << array[n] << "\n";

  delete [] array;
}


这篇关于有人可以帮我解决C ++中关于动态数组的问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆