//_7_4_main_1.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "Element" << setw(12) << "Value" << endl;
//声明一个数组,然后用一个循环初始化数组
int a[10];
for(int i=0;i<10;i++)
a[i] = 0;
for(int j=0;j<10;j++)
cout << " a[" << j << "]" << setw(9) << a[j] << endl;
cout << endl;
//在声明中用初始化列表初始化数组
int b[10] = {32,43,54,6,6,43,2,5,23,76};
for(int j=0;j<10;j++)
cout << " b[" << j << "]" << setw(9) << b[j] << endl;
cout << endl;
//用常量变量指定一个数组大小,用计算结果设定数组元素
const int arraySize = 10;//在声明变量常量的时候没有赋值是一个编译错误!!!!!!
int s[arraySize];
for(int i=0;i<10;i++)
s[i] = 2*i + 1 ;
for(int j=0;j<10;j++)
cout << " s[" << j << "]" << setw(9) << s[j] << endl;
cout << endl;
//求数组元素之和
const int arraySize_1 = 10;
int t[arraySize_1] = {87,68,96,100,23,54,5,65};
int total = 0;
for(int i=0;i<10;i++)
total = total + t[i] ;//注意啊!!!!是 ' +t[i] ' ,不是' t[arraySize_1] ' !!!!!!!!!!!!
cout << "Total of array elements:" << total << endl;
for(int j=0;j<10;j++)
cout << " t[" << j << "]" << setw(9) << t[j] << endl;
system("pause >> cout");
return 0;
}
//_7_4_main2.cpp
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
//使用条形统计图表显示数组数据
const int arraySize = 11;
int s[arraySize] = {4,0,1,2,0,8,0,4,2,5,2};
for(int i=0;i<arraySize;i++)
{
//输出“0-9 、10-19、20-29、、、、”
if(i == 0)
cout << " 0-9:" ;
else if(i == 10)
cout << " 100:" ;
else
cout << i*10 << "-" << i*10+9 << ":";
for(int star=0;star<s[i];star++)
cout << "*" ;
cout << endl;
}
cout << "\n\n" ;
srand((unsigned)time(NULL));
//使用数组汇总调查结果(40名学生评价学生食堂等级为1最差-10最好级)
const int responseSize = 40;//学生投票的数组大小
const int frequencySize = 11;//汇总的数组大小,表示每种结果(1-10)有多少人
int response[responseSize];
int frequency[frequencySize] = {0};
for(int i=0;i<responseSize;i++)//随机数产生学生投票结果
response[i] = 1 + rand()%10;
cout << "response[]:" << endl;
for(int i=0;i<responseSize;i++)
{
cout << setw(3) << response[i];
if((i+1)%10 == 0)
cout << endl;
}
//循环,计算每种评价出现的次数
for(int answer=0;answer<responseSize;answer++)
{
frequency[response[answer]]++;//response[]最大可以达到10啊啊啊啊啊,所以responseSize为11
}
cout << "Reting" << setw(11) << "Frequency" << endl;
for(int rating=1;rating<frequencySize;rating++)
cout << setw(6) << rating << setw(11) << frequency[rating] << endl;
//使用条形图统计
for(int j=1;j<frequencySize;j++)
{
cout << setw(2) << j << ": " ;
for(int count=0;count<frequency[j];count++)
cout << "*" ;
cout << endl;
}
system("pause >> cout");
return 0;
}
_7_4_main3.cpp
//使用字符数组存储和操作字符串
//在下面的例子中如果第一次输入的是“hellothere”,那么最后还要再输入一次“there”
//如果第一次输入的是“hello there”,那么结果就会直接显现出来
//试试咯试试咯
#include <iostream>
using namespace std;
int main()
{
char string1[20];
char string2[] = "string literal";
cout << "Enter the string \"Hello there\":";
cin >> string1;
cout << "string1 is :" << string1 << "\nstring2 is :" << string2 << endl;
cout << "string1 with space between character is :" << endl;
for(int i=0;string1[i]!='\0' ;i++)
cout << " " << string1[i];
cout << "\nstring2 with space between character is :" << endl;
for(int i=0;string2[i]!='\0' ;i++)
cout << " " << string2[i];
cout << endl;
cout << "enter \"there\" to string1: " ;
cin >> string1;
cout << "\nstring1 is :" << string1 << endl;
system("pause >> cout");
return 0;
}
//_7_4_main4.cpp
//static局部数组和自动局部数组
#include <iostream>
using namespace std;
void staticArrayInit(void);//定义static静态局部数组
void automaticArrayInit(void);//定义自动局部数组
int main()
{
cout << "First call to each function:\n";
staticArrayInit();
automaticArrayInit();
cout << "\n\nSecond call to each function:\n";
staticArrayInit();
automaticArrayInit();
system("pause >> cout");
return 0;
}
void staticArrayInit()
{
static int array1[3];
cout << "\nValues on entering staticArrayInit :\n";
for(int i=0;i<3;i++)
cout << "array1[" << i << "] = " << array1[i] << " " ;
cout <<"\nValues on exiting staticArrayInit :\n";
for(int i=0;i<3;i++)
cout << "array1[" << i << "] = " << (array1[i]+=5) << " " ;//array1[i]+=5要加括号啊啊啊
cout << endl;
}
void automaticArrayInit()
{
int array2[]={1,2,3};
cout << "\nValues on entering automaticArrayInit :\n";
for(int i=0;i<3;i++)
cout << "array2[" << i << "] = " << array2[i] << " " ;
cout <<"\nValues on exiting automaticArrayInit :\n";
for(int i=0;i<3;i++)
cout << "array2[" << i << "] = " << (array2[i]+=5) << " " ;
cout << endl;
}
程序运行结果