Latest :

C Program : Convert An Array Into a Zig-Zag Fashion

The given problem statement is, to write a C program to convert the array into zig-zag fashion i.e., converting it in the form where a<b>c<d>e<f. The input requirement for this is the number of elements (or size) in the array along with the data values or elements of the array. Our desired resultant output is an array converted into zig-zag format.

Convert array into Zig-Zag fashion Using Function

The first step always is to gather all the necessary inputs. To read inputs at runtime, we can make use of predefined input function scanf().

This function reads input of any primitive datatype like int, float, char, etc., at runtime and based on the format specifier mentioned, the type of input being read is determined.

For integer type, the format specifier used id ‘%d’. Firstly, we’ll read the size or number of elements of the array (n) following which we’ll create an array of the same size. After this, we read the elements or data values of the array by iterating till the end.

int n,i;

printf(“\nEnter the size of an Array : “);

scanf(“%d”,&n);

int a[n];

printf(“\nEnter the %d distinct elemens in array:\n”,n);

for(i=0;i<n;i++) {

scanf(“%d”,&a[i]);

}

Then, we make a function call to a user-defined function (zigzag) and pass the array and size as parameters. To convert it into zigzag fashion, we’ll only have to check alternate numbers for the condition and to do so we’ll use a flag variable (temp) which is initially 1.

Now, we iterate through the array from beginning (o) to the last but one number (n-2) and for every iteration, we first check whether the temp is 0 or 1.

As we can observe for the first two values a[i] should be less than a[i+1] then, the next two a[i] should be greater than a[i+1]. This continues alternatively. So to implement this, when the temp is 1 then, we check whether a[i] is grater than a[i+1].

If yes then, we swap the two by calling the swap() function. If that condition is not the case then, we leave it as it is. Then, we change the temp value to 0.

if(temp) {

if (a[i] > a[i+1])

swap(&a[i], &a[i+1]);

temp=0;

}

If the temp value is zero then, it checks whether a[i] is less than a[i+1]. If yes then, it swaps using the user-defined swap() function else, it leaves it as it is.

Then, it changes the temp value to 1. This way, since the temp value keeps changing alternatively from 0 to 1, the corresponding conditions are checked.

else {

if (a[i] < a[i+1])

swap(&a[i], &a[i+1]);

temp=1;

}

}

In the user-defined swap function, we pass the address of the values to be swapped. We use a temporary variable (temp) to swap the two. Firstly, we’ll assign temp the value of a[i] then, we assign a[i] the value of a[i+1].

Later, we assign the original value of a[i] (temp) to a[i+1]. This way, after completion of this function, a[i] and a[i+1] would have swapped itself.

void swap(int *a, int *b) {

int temp;

temp = *a;

*a = *b;

*b = temp;

}

So by the end of all the iterations in the zigzag function and when it returns back to main method, the array passed as parameter would now have been converted into zig-zag fashion.

This is our desired resultant output which will be displayed on the console screen using printf().

for(int i=0; i<n; i++)

printf(“%d “,a[i]);

Output:

Standard Method

We can observe that, we have made use of functions above and have discussed the logic to convert the array into zig-zag fashion. The main advantage of using functions is that, it makes the code reusable and enhances the readability.

If the same logic is needed elsewhere then, if it were place within a function then, just by making a function call we can reuse it instead of rewriting it.

Also, when we place things in separate function block based on its functionality, the code looks a lot cleaner.

But, that doesn’t necessarily mean that, we shouldn’t write the entire code within main method. Be it using functions or if we are writing within main method itself, if the logic is the same then, both the time and space complexity is the same.

So, if we are sure that, we don’t need a particular logic anywhere else in the code then, there is no harm in writing within main block.

In this code, the zigzag() function is not required again in the code while the swap function is used again, so, instead of writing logic in zigzag() function, we can just write it within main while, the swap() function can be placed in the function itself as shown below.

Output:

x

Check Also

C Program To Find Reverse Of An Array – C Programs

C program to find the reverse of an array – In this article, we will ...