//_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; }
程序运行结果
相关推荐
C++大学基础教程_12767412.pdf
关于指针数组关于指针数组关于指针数组关于指针数组
一个关于如何定义c++语言中的数组类型和数组指针的教学源码。
1 数组及其简单应用 2 指针及其简单应用 3 指针与数组 4 字符串 5 指针数组与命令行参数 6 引用
C++基础教程_完整版 C++基础教程_完整版 C++基础教程_完整版 C++基础教程_完整版 C++基础教程_完整版 C++基础教程_完整版
c++数组教程c++数组教程c++数组教程c++数组教程c++数组教程c++数组教程c++数组教程c++数组教程c++数组教程c++数组教程c++数组教程c++数组教程c++数组教程c++数组教程c++数组教程c++数组教程c++数组教程c++数组教程...
数据结构算法,线性表、栈和队列、字符串和多维数组、树和二叉树、图、查找技术、排序技术、索引技术
c++实现获取一维数组极值点。可以通过调整阈值,改变极值点获取值
C++_STL范例大全_教程,C++_STL范例大全_教程C++_STL范例大全_教程
C++课程-3_数组指针与字符串 C++课程-3_数组指针与字符串
java和c/c++相互调用实例,包括静态和动态生成c/c++源码,和c/c++数组,线程,对象的使用。
├─07_7_day2_scanf原理讲解.mp4 ├─08_8_day2_运算符1.mp4 ├─09_9_day2_运算符2.mp4 ├─10_10_day2_运算符3.mp4 ├─11_11_day3_选择与循环1.mp4 ├─12_12_day3_选择与循环2.mp4 ├─13_13_day3_循环与一维...
C++大学基础教程(第五版)(C++ How to Program中文版).pdf
C++小技术,三维数组的读取问题,您可以看到C++晋绥
使用 ======================== - SELECT *FROM myArray ORDER BY key DESC,value ASC, - SELECT key,value FROM myArray WHERE value= 100 , - SELECT * FROM json_file(my_json_file.txt), - SELECT * FROM xml_...
C++ 大学自学教程(第7版) 目录 第一部分 C++语言 1,C++程序设计概述 2,编写简单的C++程序 3,函数 4,程序流程控制 5,C++数据类型结构 6,结构和联合 7,C++数据进阶 8,指针、地址和动态存储器 9,引用变量 ...
7,C++数据进阶 8,指针、地址和动态存储器 9,引用变量 10,递归 11,预处理器 12,函数模板 13,程序组织结构 第二部分 使用类 14,C++类 15,构造函数和析构函数 16,转换 17,类对象数组 18,类成员 19,友元 20...
C++ 大学自学教程(第7版) 目录 第一部分 C++语言 1,C++程序设计概述 2,编写简单的C++程序 3,函数 4,程序流程控制 5,C++数据类型结构 6,结构和联合 7,C++数据进阶 8,指针、地址和动态存储器 9,引用变量...
我们应该注意到,没有一个受过伦理教育的软件工程师会同意开发出“摧毁巴格达”的程序。然而基本的职业道德却可以要求他们开发出“摧毁城市”的程序,巴格达只是这个程序的一个参数。