Professional Documents
Culture Documents
Objectives
1. Declaring and initializing one-dimensional arrays.
2. Inserting values into and printing values out of one-dimensional arrays.
Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
To declare an array, define the variable type, specify the name of the array followed
by square brackets and specify the number of elements it should store:
Syntax:
string cars[4];
char ch_arr[5];
int myNum[3];
float myNum2[3];
Exercice # 10.1
Write a C++ program to enter and display student attendance records using array as
discussed in uploaded lecture.
Exercise # 10.2
Write a C++ program, which;
Web Resources
http://www.cprogramming.com/
Objectives
1. Declaring and initializing two-dimensional arrays.
2. Inserting values into and reading values out of two-dimensional arrays.
3. Traversing through two-dimensional arrays.
Same is the case with other datatypes like string, float and double.
2D char array doesn’t work in the same fashion i.e. if you declare a char array char
ch_Arr[3][3], it means it can have 3 location and each location can store maximum 3
characters.
int main()
{
//display matrix index
for ( int i = 0; i < 3; i++ )
{
for ( int j = 0; j < 4; j++ )
{
cout << "[" << i << "][" << j << "]\t";
}
cout<<endl;
}
ch_Arr[2][2]="Row2Col2";
//input all values of ch_Arr from user
for ( int i = 0; i < 3; i++ )
{
for ( int j = 0; j < 3; j++ )
{
cout << "ch_Arr[" << i << "][" << j << "]: ";
cin>>ch_Arr[i][j];
}
cout<<endl;
}
//display matrix/stringarray(2D) i.e. ch_Arr
for ( int i = 0; i < 3; i++ )
{
for ( int j = 0; j < 3; j++ )
{
cout << "ch_Arr1[" << i << "][" << j << "]: ";
cout << ch_Arr[i][j]<<"\t";
}
cout<<endl;
CSC 102L Programming Fundamentals Labs Page 4 of 110
}
//declaration of 2D int array
int a[3][3] = { {0,1,2}, {3,4,5}, {6,7,8}};
cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<<"\t";
}
cout<<endl;
}
return 0;
}///
Exercise # 11.1
Write a C++ program which declares two 3 x 3int arrays (Matrices). Store random values
using nested for loops in array and perform Addition and Multiplication of Matrices,
stores the result of operation in another Matrix (output arrays). And displays the results.
Web Resources
http://www.tutorialspoint.com/cplusplus/
http://www.learncpp.com/
http://projectsyapa.com/cpp
Objectives
1. Traversing through two-dimensional arrays.
2. Passing two-dimensional arrays to functions.
Exercise 12.1 Searching through Two-Dimensional Arrays
Write a C++ program, which
Populates a two-dimensional array of size 10 x 10 with random numbers between 0
and 9 (Hint: C++ has a built-in function for random number generation)
Prompts the user to enter a number
Searches for this number in the 10 x 10 array and
Displays location where the number is found in the array
Your program should display ‘Value not found’ if the user’s specified number is not found in
the 10 x 10 array. Your program should have the following interface.
The 10 x 10 array is :
8 9 6 4 6 5 7 8 1 2
4 6 3 1 1 2 8 7 4 5
6 7 8 3 4 2 3 2 3 1
2 3 2 1 4 0 6 7 8 4
0 1 9 4 7 6 5 3 2 3
2 1 2 1 3 4 3 7 8 6
6 7 8 1 2 1 3 2 4 1
5 1 2 1 1 1 3 4 7 6
7 8 7 6 4 4 3 1 2 3
4 3 2 1 4 7 8 3 0 1
Maximum Value is : 9
Found at location [0][1]
Found at location [4][2]
2 instances found.
The 10 x 10 array is :
8 9 6 4 6 5 7 8 1 2
4 6 3 1 1 2 8 7 4 5
6 7 8 3 4 2 3 2 3 1
2 3 2 1 4 0 6 7 8 4
0 1 9 4 7 6 5 3 2 3
2 1 2 1 3 4 3 7 8 6
6 7 8 1 2 1 3 2 4 1
5 1 2 1 1 1 3 4 7 6
7 8 7 6 4 4 3 1 2 3
4 3 2 1 4 7 8 3 0 1
#include <iostream>
#include<string.h>
int main()
{
CSC 102L Programming Fundamentals Labs Page 7 of 110
//integer Arrays sorting
//array declaration
int arr[10];
int i,j;
int temp;
//read n elements
for(i=0;i<10;i++)
arr[i]=rand() % 100;
//cin>>arr[i];
for(i=0;i<10;i++)
cout<<arr[i]<<"\t";
cout<<endl;
for(i=0;i<10;i++)
for(j=i+1;j<10;j++)
temp =arr[i];//50
arr[i]=arr[j];//12
arr[j]=temp;//50
for(i=0;i<10;i++)
cout<<arr[i]<<"\t";
cout<<endl;
//String Arrays
string stemp;
for(i=0;i<3;i++)
cout<<st_name[i]<<"\t";
for(i=0;i<3;i++)
for(j=i+1;j<3;j++)
if(st_name[i]>st_name[j])
stemp =st_name[i];
st_name[i]=st_name[j];
st_name[j]=stemp;
for(i=0;i<3;i++)
cout<<st_name[i]<<"\t";
//CHAR ARRAYS
for(i=0;i<3;i++)
cin>>chst_name[i];
for(i=0;i<3;i++)
cout<<chst_name[i]<<"\t";
cout<<endl;
//sorting algo
strcpy(chst_name[i], chst_name[j]);
strcpy(chst_name[j], t);
for(i=0;i<3;i++)
cout<<chst_name[i]<<"\t";
cout<<endl;
return 0;
Web Resources
http://en.wikipedia.org/wiki/Tic-tac-toe
http://www.tutorialspoint.com/cplusplus/
http://www.learncpp.com/