Faculty of Engineering
ECE 308 Data Structure
Second Term
2023 / 2024
Dr. Ahmed Elshafeey
Eng. Reem Elgeddawy
Third Year
Communications
Department
Sheet #1
Arrays
and
Computer
Arrays
1. (a) Given the declarations: int sample [8]; int i,k;
Show the contents of the array sample after the following program segment is executed:
for (i=0; i<14; i++) sample [i] = 20 – i;
for (k = 7; k>3; k--) sample [k]=30+2*k;
(b) By using the same declaration given in (a), show the contents of the array sample after the following
program segment is executed:
k = 4;
for (i=0; i<=7; i++)
if (i<=3)
sample [i]=i+k;
else
sample [i]= i*i – k;
(c) What happens if the for statement in (b) is changed to: for (i=0; i<=10; i++)?
2. The following loop changes the contents of the array X. Write the final contents of the array after execution
of this loop. What is the main function of this loop?
for (j=0;j<= 3; j++)
for (k=j; k <= 4; k++)
if (X[k] <X[J])
{
current = x[j];
X[j] = X[k];
X[k] = current;
}
Where Array X = [ 66, 55, 83, 39, 26] as initial value.
// Program Search
#include <iostream>
using namespace std;
int main()
{int list [100];
int i, number, n;
cin>> number >> n;
for (i = 0; i<= n – 1; i++) cin
3.
>> list [i];
for (i = 0; i<= n – 1; i++)
(a) Write the output
of the==program,
if (number
list[i]) if the input data is: 5 6 -3 5 -4 0 5 5
(b) What
thenumber
output, <<
if the
coutis <<
"isinput
founddata is changed to:
in position "<<i<< endl;
return 0;}
Page 1 of 3
(i)
5 6 -3 7.3 5.8 0 5 8.3
(ii)
5 8 -3 7 -4 0 5 8
(iii) 5 6 -3 7 5 0 3 5 12 5 17
(c) What is the output, if the input data is changed to: 5 6 -3 7 -4 0 3 8 What is the meaning of this output?
4. Consider the following program segment:
// Program test
#include<iostream>
using namespace std;
int main()
{
int y[5];
int i,n,j,item;
bool done;
cin>>item>>n;
for(i=0;i<=n-1;i++) cin>>y[i];
done=false; j=1;
while((j<=n) && !(done))
if ……….
j++;
else
done=true;
if ………..
cout<<item<<"is not found "<<endl;
else
cout<<item<<"is found at index "<<j<<endl;
return 0;
}
(a) The program should search the array, Y, to see if a specific value is present. Assuming that the array Y
is unsorted, write the missing conditions of the if statements.
(b) If the input is 9 4 5 11 9 7, what is the output?
(c) If the input is 8 5 10 7 9 4 11, what is the output?
(d) If the input is 8 6 11 3 8 5 6 7, what is the output?
(e) Assuming that the array Y is sorted into an ascending order, write the new missing conditions of the if
statements.
Page 2 of 3
// Program Search
#include <iostream>
using namespace std;
int main()
{int list [100];
int i, number, n;
cin>> number >> n;
for (i = 0; i<= n – 1; i++) cin >> list [i];
for (i = 0; i<= n – 1; i++)
if (number == list[i])
cout << number << "is found in position "<<i<< endl;
return 0;}
Page 3 of 3