CSC 101 Lecture Notes Week 7
Introduction to C++ Arrays
and Related C++ Programming Constructs
int main() {
int percent1, percent2, percent3, percent4, percent5;
// Percentages for each graded item
float sum1, sum2, sum3, sum4, sum5;
// Column sums for each graded item
float sum_sq1, sum_sq2, sum_sq3, sum_sq4, sum_sq5;
// Sum of squares for each item plus TOTAL
. . .
}
After:
int main() {
int percent1, percent2, percent3, percent4, percent5, ..., percent50;
// Percentages for each graded item
float sum1, sum2, sum3, sum4, sum5, ..., sum50;
// Column sums for each graded item
float sum_sq1, sum_sq2, sum_sq3, sum_sq4, sum_sq5, ..., sum_sq50;
// Sum of squares for each graded item
. . .
}
int main() {
int percent[50]; // Percentages for each graded item
float sum[50]; // Column sums for each graded item
float sum_sq[50]; // Sum of squares for each graded item
. . .
}
////
//
// This program reads a list of 1000 numbers from the terminal and prints them
// back out to the terminal in reverse order.
//
////
#include <iostream.h>
int main() {
int value1; // Variable for 1st value read
int value2; // Variable for 2nd value read
// ...
int value1000; // Variable for 1000th value read
//
// Read each number into successive variables.
//
cin >> value1;
cin >> value2;
// ...
cin >> value1000;
//
// Print out the values in reverse order by accessing from the last
// variable to the first.
//
cout << value1000 << endl;
// ...
cout << value2 << endl;
cout << value1 << endl;
return 0;
}
////
//
// This program reads a list of 1000 numbers from the terminal and prints them
// back out to the terminal in reverse order.
//
////
#include <iostream.h>
int main() {
int values[1000]; // Array of 1000 values to be read from file
int i; // Counter used as index for each valueut
//
// Initialize the array index to 0.
//
i = 0;
//
// Read each number into successive positions in the array.
//
while (i < 1000) {
cin >> values[i];
i++;
}
//
// Print out the values in reverse order by accessing from the end of the
// array to the beginning.
//
i = 999;
while (i >= 0) {
cout << values[i] << endl;
i--;
}
return 0;
}
////
//
// This program reads a list of 1000 numbers from the terminal and prints them
// back out to the terminal in reverse order.
//
////
#include <iostream.h>
int main() {
int values[1000]; // Array of 1000 values to be read from file
int i; // Counter used as index for each valueut
//
// Read each number into successive positions in the array.
//
for (i = 0; i < 1000; i++) {
cin >> values[i];
}
//
// Print out the values in reverse order by accessing from the end of the
// array to the beginning.
//
for (i = 999; i >= 0; i--) {
cout << values[i] << endl;
}
return 0;
}
Figure 1: Dissecting the for loop.
int x[50]; int y[50]; x = y; // Does NOT copy 50 elements from y to x
int x[50];
int y[50];
int i;
for (i = 0; i < 50; i++) {
x[i] = y[i];
}
if (x == y) ... // No cout << x; // No x = x + y; // No return x; // No
DoSomethingWithAnArray(x);
enum DaysOfWeek {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};
const int SUNDAY = 0; const int MONDAY = 1; const int TUESDAY = 2; const int WEDNESDAY = 3; const int THURSDAY = 4; const int FRIDAY = 5; const int SATURDAY = 6;
////
//
// This program reads in seven integers that define the number of tasks to be
// done on each day of the week, from Sunday through Saturday. The task
// numbers are stored in a seven-element array, indexed by a days-of-the-week
// enumeration.
//
////
#include <iostream.h>
enum DaysOfWeek {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
};
int main() {
int days[sizeof(DaysOfWeek)]; // Array holding task numbers
//
// Prompt for and input the number of tasks for each day.
//
cout << "Input seven integers for the number of tasks to do on Sun - Sat: ";
cin >> days[SUNDAY] >> days[MONDAY] >> days[TUESDAY]
>> days[WEDNESDAY] >> days[THURSDAY] >> days[FRIDAY]
>> days[SATURDAY];
}