array 及 vector 的使用

此程序从标准输入读取整数,分别放入数组和向量中,计算总数和平均值,并输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

撰写一个程序,从标准输入装置读取一串整数,并将读入的整数依次置入array及vector,然后遍历这两种容器,求取数值总和,将总和及平均值输出至标准输装置。


// 1_6.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{

	int a[5];
	vector<int*> a_v(5);
	int total=0,total_v=0;
	//int* p;
	//p=&a_v[0];

	for(int i=0;i<5;i++)
	{
		cin >> a[i];
		a_v[i]=&a[i];
		
	}

	for(int j=0;j<5;j++)
	{
		total += a[j];
		total_v += *a_v[j];
	}

	/*for(int* p=&a_v[0];p<p+5;p++)
	{
		total_v+=*p;
	}*/

	cout << "total=" << total << endl;
	cout << "average=" << total/5 << endl;

	cout << "total_v=" << total_v << endl;
	cout << "average=" << total_v/5 << endl;
	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值