Technology Advances
Arrays
1. One dimensional integer array
Code Listing:
// 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:
0 1 2 3 4 5 6 7 8 9
2. One dimensional character array (C-string)
Code listing:
// 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:
What's your name?
Bruce Lee
Hello! Bruce Lee!
3. Two dimensional integer array
Code listing:
// 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:
Enter a number between 1 and 10: 6
The square of 6 is 36
4. Two dimensional character array (Array of Strings)
Code Lising
// 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
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
Contact
Microlaunch
Rayat Institute of Engineering and Information Technology, Railmajra, Near Ropar (Chandigarh)
+91-9463349819
microlaunch@rediffmail.com
Rayat Institute of Engineering and Information Technology, Railmajra, Near Ropar (Chandigarh)
+91-9463349819
microlaunch@rediffmail.com