1. Write a program to swap all the elements in the 1st column with all the corresponding elements in the last column, and 2nd column with the second last column and 3rd with 3rd last etc. of a 2-D array by using functions & dynamic memory allocation. Display the matrix.
2. Given array of integer, write a program to find the next smaller of next greater element of every element in array by using functions & dynamic memory allocation. Elements for which no greater element exists or no smaller of greater element exist, print -1.
Sample Input & Output
Input : arr[] = {5, 1, 9, 2, 5, 1, 7}
Output: 2 2 -1 1 -1 -1 -1
Explanation :
Next Greater -> Right Smaller
5 -> 9 9 -> 2
1 -> 9 9 -> 2
9 -> -1 -1 -> -1
2 -> 5 5 -> 1
5 -> 7 7 -> -1
1 -> 7 7 -> -1
7 -> -1 -1 -> -1
Input : arr[] = {4, 8, 2, 1, 9, 5, 6, 3}
Output : 2 5 5 5 -1 3 -1 -1
//Question 1
//Question 1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//Implement function Input Matrix
void InpMat(int** mat, int rows, int cols)//
{
printf("Please enter elements matrix:\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
scanf("%i", &mat[i][j]);
}
}
//Implement function Out matrix
void OutMat(int** mat, int rows, int cols)//
{
printf("===============================================================\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
printf("%3d", mat[i][j]);
printf("\n");
}
}
//Function ChangeMat -swap all the elements in the 1st column with all the corresponding elements in the last column....
//Formal give Matrix- r-rows,c-columns swap all element 1-columns with c columns ..
void ChangeMat(int** mat, int rows, int cols)
{
int l = 0;
int r = cols-1;
while (l < r)
{
for (int i = 0; i < rows; i++)
{
int temp = mat[i][l];
mat[i][l] = mat[i][r];
mat[i][r] = temp;
}
l++;
r--;
}
}
int main()
{
//Alloc dynamic memory
int rows, cols;
printf("Please enter rows and columns of matrix:\n");
scanf("%i%i", &rows, &cols);
int** mat = (int**)malloc(sizeof(int*) * (size_t)rows);//size_t=unsigned int
for (int i = 0; i < rows; i++)
mat[i] = (int*)malloc(sizeof(int) * (size_t)cols);
InpMat(mat, rows, cols);
ChangeMat(mat, rows, cols);
OutMat(mat, rows, cols);
free(mat);
return 0;
}
//Question 2
//Question 1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
//Next Greater Right Smalllaer function
int* NxtGreat(int* ar, int n)
{
int* ans = (int*)malloc(sizeof(int) * (size_t)n);
ans[n - 1] = ans[n - 2] = -1;
for (int i = 0; i < n - 2; i++)
{
int qq = -1;
for (int j = i ; j < n - 1; j++)
{
if (ar[i]<ar[j] && ar[j]>ar[j + 1])
{
ans[i] = ar[j + 1];
qq = 0;
break;
}
}
if (qq ==-1)
ans[i] = -1;
}
return ans;
}
int main()
{
printf("Please enter size array(n) : ");
int n;
scanf("%i", &n);
int* arr = (int*)malloc(sizeof(int) * (size_t)n);
printf("Please input all elements of array\n");
for (int i = 0; i < n; i++)
scanf("%i", &arr[i]);
int* ans = NxtGreat(arr, n);
for (int i = 0; i < n; i++)
printf("%3d", ans[i]);
printf("\n");
return 0;
}
Comments
Thanks a lot !!
Leave a comment