Specification: Assume that two arrays, x and y, are both fifty-element arrays and that they contain double values. This function is supposed to switch the lowest x value with the lowest y value; that is, the lowest value in x is switched with the lowest value in y.

```
void Switcher(double x[], double y[])
{
double low_x = 0, low_y = 0;
int i, i_x, i_y;
for(i = 0; i < 50; ++ i)
{
if(x[i] < low_x)
low_x = x[i]; //find low x
i_x = i; //remember x index
if(y[i] < low_y)
low_y = y[i]; //find low y
i_y = i; //remember y index
}
y[i_y] = low_x;
x[i_x] = low_y;
}
```


This routine initially sets the low values to 0.0, and we do not know the range of values for these arrays. Chances are, unless the arrays contain all negative values, we will not find the correct low values.

A second problem is due to the fact that both if statements do not contain { }, both assignment statements into i_x and i_y will be performed at each pass of the loop. The resulting action is that the last two values always will be switched.

There are three corrections. First set the initial low values to the first element in the arrays, set the initial index values to 0, and use the { } with the if statements.

```
double low_x = x[0], low_y = y[0];
int i, i_x = 0, i_y = 0;

for(i = 0; i < 50; ++ i)
{
if ( x[i] < low_x)
{
low_x = x[i];
i_x = i;
}
// same for the y array
}
```
After the for loop, we must then assign the low values into the opposing arrays, like this:

x[i_x] = low_y;
y[i_y] = low_x;

Computer Science & Information Technology

You might also like to view...

Which method may be used to determine the speed of a processor?

A) Read the BIOS technical documentation. B) Right-click My Computer or Computer and select Properties. C) Read it off the heat sink mounted on top of the CPU. D) Read it from the sticker attached to the outside of the computer.

Computer Science & Information Technology

Unlike the default format for text in a text box, the default alignment for shapes with text in them is _____ text.

A. left-aligned B. right-aligned C. center-aligned D. top-aligned

Computer Science & Information Technology

The apps listing that appears on Apps and features screen includes Windows 10 apps installed along with the operating system.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

Which of the following is NOT a described IDPS control strategy?

A. centralized B. fully distributed C. partially distributed D. decentralized

Computer Science & Information Technology