在C++中如何输入数组

帮我写个程序代码就以一个数组a[10]={1,2,3,4,5,6,7,8,9,10}要求a[10]是以从键盘输入,
要求在输入时也要用一个文件把a[10]存起来
逆序输出,
并把这个输出结果存入 一个文件中。
谢谢

第1个回答  推荐于2017-11-25
楼上的C语言风格太重,下面的是纯C++风格的代码

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
const int N=10;/* 必须是常整型(或者动态分配内存) */
int a[N];/* 数组声明 */
cout<<"下面录入数组各元素:\n";
ofstream out1("original.txt");/* 打开文件流 */
for(int i=0;i<N;i++)
{
cout<<"a["<<i<<"]=";/* 输入进度提示 */
cin>>a[i];/* 录入 */
out1<<a[i]<<"\n";/* 顺序保存 */
}
out1.close();/* 关闭文件流 */

ofstream out2("inverse.txt");/* 打开文件流 */
for(int j=N-1;j>=0;j--)/* 逆序输出 */
out2<<a[j]<<"\n";
out2.close();*/ 关闭文件流 */

return 0;
}本回答被提问者采纳
第2个回答  2008-06-20
#include<iostream>
using namespace std;
long sa[10];
int main()
{
freopen("in.txt","w",stdout);
for (long a=0;a<10;++a)
{
scanf("%d",&sa[a]);
printf("%d ",sa[a]);
}
freopen("out.txt","w",stdout);
for (long a=9;a>=0;--a) printf("%d ",sa[a]);
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网