Chapter - 9: Arrays

Given an array p[5], write a function to shift it circularly left by two positions. Thus, if p[0] = 15, p[1]= 30, p[2] = 28, p[3]= 19 and p[4] = 61 then after the shift p[0] = 28, p[1] = 19, p[2] = 61, p[3] = 15 and p[4] = 30. Call this function for a (4 x 5 ) matrix and get its rows left shifted.


#include<stdio.h>
#include<conio.h>

void shift(int *base)
{
	int *web, fir, sec, i;
	web = base;
	fir = *base;//saving first value in fir variable
	sec = *(base + 1);//saving second value insec variable

	for (i = 0; i<3; i++)//shifting the values by saving them in next to next addresses
		*(web + i) = *((base + 2) + i);

	*(web + 3) = fir;
	*(web + 4) = sec;
}

int main()
{
	int a[5], i;

	printf("Enter 5 numbers : ");
	for (i = 0; i<5; i++)//scanning values
		scanf("%d", &a[i]);

	shift(a);//calling function

	printf("\n\nList after shifting it's rows by two positions.\n\n");
	for (i = 0; i<5; i++)//printing values after shifting

		printf("%d ", a[i]);
	_getch();
	return 0;
}

© 2021 Garbage Valuegarbage value logo