C ++数组相对C问题 [英] C++ array relative C question

查看:61
本文介绍了C ++数组相对C问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道该组是针对C而不是C ++,但请尽量帮助我,

如何从屏幕上声明变量值来声明数组的大小?


#include" iostream.h"

#include" iomanip.h"

#include" conio.h"

float ArraySum(float Array [],int ArraySize);

int main()

{

clrscr();

int ArraySize = 0;

//读取数组元素

cout<< 输入数组大小: ;

cin>> ArraySize;

^^^^^^^^^^^^^^^^^^^^^

float数组[ArraySize];

^^^^^^^^^^^^^^^^^^^^^^^

cout<< endl<< 输入数组元素 ;

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

{

cout<< endl<< 输入元素号 << i<< " :" ;;

cin>>数组[i];

}

cout<< endl<< 元素值的总和: << setw(5)<<

ArraySum(Array,ArraySize);

返回0;

}

float ArraySum(float arr [],int n)

{

int total = 0;

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

总计+ = arr [i];

返回总数;

}

I know that group is for C not for C++ but please try to help me ,
How can I declare size of an array as variable value came from screen?

#include "iostream.h"
#include "iomanip.h"
#include "conio.h"
float ArraySum(float Array[],int ArraySize);
int main()
{
clrscr();
int ArraySize=0;
// Read the array elements
cout << "Enter Array Size : " ;
cin >> ArraySize;
^^^^^^^^^^^^^^^^^^^^^
float Array[ArraySize];
^^^^^^^^^^^^^^^^^^^^^^^
cout <<endl<< "Enter Array Elements " ;
for (int i=0;i<=ArraySize;i++)
{
cout <<endl<< "Enter elements no " << i << " : ";
cin >> Array[i];
}
cout <<endl << "The sum of the elements values :" << setw(5) <<
ArraySum(Array,ArraySize);
return 0;
}

float ArraySum(float arr[],int n)
{
int total=0;
for (int i=0;i<=n;i++)
total+=arr[i];
return total;
}

推荐答案

eh ******** ***@yahoo.com (hp******@yahoo.com)写在

新闻:7e ************* *************@posting.google.c om:
eh***********@yahoo.com (hp******@yahoo.com) wrote in
news:7e**************************@posting.google.c om:
我知道该组是针对C而不是C ++,但请尽量帮助我,
如何从屏幕声明变量值来声明数组的大小?
I know that group is for C not for C++ but please try to help me ,
How can I declare size of an array as variable value came from screen?




你不能。你必须malloc()内存并使用指针而不是

数组。例如


float * pArray = malloc(ArraySize * sizeof * pArray);


-

- 马克 - >

-



You cannot. You must malloc() the memory and use a pointer instead of an
array. E.g.

float *pArray = malloc(ArraySize * sizeof *pArray);

--
- Mark ->
--


你好,

Hi there,

我知道group for C for C ++ for c ++但是请尽量帮助我,


问题是:你想做什么?

你想做什么它是用C + +,C89,C99吗?

我们可以帮助你只使用最后两个 - 而且,局部只有

的便携式代码。


我如何声明数组的大小作为变量值来自屏幕?


所有三种语言:malloc()(和free())

C ++:new(和删除)

C89 :malloc()(和free())

C99:malloc()(和free()),可变长度数组

#include" iomanip.h"
#include" conio.h"
float ArraySum(float Array [],int ArraySize);
int main( )
{
clrscr();
int ArraySize = 0;
注意:将size_t用于你想用作数组索引的所有内容//读取数组元素
cout<< 输入数组大小: ;
cin>> ArraySize;
^^^^^^^^^^^^^^^^^^^^^
float数组[ArraySize];
^^^^^^^^^在C99中^^^^^^^^^^^^^^
,此行完全有效

C89:

float *数组;

在main()的开头与其他声明,和

Array = malloc(ArraySize * sizeof * Array);

if(Array == NULL){

/ *句柄错误,通常是退出(EXIT_FAILURE)。 * /

}

和最后 - 或者一旦你不再需要内存

数组现在指向 -

免费(数组); cout<< endl<< 输入数组元素 ;
for(int i = 0; i< = ArraySize; i ++)
{
cout<< endl<< 输入元素号 << i<< " :" ;;
cin>>数组[i];
}
cout<< endl<< 元素值的总和: << setw(5)<<<
ArraySum(Array,ArraySize);
返回0;
}
浮点数ArraySum(float arr [],int n)
{total = 0;
for(int i = 0; i< = n; i ++)
total + = arr [i];
返回总数;
}
I know that group is for C not for C++ but please try to help me ,
The question is: What do you want to do?
Do you want to do it in C++, C89, C99?
We can help you only with the last two -- and, topically, only
with portable code.

