0% found this document useful (0 votes)
10 views

CSC 202 Session 6

Uploaded by

Abimbade Jamiu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

CSC 202 Session 6

Uploaded by

Abimbade Jamiu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Prepared by : Akande Noah O.

ARRAYS
 When multiple data items with common
characteristics (such as the same name and data type)
are to be processed then Arrays could be used.
 For instance, a set of numerical data: x1 + x2 + … xn can
be processed with an array
 Each individual data item of an array can be referred to
by specifying the array name followed by the
subscripts, with each subscript enclosed in square
brackets . e.g. X[0], x[1], x[2]…x[n-1] etc.
 The number of subscripts of an array determines the
dimensionality of the array e.g. :
 One dimensional as in x[i]
 Two dimensional as in x[i][j]
 Three dimensional as in x[i][j][k]
DEFINING AN ARRAY
 The syntax for defining an array is:
 Data_type array_name [array size]
 Examples are:
 int age[50];
 char name[25] ;
 float result[25];
 Values can be assigned to arrays from the declaration
point as in:
 int age[10] = (15, 27, 23, 14, 22, 34, 27,2 8, 29, 31);
 age[0] = 15; digits[1] = 27; digits[2] = 23 ….

 float CGPA[6] = (3.50, 2.25, 4.51, 2.22, 3.85, 4.99);


 char color[3] = { ‘R’ , 'E', ‘ D ’ } ;
 Array elements without initial values will
automatically be set to zero
LOADING AND OFFLOADING AN ARRAY<<
#include <iostream>
using namespace std;

int main ()
{
int n, Grade[50], count ;
cout << "How many numbers have you?"<< endl ;
cin >> n;
for (count = 0; count < n; ++count)
{
cout << "supply your value "<< count + 1<<endl;
cin >> Grade[count] ;
}
for (count = 0; count < n; ++count)
cout << "your values are "<< count + 1 <<":"<< Grade[count] <<endl;
}
SORTING AN ARRAY <<
 #include <iostream>
 #include <iomanip>
 using namespace std; int main(void)
 { int a[10], i=0, j=0, n, t;
 cout<< "\n Enter the no. of elements: "<<endl;
 cin >> n;
 //A. DETERMINING THE NUMBER OF ELEMENTS IN THE ARRAY
 for (i = 0; i <n; i++) {
 cout<<"\n Enter element : " << (i+1)<< endl;
 cin >> a[i]; }
 //B. SORTING THE ARRAY
 for (j=0 ; j<n; ++ j) {
 for (i=j+1 ; i<n ;++ i) {
 if (a[j] > a[i]) {
 t = a[j];
 a[j] = a[i ];
 a[i] = t; } }}
 //C. DISPLAYING CONTENTS IN ASCENDING ORDER
 cout<< "\n Ascending order: "<< "\n";
 for (i=0 ; i<n ; ++i) {
 cout<< "element" << i+1 << setw(4) << a[i] << "\n"; }
 //D. DISPLAYING CONTENTS IN DESCENDING ORDER
 cout<< "\n \nDescending order: " << "\n";
 for (i=n ; i>0 ; i--) {
 cout<< "element" << i+1 << setw (4) << a[i-1] << "\n"; }
 return 0; }
MULTIDIMENSIONAL ARRAYS
 A multidimensional array is an array with two or more
dimensions.
 It could be of two dimensions as in A[m][n], three
dimensions as in A[m][n][0] etc
 Multidimensional arrays are defined in a similar way as
one-dimensional arrays
 data-type array[expression_1][expression_2] . . .
[ expression_n];
 A m x n two-dimensional array can be referred to as a
table of values with m rows and n columns
 A three-dimensional array can be visualized as a set of
tables
MULTIDIMENSIONAL ARRAYS
 For example: float scores [50][50];
 double records[100][66][255];
 Elements of a multidimensional arrays are assigned on
a row by row basis
 For a two-dimensional array, the elements of the first
