Tools needed:
1.
PC running Windows Operating
System (OS)
2.
DevC++ (installed)
Turn on the PC and log into it using your student
account information.
Note:
The information and concepts discussed in this lab
manual are in sufficient detail but are still not exhaustive. It is assumed
that all these concepts are already taught to you in your ITC theory class.
1. Arrays
When we need to store a lot of values of the same
type, instead of declaring a lot of variables, it is convenient to be able to
use a single name but access different values by indexing. Such data structures
are called arrays.
Declaration:
An array is declared as follows:
datatype nameofarray[sizeofarray];
Note the square brackets [ ] are part of the syntax.
For example:
int age[10]; //to store 10 integer values
float marks[100]; //to store marks of up to 100
students
Indexing:
Individual values within an array are called elements.
An array element is accessed through indexing. Index values start from 0 and
the maximum index value can be one less than the size of the array. For example
for an array of size 10, legitimate index values vary from 0 - 9. The index is provided
within square brackets, as follows:
int age[10];
cin >> age[0]; //get input into first index of
the array
Indices may not be constants. Any expression that
evaluates to an integer value can be used as an index. For example consider the
code below:
int age[10]; //to store 10 age values
for(int i = 0; i < 10; i++)
cin >> age[i];
//get input into ith index of the array
Similarly a loop can be used to output (display) the
values in an array. Also note the expression used as index:
int age[5]; //to store 5 age values
for(int i = 1; i <= 5; i++)
cout << age[i-1]
<< endl; //display value at (i-1)th index
Initialization:
An array can be initialized right when it is declared,
as follows:
datatype nameofarray[sizeofarray] = {value1, value2,
...};
Note that ellipsis (...) is not part of the syntax.
Instead it means that we need to supply enough values to initialize the
required number of elements in an array as needed. Number of values being
initialized can be less than the size of the array. The rest of the values get
initialized to 0.
int age[5] = {19,20,20,21,23}; //all values init
int age[5] = {12,19,20}; //first 3 values init, rest
init to 0
A detailed memory-map of the array age[ ] (first
example above) is as given below:
Element #
|
Index
|
Byte address
|
Memory contents
(4 bytes block)
|
Access
|
1
|
0
|
0x00FF2200
|
19
|
age[0]
|
2
|
1
|
0x00FF2204
|
20
|
age[1]
|
3
|
2
|
0x00FF2208
|
20
|
age[2]
|
4
|
3
|
0x00FF220C
|
21
|
age[3]
|
5
|
4
|
0x00FF2210
|
23
|
age[4]
|
Note byte address differs by 4 as each element in the
array is an int which takes 4 bytes to store the value.
In the second case, the map would look like this:
Element #
|
Index
|
Byte address
|
Memory contents
(4 bytes block)
|
Access
|
1
|
0
|
0x00FF2214
|
12
|
age[0]
|
2
|
1
|
0x00FF2218
|
19
|
age[1]
|
3
|
2
|
0x00FF221C
|
20
|
age[2]
|
4
|
3
|
0x00FF2220
|
0
|
age[3]
|
5
|
4
|
0x00FF2224
|
0
|
age[4]
|
When an array is not initialized, the elements can
take any values. It is customary to call such values as garbage values, since
we do not know for sure what they are. We can denote a value with a ? if we do
not know what value it is at a specific point in the execution of the code.
For example, after the declaration of an array as
follows:
int age[5]; //uninitialized array
the memory map would be:
Element #
|
Index
|
Byte address
|
Memory contents
(4 bytes block)
|
Access
|
1
|
0
|
0x00FF2214
|
?
|
age[0]
|
2
|
1
|
0x00FF2218
|
?
|
age[1]
|
3
|
2
|
0x00FF221C
|
?
|
age[2]
|
4
|
3
|
0x00FF2220
|
?
|
age[3]
|
5
|
4
|
0x00FF2224
|
?
|
age[4]
|
The values provided to initialize the array must
conform to the datatype of the array.
Address
of an array:
The array name is treated as its starting (or base) address.
Note that an array is contiguous in memory, i.e., it is allocated all the memory
in a single chunk. An array that is declared to hold 10 int values will be
allocated 4x10 = 40 bytes. The array name serves as the starting address.
Subsequent array elements are stored in increasing memory addresses.
int age[10];
cout << age;
This will display the address of the array (first
element) in hex; not the value of any particular element in the array.
Caution:
Array bounds
In C++ the array bounds must be taken care of by the
programmer. Changing values of elements at indices that are not within limits
(0 to one less than the size-of-array) would overwrite (i.e. change) the
adjacent memory bytes and may corrupt other variables! Even in the case of
read, we should be careful not to exceed the array bounds, otherwise we’re
liable to read other variables’ data, which could be a privacy breach.
Example
1: For
an array of integer values, the program below asks the user to enter an integer
value and then checks whether that value exists in the array or not. If it
does, the index at which the value is stored in the array is printed.
int arr[10] = {25, -10, 3, 4, 99, 100, 74,
-890, 0, 1}, val;
cout << "Enter an integer value:
"; cin >> val;
int index = -1;
for(int i = 0; i < 10; i++)
{
if(arr[i] == val)
index = i;
}
if(index >= 0)
cout << "Value found at
" << index << " index.";
else
cout << "Value not
found.";
cout << endl;
The algorithm used above for searching a value in an
array is known as Linear Search.
Task
# 1: Based on Example 1 above
I)
Print the average value of
the elements of the array arr.
II)
Print the average value of
the even elements of the array arr, the average of odd values, and the average
of the average of even and average of odd elements.
III)
Print the smallest value of
the array arr.
IV)
Challenge Task:
Print the smallest value of the array arr, then the smallest of the rest, then
the smallest of the rest, and so on until you have displayed the whole array in
ascending order. Hint: you need to use
nested loops -- for every iteration of an outer array that iterates 10 times
you need to iterate an inner array 10 times.
Code:
#include <iostream>
using namespace std;
int main()
{
int arr[10] = {25, -10, 3, 4, 99, 100, 74,
-890, 0, 1};
int
avg,evenavg,evensum=0,oddsum=0,oddavg,small,allavg,sum=0,n=0,reve,rodd,m=0;
for(int
i = 0; i < 10; i++)
{
sum=sum+arr[i];
}
avg=sum/10;
cout<<"Average
value of elements of array :"<<avg<<endl;
for(int
i = 0; i < 10; i++)
{
reve=arr[i]%2;
if
(reve==0)
{
n++;
evensum=evensum+arr[i];
}
}
evenavg=evensum/n;
cout<<"Even
element average is: "<<evenavg<<endl;
for(int
i = 0; i < 10; i++)
{
rodd=arr[i]%2;
if
(rodd==!0)
{
m++;
oddsum=oddsum+arr[i];
}
}
oddavg=oddsum/m;
cout<<"ODD
element average is : "<<oddavg<<endl;
allavg=(oddavg+evenavg)/2;
cout<<"average
of even an odd :"<<allavg<<endl;
int
min = arr[0];
for (int j = 0; j < 10; j++)
{
if (min > arr[j])
min = arr[j];
}
cout << "Smallest element : " << min;
for(int k=0; k<10; k++)
{
for(int j=k+1; j<10; j++)
{
if(arr[j] < arr[k])
{
int temp = arr[k];
arr[k] = arr[j];
arr[j] = temp;
}
}
}
cout << endl;
cout<<"Elements of array in ascending
order:"<<endl;
for(int i=0; i<10; i++)
{
cout<<arr[i]<<endl;
}
cout
<< endl;
}
Task
# 2: For an array of integer values, the program below asks
the user to enter the element # of the array to print. The program then
determines if the entered element is less than the size of the array. If not,
it keeps on asking user to enter element # equal to or less than the array
size. Once such number is entered, it prints out the element at that location.
Code:
#include <iostream>
using namespace std;
int main()
{
int size,n,x;
cout
<<"input size of array :";
cin>>size;
int
arr[size];
cout<<"Input the Index for
Ouput";
cin>>n;
x=n;
do
{
cout
<<"out of bound"<<endl;
cout<<"again
input according to size";
cin>>n;
}
while (n>size);
cout<<"value
found at :"<<n;
cout<<endl;
return 0;
}
Task # 3: Take an array of
10 elements. Write a code to find the occurance of a number in an array entered
by user. e.g.
1
|
5
|
4
|
1
|
1
|
3
|
2
|
5
|
1
|
5
|
Occurance of 1: 4
Code:
#include <iostream>
using namespace std;
int main()
{
int arr[10]={1,5,4,1,1,3,2,5,1,5};
int
n,oc=0;
cout<<"input
number to check occcurence: ";
cin>>n;
for(int
i = 0; i < 10; i++)
{
if
(arr[i]==n)
oc++;
}
cout<<"Occurence
of "<<n<<" in array is :"<<oc;
return
0;
}
Task
# 4: Take
an array of 9 elements. Split it into middle and store the elements in two
different arrays. e.g.
Initial array:
Initial array:
22
|
58
|
19
|
87
|
15
|
72
|
9
|
1
|
37
|
After splitting:
22
|
58
|
19
|
87
|
15
|
72
|
9
|
1
|
37
|
|
Code:
#include <iostream>
using namespace std;
int main()
{
int arr[9]={22,58,19,87,15,72,9,1,37};
int half[5],ndhalf[4];
cout
<<"1st Half"<<endl;
for
(int j=0; j<5;j++)
{
half[j]= arr[j];
cout<<half[j]<<'\t';
}
cout<<endl;
cout<<"2nd Half"<<endl;
for (int i=5; i<9; i++)
{
ndhalf[i]=arr[i];
cout<<ndhalf[i]<<'\t';
}
cout<<endl;
return 0;
}
Task
# 5: Store an array of 10 integers and write a code that
checks if the array is stored in ascending order or not.
Code:
#include <iostream>
using namespace std;
int main()
{
int arr[10];
int
j=0,n=0;
for(int
i = 0; i < 10; i++)
{
cout<<"input
"<<i <<" index element of array : ";
cin>>arr[i];
}
for(int
i = 0; i < 10; i++)
{
if
(arr[i]>arr[i+1])
{
n++;
}
else
{
j++;
}
}
if
(n>0)
{
cout<<"array
is not in ascending order";
}
else
{
cout<<"array
is in ascending order";
}
return
0;
}
No comments:
Post a Comment