How can I declare size of an array as variable value came from screen?
In all three languages: malloc() (and free())
C++: new (and delete)
C89: malloc() (and free())
C99: malloc() (and free()), variable length array

#include "iostream.h"
#include "iomanip.h"
#include "conio.h"
float ArraySum(float Array[],int ArraySize);
int main()
{
clrscr();
int ArraySize=0; note: use size_t for everything you want to use as array index // Read the array elements
cout << "Enter Array Size : " ;
cin >> ArraySize;
^^^^^^^^^^^^^^^^^^^^^
float Array[ArraySize];
^^^^^^^^^^^^^^^^^^^^^^^ in C99, this line is perfectly valid
C89:
float *Array;
at beginning of main() with the other declarations, and
Array = malloc(ArraySize * sizeof *Array);
if (Array == NULL) {
/* Handle error, usually exit(EXIT_FAILURE). */
}
and at the end -- or as soon as you no longer need the memory
Array is now pointing to --
free(Array); cout <<endl<< "Enter Array Elements " ;
for (int i=0;i<=ArraySize;i++)
{
cout <<endl<< "Enter elements no " << i << " : ";
cin >> Array[i];
}
cout <<endl << "The sum of the elements values :" << setw(5) <<
ArraySum(Array,ArraySize);
return 0;
}

float ArraySum(float arr[],int n)
{
int total=0;
for (int i=0;i<=n;i++)
total+=arr[i];
return total;
}




注意:对于C99 VLA,传递它们时有特殊选项

作为参数。

如果你能更明确地指出你的需求,我们可以帮助你更好地支付


给我们提供C代码而不是C ++代码也会更好。

干杯

Michael



Note: For C99 VLAs, there are special options when passing them
as arguments.
If you can specify your needs somewhat clearer, we can help you
better.
It would also be better to give us C code instead of C++ code.
Cheers
Michael




hp ****** @ yahoo.com 写道:
我知道该组是针对C而不是C ++但是请尽量帮助我,
I know that group is for C not for C++ but please try to help me ,




假设你在数学课上,讲师问任何

问题?并且有人吹嘘我有关于我的物理学的这个问题

作业。


这不是数学家是否是一个案例现在知道

物理;在许多情况下他们会,但他们在那里讨论数学,

不是物理学,而后者是浪费每个人的时间。


就像在comp.lang.c中讨论C ++一样。你的新闻组软件

不能以某种方式访问​​comp.lang.c ++吗?

C和C ++是完全不同的语言,即使它们都是

以相同的字母开头(如C和Cobol,或PL / 1和PL / SQL),并且

讨论一个人的细节可以发展成为实质性的

带宽生猪,即使这不是OP的意图,所以请

限制你的帖子只限于On Topic项目。


戴夫。



Suppose you were in a Maths class, and the lecturer asked "Any
questions?" and someone piped up "I have this question about my Physics
homework."

It''s not a case of whether or not the mathematicians present know
physics; in many cases they will, but they are there to discuss Maths,
not Physics, and the latter is a waste of everyone''s time.

As is discussing C++ in comp.lang.c. Does your newsgroup software
somehow not have access to comp.lang.c++?

C and C++ are substantially different languages, even though they both
begin with the same letter (like C and Cobol, or PL/1 and PL/SQL), and
discussions about the details of one can develop into substantial
bandwidth hogs, even if this wasn''t intended by the OP, so please
restrict your posting to On Topic items only.

Dave.


这篇关于C ++数组相对C问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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