Arrays: Dept. of Computer Science Faculty of Science and Technology
Arrays: Dept. of Computer Science Faculty of Science and Technology
Valid indexes:
values[0]=5;
values[9]=7;
Invalid indexes:
values[10]=3;
values[-1]=6;
In memory: elements of an array are stored
at consecutive locations
Arrays - Example
#include <iostream> Using symbolic
#define N 6 constants for array
int main (void) size makes program
{ more general
int values[N];
int index;
for ( index = 0; index < N; ++index ) {
cout<<“Enter value of element”<<index<<endl;
cin>>values[index];
}
for ( index = 0; index < N; ++index )
cout<< “values[“<<i<<“]=“<< values[index]<<endl;
return 0;
} Typical loop for
processing all
elements of an array
What goes wrong if an index goes out of range ?
#include <iostream>
using namespace std;
int main (void){
int NA, NB;
cout<<"Enter NA and NB"<<endl;
cin>>NA>>NB;
int b[NB],a[NA];
int index;
for ( index = 0; index < NB; index++ )
b[index]=10+index;
for ( index = 0; index < NA+2; ++index )
a[index]=index;
for ( index = 0; index < NA+2; ++index )
cout<<"a ["<<index<<"]= "<<a[index]<<endl;
for ( index = 0; index < NB; ++index )
cout<<"b ["<<index<<"] ="<< b[index]<<endl;
return 0;
}
Exercise: Array of counters
response
ratingCounters
++
0 1 2 9 10
2 3 5 7 11
0 1 2
primeIndex
Exercise: Prime numbers
#include <iostream>
using namespace std;
// Modified program to generate prime numbers
int main (void) {
int p, i, primes[50], primeIndex = 2; bool isPrime;
primes[0] = 2; primes[1] = 3;
for ( p = 5; p <= 50; p = p + 2 ) {
isPrime = true;
for ( i = 1; isPrime && p / primes[i] >= primes[i]; ++i )
if ( p % primes[i] == 0 )
isPrime = false;
if ( isPrime == true ) {
primes[primeIndex] = p;
++primeIndex;
}
}
for ( i = 0; i < primeIndex; ++i )
cout<<"Primes["<<i<<"]"<<primes[i]<<endl;
return 0;
}
Initializing arrays
int counters[5] = { 0, 0, 0, 0, 0 };
a special case of character arrays: the character string type =>in a later chapter
Example: Base conversion using arrays
#include <iostream>
using namespace std;
int main (void)
{
const char baseDigits[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
int convertedNumber[64];
long int numberToConvert;
int nextDigit, base, index = 0;
// get the number and the base
cout<<"Number to be converted? "<<endl;
cin>>numberToConvert;
cout<<"Base? "<<endl;
cin>>base;
Example continued
// convert to the indicated base
do {
convertedNumber[index] = numberToConvert % base;
++index;
numberToConvert = numberToConvert / base;
}
while ( numberToConvert != 0 );
// display the results in reverse order
cout<<"Converted number = ";
for (--index; index >= 0; --index )
{
nextDigit = convertedNumber[index];
cout<<baseDigits[nextDigit];
}
return 0;
}
new and delete operators in C++ for dynamic
memory
Dynamic memory allocation in C++ means allocating memory
manually by programmer when needed.
How is it different from memory allocated to normal variables?
Previously used variables like “int a”, “char str[10]”, etc.,
memory is automatically allocated and deallocated. For
dynamically allocated memory it is programmers responsibility
to deallocate memory when no longer needed. If programmer
doesn’t deallocate memory, it causes memory leak (memory is
not deallocated until program terminates).
How is memory allocated/deallocated in C++?
C++ has two operators new and delete that perform the task of
allocating and freeing the memory. The allocation is done at
runtime.
Example: Variable length arrays
#include <iostream>
using namespace std;
int main() {
int n;
int i;
cout<<"How many elements do you have? "<<endl;
cin>>n; Pointer is used so that we know where
the memory is allocated.
int *a = new int[n];
Now when we are new operator is responsible for allocating
done using our
allocated memory for(i = 0; i < n; i++) the memory
which is where a cin>>a[i];
pointer is
for(i = 0; i < n; i++) It indicates, memory is allocated for this
pointing ,we must
deallocate it using cout<< a[i]; type of datatype, here it is int
delete operator.
If we want to allocate memory for multiple data’s we
delete a;
can create array by providing any positive integer
return 0; number or a variable having similar type value
}