Uploaded by Omar Mahafeza

14361

advertisement
Jadara University - CS Department
Exercise 2 / PL2 2020/2021-1
Dr. Sami && Dr. Mutaz
1. Consider the following declaration:
double salary[10];
In this declaration, identify the following:
a. The array name.
b. The array size.
c. The data type of each array component.
d. The range of values for the index of the array.
2. Write C++ statements to do the following:
a. Declare an array alpha of 15 components of type int.
b. Output the value of the tenth component of the array alpha.
c. Set the value of the fifth component of the array alpha to 35.
d. Set the value of the ninth component of the array alpha to the sum of the sixth and thirteenth
components of the array alpha.
e. Set the value of the fourth component of the array alpha to three times the value of the eighth
component minus 57.
f. Output alpha so that five components per line are printed.
3. What is the output of the following program segment?
int temp[5];
for (int i = 0; i < 5; i++)
temp[i] = 2 * i - 3;
for (int i = 0; i < 5; i++)
cout << temp[i] << " ";
cout << endl;
temp[0] = temp[4];
temp[4] = temp[1];
temp[2] = temp[3] + temp[0];
for (int i = 0; i < 5; i++)
cout << temp[i] << " ";
cout << endl;
4. Suppose list is an array of five components of type int.
What is stored in list after the following C++ code executes?
for (int i = 0; i < 5; i++){
list[i] = 2 * i + 5;
if (i % 2 == 0)
list[i] = list[i] - 3; }
5. Suppose list is an array of six components of type int. What is stored in
list after the following C++ code executes?
list[0] = 5;
for (int i = 1; i < 6; i++){
list[i] = i * i + 5;
if (i > 2)
list[i] = 2 * list[i] - list[i - 1]; }
Jadara University - CS Department
Exercise 2 / PL2 2020/2021-1
Dr. Sami && Dr. Mutaz
6. Correct the following code so that it correctly initializes and outputs the elements
of the array myList;
int myList[10];
for (int i = 1; i <= 10; i++)
cin >> myList;
for (int i = 1; i <= 10; i++)
cout << myList[i] << " ";
cout << endl;
7. Determine whether the following array declarations are valid.
a.
b.
c.
d.
e.
int a[5] = {0, 4, 3, 2, 7};
int b[10] = {0, 7, 3, 12};
int c[7] = {12, 13, , 14, 16, , 8};
double lengths[] = {12.7, 13.9, 18.75, 20.78};
char name[8] = "Samantha";
8. Suppose that you have the following declaration:
int list[10] = {8, 9, 15, 12, 80};
What is stored in each of the components of list?
9. What is the output of the following code?
int list[] ={6, 8, 2, 14, 13};
for (int i = 0; i < 4; i++)
list[i] = list[i] - list[i + 1];
for (int i = 0; i < 5; i++)
cout << i << " " << list[i] << endl;
10. Consider the following function heading:
void tryMe(int x[], int size);
and the declarations:
int list[100];
int score[50];
double gpas[50];
Which of the following function calls is valid?
a. tryMe(list, 100);
b. tryMe(list, 75);
c. tryMe(score, 100);
d. tryMe(score, 49);
e. tryMe(gpa, 50);
4/11/2020
Download