row will be assigned, followed by the elements of the
second row, and so on.
 Int values[3][4] = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
 The natural order in which the initial values are
assigned can be altered by forming groups of initial
values enclosed within braces
 However, the number of values within each pair of
braces cannot exceed the defined row size.
MULTIDIMENSIONAL ARRAYS
 If the number of rows or columns defined is more
than the initial values assigned then the
remaining rows and columns will be filled with
zero as in:
 int values[3][4] = {
 {a, b, c},
 {d, e, f},
 {g, h, i},
 },
 int values[3][4] = {1, 2, 3, 4, 5, 6,7 , 8 };
LOADING A TWO-DIMENSIONAL ARRAY
 Values can be read into Array M x N by first reading values into
the first row, then the next row until all the rows are filled up
#include <stdio.h>
int a[10][10], rows, cols, i,j;
main()
{
cout<<"How many number of rows ? \n";
cin<< rows;
cout<<"How many number of columns ? \n";
cin <<&cols;
for (i = 0; i < rows; i++)
{
cout<<"supply your values for row : \n \n“<< i;
for (j = 0; j < cols; j++)
{
cout<<" Column : “<< j;
cin <<a[i][j];
} }}
DISPLAYING THE CONTENTS OF A TWO-
DIMENSIONAL ARRAY

for (i = 0; i < rows; i++)


{
for (j = 0; j < cols; j++)
{
cout<< a[i][j];
}
cout<<"\n";
}
PROGRAMMING EXAMPLES
 Adding Two Tables of Numbers >>
 To achieve this: values will be read into each table
then the addition of the elements will be carried
out
PROGRAMMING EXAMPLES
#include <iostream>

// function declaration
void readFirstMatrix();
void printFirstMatrix();
void readSecondMatrix();
void printSecondMatrix();
void sumMatrix();
void subtractMatrix();

Int firstMatrix[10][10], secondMatrix[10][10],


matrixSum[10][10], matrixDiff[10][10];
int i, j, FMrow, FMcols, SMrow, SMcols;
String EXAMPLE <<
 #include <iostream>
 using namespace std;
 int main()
{
 const int MAX = 80;
 char str[MAX];
 cout << "Enter a string: ";
 cin.get (str, MAX); // cin.get (str, MAX, ‘$’);
 //display string from str
 cout << "You entered: " << str << endl;
 return 0;
}
String EXAMPLE <<
 #include <iostream>
 #include <iomanip>
 using namespace std;
 int main()
 { const int MAX = 80;
 char sentence[MAX];

 int i, vowels = 0, consonants = 0, special = 0;


 cout << "Enter a sentence "<< "\n";
 cin.get(sentence, MAX);
 cout << "You entered: " << sentence << endl;
 for (i = 0; sentence[i] != '\0'; i++)
 { if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 'i' || sentence[i] ==
'o' || sentence[i] == 'u') || (sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] == 'I'
|| sentence[i] == 'O' || sentence[i] == 'U'))
 {vowels = vowels + 1;}
 else
 {consonants = consonants + 1;}
 if (sentence[i] =='\t' ||sentence[i] =='\0' || sentence[i] ==' ')
 {special = special + 1;}
 } consonants = consonants - special;
 cout << "Total Number of vowels in the sentence is" << vowels << endl;
 cout << "Total Number of consonants is " << consonants << endl;
 }
Programming Examples
 Loading and off-loading an array
 Sorting the contents of an array
 Calculating deviations about an Average
 d =xi - avg
 Calculating semester Grade Point
 Reversing the content of an array
 Strings as an array of characters
 Write a C program using array to:
 Take the sentence as input.
 Using any control structure of interest, scan the sentence and check
for vowels and consonants separately.
 Print the output accordingly and exit.
 Write a C Program to accept N integer number and store them
in an array VALUES.
 Copy the odd elements in array VALUES into array ODAR and other
elements into array EVAR.
 Display the contents of OAR and EAR.

You might also like