Sei sulla pagina 1di 3

Practice questions

Task 1: Dry run the following code and give the values of the variables using table as discussed:

int start =7 , end =10;


int a[11]= {51,52,53,54,55,56,57,58,59,60,61};

for( int i= start; i<= end-1 ;i=i+1)


{
for( int j= end;j>= i; j=j-1)
{
temp = a[i];
a[i]=a[j]+10;
a[j]= temp/2;
}
}

Task 2: Dry run the following code, and provide array values after each call of printarray function.

int a[5] = {10, 20, 30, 40, 50}, size = 5;


printarray(a, size);
//array values?

cout << "at which index you want to insert data : ";
cin >> ind;
cout << "Enter data you want to put in array : ";
cin >> data;

int b[6], i=0;


size++;
for (i; i<ind; i++)
b[i] = a[i];

b[i] = data;

for (i; i<size; i++)


b[i] = a[i-1];
//dry run of loop

printarray(b, size);
//array values?

Task 3: Dry run following programs and show contents of array 2 after execution.
void function1(int a[], int n1, int b[], int n2, int c[])
{
int i=0, j=0, k=0;
while (i<n1 && j<n2)
{
if(a[i]) < b[j]) { c[k] = a[i]; I = i+1; }
else { c[k] = b[j]; j= j+1; }
k = k + 1;
}
while (i < n1) { c[k] = a[i]; i = i+1; k = k+1;}
while (j<n2) { c[k] = b[j]; j = j+1; k = k+1}
}

int main()
{
cons tint array_size = 4;

int x[array_size] = {2,3,9,18};


int y[array_size] = {5,8,20,25};
int z[2*array_size] = {)};
function1(x, array_size, y, array_size, z);

return 0;
}

Task 4: Dry run following programs and show contents of array 2 after execution
void function1(int a[], int n1, int b[], int n2, int c[])
{ Int i=0, j=0, k=0;
while (i<n1 && j<n2)
{
if(a[i] < b[j]) { c[k] = a[i]; I = i+1; }
else { c[k] = b[j]; j = j+1; }
k = k+1;
}
while (i < n1) { c[k] = a[i]; i = i+1; k = k+1;}
while(j < n2) { c[k] = b[j]; j = j+1; k = k+1}
}

int main()
{
cons tint array_size = 4;
int x[array_size] = {3, 4, 10, 20};
int y[array_size] = {6, 9, 21, 27};
int z[2*array_size] = {0};
function1(x, array_size, y, array_size, z);

return 0;
}

Task 5: Write a function with an array of 10 values. The function also receives the range of indices. It sums all array
entries between the given range and returns the result.

For example, if the received array is like following


10 5 8 2 3 17 8 9 14 5

And the starting index is 3, and ending index is 6. Then, the result returned is the sum of entries between index 3 and
index 6 is, which is 30 in this example.

Task 6: Write a C++ program with an array a of 10 values, filled with random numbers. The program copies this array a
into another array b in the following fashion.
1- First 5 elements of a are placed into the last 5 indexes of b in ascending sort.
2- Last 5 elements of a are placed into the first 5 indexes of b in reverse order.
For example, first 5 elements of a are colored, and the last 5 are not
10 5 8 2 3 17 8 9 14 5

in b they become
5 14 9 8 17 2 3 5 8 10

Task 7: Write a C++ program that deletes the first occurrence of a single character entered by user from a character
array starting from left side (index 0) of array.

Task 8: Improve Task 7 such that it not only deletes the first occurrence, but now deletes ALL occurrences of the
character entered by user from the character array.

Potrebbero piacerti anche