One-Dimensional Arrays: TH TH
One-Dimensional Arrays: TH TH
It is a list(or table) of a fixed number of variables of the same type which are stored sequentially in the computer memory. It is used
to store lists of numbers, letters, words, and other data types. Arrays allow you to organize data in either list or tabular form. The variables of
an array have a common name and are distinguished by means of subscripts (indexes/offsets). For example, below is an array containing 4
integer variables:
list[0], list[1], list[2], list[3]
The variables in an array are called its components or elements(list[0],list[1],… The number inside the bracket is the subscript). Each element is
numbered beginning at 0 up to array_size minus-1, where array_size is the number of elements.
One-Dimensional Arrays
Arrays with a single column of entries.
Array Declaration
data_type name[array_size];
Examples: double value[5]; //read as: “value is an array of 5 doubles
char name[20]; //name is an array of 20 characters
Initializing an Array
List the element’s initial value in braces separated by a comma
Example : int num[5] = {4,2,13,7,8};
Effect: num[0]=4, num[1]=2, num[2]=13, num[3]=7, num[4]=8
If you do not list enough values the computer will fill the remaining elements with 0
Example: int num[5] = {0};
Effect: num[0]=0, num[1]=0, num[2]=0, num[3]=0, num[4]=0
If you initialize an array in its declaration, you can omit the size of the array
Example : int num[] = {4,2,13,7,8};
Effect: the size of this array is 5 since there are 5 elements inside the braces
Sample Problem
Write a program that stores ten numbers in array num. Your program should locate and display both the minimum and maximum
values in the array.
Program:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int num[10], i,min,max;
Sample Problem
a.Write a declaration to store the string “This is a test” into an array named str. Include the declaration in a program to display the
message using for loop.
b. Modify the for statement to display only the characters t,e,s, and t (“test”)
a.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char str[14]={'T','h','i','s',' ','i','s',' ','a',' ','t','e','s','t'};
int i;
//display contents of str
for(i=0; i<14; i++)
cout<<str[i];
getch();
return 0;
}
b. //display “test” only
for(i=10; i<14; i++)
cout<<str[i];
Program
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int rate[5],hrs[5];
//get rate and hours worked
cout<<"Enter rate and hours worked of employee \n";
for(int i=0; i<5; i++)
{
cout<<"\nEmployee "<<i+1<<": ";
cout<<"\nRate : ";
cin>>rate[i];
cout<<"Hours Worked : ";
cin>>hrs[i];
}
//heading
cout<<"\tEmployee No. Rate Hours Worked Salary\n";
//display table
for(int i=0; i<5; i++)
cout<<"\n\t "<<i+1<<" "<<rate[i]<<" "<<hrs[i]<<"
"<<rate[i]*hrs[i];
getch();
return 0;
}
int num[10],i;
srand(time(NULL));
for(i=0; i<10; i++)
num[i]=1+rand()%20; //store random no. in num[i]
srand(time(NULL))- randomizing to produce different sequence of random numbers for each execution
rand()%20 – produces numbers 0 – 19(called scaling), 20 is the scaling factor, to produce 1 to
20, we add 1. You may change the scaling factor.
Bubble Sort – adjacent elements in the list are exchanged with one another in such a manner that
the list becomes sorted
Pseudocode for bubble sort:
For the first element in the list to one less than the last element(I index)
For the second element in the list to the last element(j index)
If list[j] < list[j-1]
Swap list[j] with list[j-1]
End for
End for
Exercise: convert the above pseudocode to C++ statements.
Two-dimensional Arrays
Sometimes referred to as a table of data, consists of both rows and columns of elements.
Example: array num
Row 0 8 16 9 3
Row 1 7 2 17 1
Row 2 10 20 4 6
An element in a two-dimensional array is identified by its position. Consider the array num
above, 8 is the element on the first row, first column(num[0][0]), 17 is the element on the 2nd
row, 3rd column (num[1][2]), etc.
f.write a program segment that will display the elements of the first row of num
for(c=0; c<4; c++)
{
cout<<num[0][c]; //1st row only, row number is set to 0
}
g.write a program segment that totals the elements of the 3 rd column of num, it will also compute
the average
sum=0;
for(r=0; r<3; r++)
{
sum+=num[r][2];
}
float ave=sum/3.0;
Sample Problem
Write a program that will load a 2-dimensional array with random numbers. The program
should then display the random numbers generated. Use functions to load the array and display the
array
Program
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
getch();
return 0;
}
Problems:
1. Write a program that will generate 20 random numbers (range: 1 – 50) and store it in one-
dimensional array; then the program will read a number d and check whether the number d is
present in the array. If it is so, print how many times the number d is repeated in the
array. Use the ff. functions:
a. Load() – load array with random numbers
b. Search() – search array if it contains d and return the number of times d is
repeated
c. Display()-display result
2. Write a program to read 10 numbers and store it in 1-dimensional array; then it will find
the largest and the smallest number. Find also the difference between the 2 numbers. Use
functions
3. Write a program to read 10 numbers and store it in one-dimensional array A; copy the
elements in another array B in reverse order; find the sum of the individual elements of
the array A and B and store the results in another array C(ex. C[0]=A[0]+B[0];
C[1]=A[1]+B[1];….). Display all the 3 arrays. Use functions
4. Write a program to read a 2-dimensional square matrix A and display its transpose.
Transpose of a matrix A is obtained by interchanging all the elements of rows and columns
of original matrix A. Use functions
5. Write a program to read a two dimensional array and find the sum of the elements row wise
and column wise separately, and display the sums of the rows and columns. Use functions.
6. Write a program that loads array A[4][3] with random numbers(range:1-50). Load into array
E all even integers and into array O all odd integers of array A. Print array E with
elements arrange in ascending order, in one line. Print array O on the second line.