Array 


Contents of this section:

  1. One dimensional integer array
  2. One dimensional character array (C-string)
  3. Two dimensional integer array
  4. Two dimensional character array (Array of strings)

1. One dimensional integer array

    Code Listing: 

// Author:    Herbert Schildt
// File name: ~ftp/pub/class/cpluslus/Array1.cpp
// Purpose:   Read 10 integers into an array and then print
//	      them out

#include <iostream>
using namespace std;

int main()
{
	int sample[10]; // this reserves 10 integer elements
	int t;

	// load the array
	for(t = 0; t < 10; t++)
		sample[t] = t;

	// display the array
	for(t = 0; t < 10; ++t)
		cout << sample[t] << ' ' ;
	
	return 0;
}

    Running session:

mercury[34]% CC -LANG:std Array1.cpp -o Array1
mercury[35]% Array1
0 1 2 3 4 5 6 7 8 9 mercury[36]% 
mercury[36]% 

2. One dimensional character array (C-string)

Code listing:

// File name: ~ftp/pub/class/cplusplus/Array/Array2.cpp
// Purpose:   Demonstrate the use of character array.
//	      Ask for the user's name and print it out.

#include <iostream>
#include <stdlib.h> //for newer compilers, include <cstdlib>
using namespace std;

int main()
{
	char name[32]; // big enough to hold 32 characters

	// prompt for the name
	cout << "What's your name?" << endl;
	gets(name); // read a string from the key board.
	cout << "Hello! " << name << "!"  << endl;

	return 0;
}

Running session:

mercury[41]% CC -LANG:std Array2.cpp -o Array2
mercury[43]% Array2
What's your name?
Bruce Lee
Hello! Bruce Lee!
mercury[44]% 

3. Two dimensional integer array

Code listing:

// Author:    Herbert Schildt 
// Modified:  Qiang Hu
// File name: ~ftp/pub/class/cplusplus/Array/Array3.cpp
// Purpose:   Use sqrs[][] -- two dimentional integer
//	      array to store integers from 1 to 10 and
//	      their squares.  
//            Ask user for a number, then look up this
// 	      number in the array and then print out its
//            corresponding square.

#include <iostream>
using namespace std;

int main()
{
  // C++ allows for the initialization of arrays.  In the following,
  // we are initializing a 10x2 integer array. After initialization,
  // sqrs[0][0] = 1
  // sqrs[0][1] = 1
  // sqrs[1][0] = 2
  // sqrs[1][1] = 4 
  // and so on
  int sqrs[10][2] = { 
         {1, 1}, 
	 {2, 4}, // The square of 2 is 4,and so on
	 {3, 9}, 
	 {4, 16}, 
	 {5, 25}, 
	 {6, 36}, 
	 {7, 49}, 
	 {8, 64}, 
	 {9, 81}, 
	 {10, 100} 
  };

  int i, j;

  cout << "Enter a number between 1 and 10: ";
  cin >> i;

  // look up i
  for(j = 0; j < 10; j++) 
    if(sqrs[j][0] == i) break; // break from loop if i is found 
  cout << "The square of " << i << " is " ;
  cout << sqrs[j][1] << endl;

  return 0;
}

Running session:

mercury[66]% CC -LANG:std Array3.cpp -o Array3
mercury[67]% Array3
Enter a number between 1 and 10: 6
The square of 6 is 36
mercury[68]% 

 

4. Two dimensional character array (Array of Strings)

Code Lising

// Author:   Herbert Schildt
// Modified: Qiang Hu
// File name: ~ftp/pub/class/cplusplus/Array/Array4.cpp
// Purpose:  A simple employee database program.

#include <iostream>
using namespace std;

// function prototyping
int menu();    // funciton to display the menu
void enter();  // function to enter info
void report(); // function to print report

// Global variables:
char name[2][80];  // this array holds employee names
char phone[2][20]; // their phone numbers
float hours[2];    // hours worked per week
float wage[2];     // wage
int choice;

int main()
{
  do {
    choice = menu(); // get selection
    switch(choice) {
      case 0: break;
      case 1: enter();
        break;
      case 2: report();
        break;
      default: cout << "Try again.\n\n";
    }
  } while(choice != 0);

  return 0;
}

// Return a user's selection.
int menu()
{
  int choice;

  cout << "0. Quit\n";
  cout << "1. Enter information\n";
  cout << "2. Report information\n";
  cout << "\nChoose one: ";
  cin >> choice;

  return choice;
}

// Enter information.
void enter()
{
  int i;
  for(i=0; i<2; i++) {
    cout << "Enter last name: ";
    cin >> name[i];
    cout << "Enter phone number: ";
    cin >> phone[i];
    cout << "Enter number of hours worked: ";
    cin >> hours[i];
    cout << "Enter wage: ";
    cin >> wage[i];
  }
}

// Display report.
void report()
{
  int i;

  for(i=0; i<2; i++) {
    cout << name[i] << ' ' << phone[i] << '\n';
    cout << "Pay for the week: " << wage[i] * hours[i];
    cout << '\n';
  }
}

Running Session

mercury[81]% CC -LANG:std Array4.cpp -o Array4
mercury[82]% Array4
0. Quit
1. Enter information
2. Report information

Choose one: 1
Enter last name: Smith
Enter phone number: 5556666
Enter number of hours worked: 30
Enter wage: 23.40
Enter last name: Bush
Enter phone number: 6668888
Enter number of hours worked: 20
Enter wage: 40.00
0. Quit
1. Enter information
2. Report information

Choose one: 2
Smith 5556666
Pay for the week: 702
Bush 6668888
Pay for the week: 800
0. Quit
1. Enter information
2. Report information

Choose one: 0
mercury[83]% 


Last modified: Friday, 21-Aug-2020 15:28:15 CST
Copyright 2000 Department of Computer Science, University of Regina.

 CS Dept Home Page

Teaching Material

C++ Index Page