C++ experiment 02 (03) the use of local variables and global variables

Title description
Write a program: sum the array elements with 12 integer data, the result is stored in the global variable sum, and the elements with an odd value in the array element are summed, and the result is stored in the local variable sum, and the two are stored in the main program Results output. This question requires experience and understanding of the concept and basic usage of scope operators.
Input description
12 integer data
Output description The
output is divided into two lines:
the sum of the entire array elements, the sum
of odd values ​​in the array elements.
Input example
1 2 3 4 5 6 7 8 9 10 11 12 The sum of all elements of the
output example
is : The
sum of the odd values ​​in the 78 array elements is: 36 (Chinese colon)

#include <iostream>
using namespace std;
int sum=0;
int main()
{
    
    
	int f[12],i=0,sum=0;
	for(i=0;i<12;i++)
		cin>>f[i];
	for(i=0;i<12;i++)
	{
    
    
		::sum+=f[i];
		if(f[i]%2==1)
			sum=sum+f[i];
	}
	cout<<"所有元素的和为:"<<::sum<<endl;
	cout<<"数组元素中值为奇数的和为:"<<sum;
	return 0;
}

Global variables and local variables

::sum是全部变量

Guess you like

Origin blog.csdn.net/weixin_44179485/article/details/105890634