Placement Questions in C++

Placement Questions in C++

Here are some questions which is very helpful in preparing for On campus or Off campus placement. 

Arrange Array or List in Ascending Order in C++

#include<iostream>
using namespace std;

int * ArrangeArray(int *arr, int len)
{
  int i,j,temp;
  
  for(i=0; i<len; i++)
  {
    for(j=i+1; j<len; j++)
    {
      if (arr[i]>arr[j])
      {
         temp=arr[i];
	 arr[i]=arr[j];
	 arr[j]=temp;
      }
    }
  }
  return arr;
}

int main()
{
  int i, arr[] ={1, 54, 65, 12, 0, 98};
  int len=(sizeof(arr)/sizeof(int));
  
  int *arr1 = ArrangeArray(arr, len);
  
  for(i=0; i<len; i++)
  {
    cout << arr1[i] << " ";
  }
    return 0;
}
Output:
0 1 12 54 65 98 

Arrange Array or List in Descending Order in C++

#include<iostream>
using namespace std;

int * ArrangeArray(int *arr, int len)
{
  int i,j,temp;
  
  for(i=0; i<len; i++)
  {
    for(j=i+1; j<len; j++)
    {
      if (arr[i]<arr[j])
      {
         temp=arr[i];
	 arr[i]=arr[j];
	 arr[j]=temp;
      }
    }
  }
  return arr;
}

int main()
{
  int i, arr[] ={1, 54, 65, 12, 0, 98}, len=(sizeof(arr)/sizeof(int));
  
  int *arr1 = ArrangeArray(arr, len);
  
  for(i=0; i<len; i++)
  {
    cout << arr1[i] << " ";
  }
    return 0;
}
Output:
98 65 54 12 1 0 

Print Maximum number of Array or List in C++

#include<iostream>
using namespace std;

int MaxOfArray(int * arr, int len)
{
  int i,max;
  max=arr[0];
  
  for(i=1; i<len; i++)
  {
    if (arr[i]>max)
    {
      max = arr[i];
    }

  }
 return max;
}

int main()
{
  int arr[] ={1, 54, 65, 12, 0, 98},i;
  int len=(sizeof(arr)/sizeof(int));
  cout << MaxOfArray(arr, len);
}
Output:
98

Previous
Next Post »