SER5

 #include<stdio.h>
void thirdLargest(int arr[], int arr_size)
{int t,j,i,b;
  for(i=arr_size-1;i>=0;i--)
{
for(j=0;j<i;j++)
{
if(arr[j]>arr[j+1])
{
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
for(i=0;i<arr_size;i++)
b=arr[arr_size-3];
printf("The third Largest element is %d",b);
}


int main( )
{
int a[20],n,i;
scanf("%d\n",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
  thirdLargest(a,n);

return 0;
       }

11 comments:

  1. Command failed: ./a.out <input.txt
    Segmentation fault

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. #include
    using namespace std;

    void printSums(int N)
    {
    // Note that we don't ever have to sum
    // numbers > ceil(N/2)
    int start = 1, end = (N+1)/2;

    // Repeat the loop from bottom to half
    while (start < end)
    {
    // Check if there exist any sequence
    // from bottom to half which adds up to N
    int sum = 0;
    for (int i = start; i <= end; i++)
    {
    sum = sum + i;

    // If sum = N, this means consecutive
    // sequence exists
    if (sum == N)
    {
    // found consecutive numbers! print them
    for (int j = start; j <= i; j++)
    printf("%d ", j);

    printf("\n");
    break;
    }

    // if sum increases N then it can not exist
    // in the consecutive sequence starting from
    // bottom
    if (sum > N)
    break;
    }
    sum = 0;
    start++;
    }
    }

    // Driver code
    int main(void)
    {
    int N;
    cin>>N;
    printSums(N);
    return 0;
    }

    ReplyDelete
    Replies
    1. First line*** #include
      4th last line*** scanf("%d",&N);
      And then the program runs

      Delete
  6. SER5
    QUESTION DESCRIPTION

    Ramesh is conducting an entertiment even for students. During break tiime, he need to ask few mind oriented questions. He asked the questions to participants like , he will tell the number and paricipants need to tell possible sum of given number N. The task is to print all possible sums of consecutive numbers that add up to N. Example Input: 125 possible output: 8 9 10 11 12 13 14 15 16 17
    23 24 25 26 27
    62 63

    Mandatory: To print all Possible Sums , participants should be use the following function signature "void printSums(int N)"
    TEST CASE 1

    INPUT
    125
    OUTPUT
    8 9 10 11 12 13 14 15 16 17
    23 24 25 26 27
    62 63
    TEST CASE 2

    INPUT
    100
    OUTPUT
    9 10 11 12 13 14 15 16
    18 19 20 21 22



    code::::

    #include
    void printSums(int N)
    {
    int start = 1, end = 1;
    int sum = 1;
    int i;

    while (start <= N/2)
    {
    if (sum < N)
    {
    end += 1;
    sum += end;
    }
    else if (sum > N)
    {
    sum -= start;
    start += 1;
    }
    else if (sum == N)
    {
    for (i = start; i <= end; ++i)
    printf("%d ", i);

    printf("\n");
    sum -= start;
    start += 1;
    }
    }
    }

    int main()
    {
    int number;
    scanf("%d", &number);
    printSums(number);
    return 0;
    }

    ReplyDelete

SRM ELAB SOLUTUONS   DATA-STRUCTURE                                                                             **IF THE PROGRAM DON...