#include <iostream>
using namespace std;
// the program asks user to input 5 numbers
// then output the average
void main() {
// declare an array with 5 numbers
// sum is the total sum of all the scores
double score[5], sum = 0;
cout << "Please input 5 numbers. "
<< "Seperate the numbers by spaces: \n";
int i;
for(i = 0; i < 5; i++) {
cin >> score[i]; //ask the user to input the i-th score
sum+=score[i];
}
// magic formula
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The scores are : \n";
for(i = 0; i < 5; i++) {
cout << score[i] << " ";
}
cout << endl;
cout << "The average is " << sum/5 <<endl;
char line[100];
cin.getline(line,100);
cin.getline(line,100);
}
|
Please input 5 numbers. Seperate the numbers by spaces: 92.25 33 45.75 88.8 77.77 The scores are : 92.25 33.00 45.75 88.80 77.77 The average is 67.51 |
score[0] |
score[1] |
score[2] |
score[3] |
score[4] |
| 92.25 | 33 | 45.75 | 88.8 | 77.77 |
double score[5] declares an array of 5 doubles
names scoresscore[i] is the i-th element.| Declare an array Type arrayName[size]; size should be a number or a const variable. It shouldn't be other type of variables. |
int year[50];int is called the base type of the array,
50 is the size. year is the name.double taxRate, income[99];The order doesn't matter.
#include <iostream>
using namespace std;
// the program asks user to input 5 numbers
// then output the average
void main() {
const int NUM_OF_STUDENTS = 5;
// declare an array with NUM_OF_STUDENTS numbers
// sum is the total sum of all the scores
double score[NUM_OF_STUDENTS], sum = 0;
cout << "Please input "<< NUM_OF_STUDENTS << " numbers. "
<< "Seperate the numbers by spaces: \n";
int i;
for(i = 0; i < NUM_OF_STUDENTS; i++) {
cin >> score[i]; //ask the user to input the i-th score
sum+=score[i];
}
// magic formula
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The scores are : \n";
for(i = 0; i < NUM_OF_STUDENTS; i++) {
cout << score[i] << " ";
}
cout << endl;
cout << "The average is " << sum/NUM_OF_STUDENTS <<endl;
char line[100];
cin.getline(line,100);
cin.getline(line,100);
}
|
const int NUM_OF_STUDENTS = 100;
#include <iostream>
using namespace std;
void main() {
int numOfStudents;
cout << "Please enter the number of students: ";
cin >> numOfStudents;
double score[numOfStudents], sum = 0;
char line[100];
cin.getline(line,100);
cin.getline(line,100);
}
|
score[0],score[1],score[2],score[3],score[4]
represent the first, second, third, forth and fifth element in
the array scorescore[i] as variables. i.e. you can
use it in assignments, arithmetic expression etc etcscore[i], or income[n+1].
for(i = 0; i < NUM_OF_STUDENTS; i++) {
cin >> score[i]; //ask the user to input the i-th score
sum+=score[i];
}
|
#include<iostream>
#include<cmath>
using namespace std;
void main() {
// declare an integer array and an integer x
double A[10], x=2.5;
// array index starts with 0
for(int i=0; i < 10; i++) {
// access A[i]
A[i] = x*i;
}
for(int i=0; i < 10; i++) {
cout << A[i] << " ";
}
cout << endl;
// A[i] can be used like a variable;
A[0]++;
cout << "A[0] = " << A[0] << endl;
A[1] = A[0]*A[2];
cout << "A[1] = " << A[1] << endl;
cout << "sqrt(A[3]) = " << sqrt(A[3]) << endl;
x+=A[5];
cout << "x = " << x << endl;
char line[100];
cin.getline(line, 100);
}
|
0 2.5 5 7.5 10 12.5 15 17.5 20 22.5 A[0] = 1 A[1] = 5 sqrt(A[3]) = 2.73861 x = 15 |
double temperature[24]; // 24 is array sizetemperaturetemperature[0], temperature[1] …
temperature[23]temperature[24] = 5;
#include<iostream>
using namespace std;
void main(){
int temperature[24];
int i;
for(i = 0; i < 24; i++) {
temperature[i] = i + 60;
}
cout << temperature[24] << endl;
char line[100];
cin.getline(line,100);
}
|
1245120 |
int children[3] = {2, 12, 1};
|
int children[3]; children[0]=2; children[1]=12; children[2]=1; |
int b[] = {5, 12, 11};
|
int b[3] = {5, 12, 11};
|
#include<iostream> |
| 1 3 5 0 0 |