Pages

Showing posts with label wipro. Show all posts
Showing posts with label wipro. Show all posts

Sunday

wipro coding question

Wipro Coding questions 

1) Find the distinct elements in a given array. (Assume size of an array n<=20)
Sample Input:
  • 9 = size of an array 
  • 2 3 4 5 6 1 2 3 4  =  array elements
Sample Output:
  • 2 3 4 5 6 1
Program:


// C program to print all distinct elements in a given array
#include
void distict_elements(int a[], int n);
int main()
{
int size_array, i, arr[20];
// Get the array size
scanf(“%d”, &size_array)
// Get the array elements
for(i=0; i<size_array; i++)
{
scanf(“%d”, &arr[i]);
}

// Function call to print the distinct elements in an array
distict_elements(arr, size_array);
return 0;
}
void distict_elements(int a[], int n)
{
int i, j;
// Pick all elements one by one
for (i=0; i<n; i++)
{
// Check if the picked element is already printed
for (j=0; j<i; j++)
{
if (a[i] == a[j])
break;
}

// If not printed earlier, then print it
if (i == j)
printf(“%d “, a[i]);
}
}

wipro coding question

2) Program to sort array in ascending & descending order.
Input:
5
8 6 9 2 7
Output:
2 6 7 8 9
9 8 7 6 2
Program:

// C program to sort the given array elements in ascending and
//descending order
#include<stdio.h>
int main(void)
{
int arr[10], i=0, j=0, size, temp;
// Get the size of an array
scanf (“%d”, &size);
// Get the array elements as an input
for (i = 0; i <size; i++)
{
scanf (“%d”, &arr[i]);
}
// Sorting elements in ascending order
for (j=0 ; j<(size-1) ; j++)
{
for (i=0 ; i<(size-1) ; i++)
{
if (arr[i+1] < arr[i])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
// Print the elements in ascending order
for (i=0 ; i {
printf (“%d “, arr[i]);
}
printf(“\n”);

// Print the elements in descending order
for (i=size-1; i>=0 ; i–)
{
printf (“%d “, arr[i]);
}
return 0;
}