Showing posts with label Problem Solving. Show all posts
Showing posts with label Problem Solving. Show all posts

Friday, 7 September 2018

Program to demonstrate using O_DIRECT option in file operations and writev() call

Program

/* Below program showcases the use of O_DIRECT file option. Also, demonstrates writev() use */

#include <iostream>
#include <fcntl.h>    /* For O_RDWR */
#include <unistd.h>   /* For open(), creat() */
#include <string.h>   /* For strlen() */
#include <malloc.h>   /* For memalign() */
#include <sys/uio.h>  /* For struct iovec */
#include <stdint.h>   /* For datatypes */

#define O_DIRECT_ENABLE 1
using namespace std;

int main(int argc, char *argv[])
{
        if (argc != 3) {
            cout << "run: ./<prog_binary_name> <input_file_name> <outputFileName>"<<endl;
            return 0;
        }

        int fd_out, fd_in, ret;
#if O_DIRECT_ENABLE
        fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC | O_APPEND | O_DIRECT, S_IRWXG | S_IRWXU);
        fd_in = open(argv[1], O_RDONLY | O_DIRECT);
#else
        fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC | O_APPEND, S_IRWXG | S_IRWXU);
        fd_in = open(argv[1], O_RDONLY);
#endif
        if(!fd_out || !fd_in) {
            cout << "Unable to open one of the given files"<<endl;
            return 0;
        }
        char file_data1[4096];
        char *file_data2 = (char*)memalign(4096, 4096);

        /* Below vector declaration won't compile since aligned_allocator defined in xilinx lib.
        vector<uint8_t, aligned_allocator<uint8_t>>  file_data3(4096);
        read(fd_in, file_data3, 4096);
        int file_data3_len = strlen(file_data2);
        */

        read(fd_in, file_data1, 4096); /* not work with O_DIRECT option, it returns 0 bytes */
        int file_data1_len = strlen(file_data1);

        /* works with O_DIRECT since memalign is used for creating the buffer */
        read(fd_in, file_data2, 4096);
        int file_data2_len = strlen(file_data2);

        cout<<"read_4k_nonMemAlign, read_4k_memAlign: "<<file_data1_len<<", "<<file_data2_len<<endl;

        char *data = (char*)malloc(sizeof(char)* 4096);
        strcpy(data, "salkfalksfjslfnsdlfksdklfnsldkfslkdfskldfjskldngslkdgjsdngslkdgskldglksdglksdglsdglsdgklsdfgdfkg");
        int data_len =  strlen(data);

        char *data2 = (char*)memalign(4096, 4096);
        strcpy(data2, "12345678912345678912345678912345678912345678912345678912345678912345678912345678912345678912345");
        int data2_len =  strlen(data2);

        ret = write(fd_out, data, data_len); /* fails with -1 if O_DIRECT enabled */
        cout<<"data(malloc) write ret: "<<ret<<endl;

        ret = write(fd_out, data2, 4096); /* write() works with O_DIRECT since data buffer is memaligned and given size is 4096 also aligned */
        cout<<"data2(memalign) write ret: "<<ret<<endl;

        ret = lseek(fd_out, data_len, SEEK_SET);
        cout<<"seek ret1: "<<ret<<endl;
        ret = write(fd_out, file_data2, 4096); /* write() works with O_DIRECT since buf is memaligned */
        cout<<"write_ret2: "<<ret<<endl;

        ret = lseek(fd_out, 0, SEEK_CUR);
        cout<<"seek ret2: "<<ret<<endl;

        struct iovec    iov[4];
        iov[0].iov_base = (char *) file_data2;
        iov[0].iov_len  = 4096;
        iov[1].iov_base = (char *) data2;
        iov[1].iov_len  = 4096;//data2_len;
        iov[2].iov_base = (char *) file_data1;
        iov[2].iov_len  = file_data1_len;
        iov[3].iov_base = (char *) data;
        iov[3].iov_len  = data_len;

        int total_len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len + iov[3].iov_len;
        cout<<"total_len: "<<total_len<<" {"<<data_len<<", "<<data2_len<<", "<<file_data1_len<<", "<<file_data2_len<<"}"<<endl;

/*        ret = writev(fd_out, &iov[0], 4); */
        ret = writev(fd_out, &iov[0], 2);
        cout<<"writev ret: "<<ret<<endl; /*writev() works fine when O_DIRECT flag removed in file open option. */
        ret = lseek(fd_out, 4096+data2_len, SEEK_SET);
        cout<<"seek ret: "<<ret<<endl;
        ret = write(fd_out, data, 4096); /* write() works with O_DIRECT since buf is memaligned */
        cout<<"ret: "<<ret<<endl;
        close(fd_out);
        close(fd_in);
        return 0;
}

Program Output

$g++ odirect_writev_prog.cpp -o odirect_writev_prog
$ ./odirect_writev_prog input.txt output_fd.txt
read_4k_nonMemAlign, read_4k_memAlign: 0, 4096
data(malloc) write ret: -1
data2(memalign) write ret: 4096
seek ret1: 96
write_ret2: -1
seek ret2: 96
total_len: 8288 {96, 95, 0, 4096}
writev ret: -1
seek ret: 4191
ret: -1

Tuesday, 31 October 2017

Generate Binary Numbers using Queue data structure

Generate Binary Numbers using Queue data structure

Problem Statement (from geeksforgeeks):

Given a number n, Write a program that generates and prints all binary numbers with decimal values from 1 to n.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N.

Output:
Print all binary numbers with decimal values from 1 to n in a single line.

Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 500

Example:
Input
2
2
5
Output
1 10
1 10 11 100 101

Solution:

/* Generating Binary Numbers of given input using Queue Data structure.
 * Implemented in C language.
 * Used Circular Queue as part of the implementation
 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct queue{
    int cap;
    int front, rear;
    int size;
    char array[1000][10];
};

struct queue* createQueue(int n){
    struct queue *q = (struct queue*)malloc(sizeof(struct queue));
    
    q->cap = n;
    q->front = -1;
    q->rear = n - 1;
    q->size = 0;
    return q;
}

int isQueueEmpty(struct queue *q){
    return q->size == 0;
}

int isQueueFull(struct queue *q){
    return q->size == q->cap;
}

void enQueue(struct queue *q, char *ch){
    if (isQueueFull(q))
        return;
    q->rear = (q->rear + 1) % q->cap;
    strcpy (q->array[q->rear], ch);
    q->size += 1;
}

char* deQueue(struct queue *q){
    if (isQueueEmpty(q))
        return NULL;
    q->front = (q->front + 1) % q->cap;
    q->size -= 1;
    return q->array[q->front];
    
}

void printQueue(struct queue *q){
    printf("Queue elements...\n");
    for (int i = 0; i < q->front; i++)
        printf("ele: %s\n", q->array[i]);
}
void toBinary(int n) {
    struct queue *q = createQueue(n);
    
    enQueue(q, "1");
    
    for (int i = 1; i <= n; i++) {
        char d1[10], d2[10];
        strcpy(d1, deQueue(q));
        printf("%s ", d1);
        strcpy(d2, d1);
        strcat(d1, "0");
        strcat(d2, "1");
        enQueue(q, d1);
        enQueue(q, d2);
    }
    printf("\n");
    free(q);
}

int main()
{
    // Note that size of arr[] is considered 100 according to
    // the constraints mentioned in problem statement.
    int arr[100], t, n;
 
    // Input the number of test cases you want to run
    scanf("%d", &t); 
 
    // One by one run for all input test cases
    while (t--)
    {
        // Input the size of the array
        scanf("%d", &n);
        toBinary(n);
    }
    return 0;
}

Thursday, 7 November 2013

[C Programming] Check if two strings are anagrams

Anagram: Two strings S1 and S2 are called anagrams when string S2 can be formed if we rearrange the letters in string S1 and vice-versa.


Example: S1 = god, S2 = dog

Solution:
------------
If we apply XOR property here then we can easily find the given strings are anagrams or not.

1 XOR 1 = 0
1 XOR 0 = 1
a XOR z = 1
s XOR s = 0

If Inputs to the XOR operator are equal then result would be ZERO.

Apply XOR for each alphabet in both strings. If the result is ZERO then the strings are anagrams else not anagrams. It may not be sufficient condition since the strings "aaa" and "bbb" are not anagrams but still the XOR of these two strings is ZERO. Hence, one more check was needed to eliminate these usecases. Compute the sum of ascii value of two strings and check if they are equal. If equal then the two given strings are anagrams.

Sample code (anagram_string.c):
--------------------------------------
  #include<stdio.h>
  #include<string.h>

  void main(int argc, char *argv[])
  {
          char *str1, *str2;
          int xor, i;
          int sum1 = 0, sum2 = 0;


          if ( argc != 3 ) /* argc should be 3 for correct execution */
          {
                  /* argv[0] is the program name */
                  printf( "usage: %s string1 string2\n", argv[0] );
                  return;
          }

          str1 = argv[1];
          str2 = argv[2];

          printf("Entered strings \"%s\" and \"%s\"\n", str1, str2);

          if (strlen(str1) != strlen(str2)) {
                  printf("Given Strings are not anagrams\n");
                  return;
          }

          for (i = 0; i < strlen(str1); i++) {
                  xor ^= str1[i];
                  xor ^= str2[i];
                  sum1 += str1[i];
                  sum2 += str2[i];
          }

          if (!xor && (sum1 == sum2))
                  printf("Given Strings are anagrams\n");
          else
                  printf("Given Strings are not anagrams\n");

  }

Output:
======
gcc anagram_string.c
./a.out ram arm

Entered strings "ram" and "arm"
Given Strings are anagrams

Wednesday, 6 November 2013

[C programming] Reverse an array in place (Do not use additional array)

Reverse an array in-place i.e you cannot use any additional array or
in other words, space complexity of the solution should be O(1)

#include<stdio.h>
#include<stdlib.h>

void main()
{
int size, i, temp;
int *array;

printf("Enter size of an array:\n");
scanf("%d", &size);

array = (int*)malloc(sizeof(int) * size);

printf("Enter %d elements\n", size);
for (i = 0; i < size; i++)
scanf("%d", &array[i]);

printf("List elements are..\n");
for (i = 0; i < size; i++)
printf("%d\n", array[i]);

for (i = 0; i <= (size - 1) / 2; i++) {
temp = array[i];
array[i] = array[size-1-i];
array[size-1-i] = temp;
}

printf("After Reversing the list,  elements are..\n");
for (i = 0; i < size; i++)
printf("%d\n", array[i]);
}

Tuesday, 29 October 2013

[C Programming] Implementation of Linked List in C

#include<stdio.h>
#include<stdlib.h>

struct node {
  int data;
  struct node *next;
};

void insert_node(struct node **Head)
{
struct node *iterate;
int data;

printf("Enter node data:");
scanf("%d", &data);

if(*Head == NULL) {
*Head = (struct node*)malloc(sizeof(struct node*));
(*Head)->data = data;
(*Head)->next = NULL;
} else {
  iterate = *Head;
  while(iterate->next != NULL)
   iterate = iterate->next;
   iterate->next = (struct node*)malloc(sizeof(struct node*));
   iterate->next->data = data;
   iterate->next->next = NULL;
}
}

void show_linked_list(struct node *Head)
{
struct node *iterate = Head;

if(!iterate) {
      printf("Linked List is empty\n");
return;
}

printf("Linked List elements are\n");

while (iterate != NULL) {
     printf("%d\n", iterate->data);
 iterate = iterate->next;
}
}

void isLL_ContainLoop(struct node *Head)
{
struct node *start, *end = NULL;

start = Head;

do {
  if (start != NULL) {
  if(start->next)
 start = start->next->next;
    else
              start = start->next;
        }

        if (end == NULL)
           end = Head;
        else
           end = end->next;
}while(start != end && start!= NULL);

if (start == end && start != NULL)
  printf("+++Linked List has LOOP+++\n");
    else
  printf("+++Linked List has NO LOOP+++\n");
}

void cleanup(struct node **Head)
{
  struct node *iterate = *Head;
  while(*Head)
  {
iterate = (*Head);
*Head = (*Head)->next;
iterate->next = NULL;
free(iterate);
}
}

struct node* search_node(struct node *Head, int data)
{
  while (Head) {
  if (Head->data == data)
   return Head;
  Head = Head->next;
  }
  return NULL;
}


void delete_node(struct node **Head)
{
  struct node *del_node = NULL;
  struct node *iterate = *Head;
  int data, find;
 
  if (*Head == NULL)
  printf("Linked List is empty\n");

  show_linked_list(*Head);
  printf("Select data to delete from linked list\n");
scanf("%d", &data);

del_node = search_node(*Head, data);
if (del_node) {
  printf("Data is present\n");

    if (del_node == *Head) {
        *Head = (*Head)->next;
        iterate->next = NULL;
        free(iterate);
    } else {
  while (iterate->next != del_node)
    iterate = iterate->next;
           iterate->next = iterate->next->next;
           del_node->next = NULL;
           free(del_node);
              }

} else {
  printf("Data is not present\n");
}
}

void main()
{
struct node *Head = NULL;
int choice, data;

while(1) {
printf("****Menu*****\n");
printf("select 1: Insert new node\n");
printf("select 2: show linked list elements\n");
printf("select 3: is Linked list contain loop\n");
printf("select 4: Delete node\n");
printf("select 5: Search node\n");
printf("select 6: Exit\n");
printf("Enter your input 1-6 ?\n");
scanf("%d", &choice);

switch(choice){
case 1: insert_node(&Head); break;
case 2: show_linked_list(Head); break;
case 3: isLL_ContainLoop(Head); break;
case 4: delete_node(&Head); break;
case 5: printf("Enter Data to search in Linked List\n");
scanf("%d", &data);
if (search_node(Head, data)) printf("Data is present\n");
   else printf("Data is not present\n");
    break;
case 6:
        default: cleanup(&Head); exit(1);
}
printf("=========================\n");
    }
}

Output:
-----------
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
1
Enter node data:11
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
1
Enter node data:22
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
2
Linked List elements are
11
22
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
3
+++Linked List has NO LOOP+++
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
5
Enter Data to search in Linked List
22
Data is present
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
4
Linked List elements are
11
22
Select data to delete from linked list
11
Data is present
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
2
Linked List elements are
22
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
6

Monday, 28 October 2013

[C Programming] Find roots of any quadratic equation

#include<stdio.h>
#include<math.h>

void main()
{
  int coefficient_a, coefficient_b, coefficient_c;
  int delta;
  float root1, root2;
  float temp;
 
  printf("Enter values for coefficients a, b and c\n");
  scanf("%d%d%d", &coefficient_a, &coefficient_b, &coefficient_c);
 
  printf("Entered expression is\n");
  printf("%dx^2 + %dx + %d\n", coefficient_a, coefficient_b, coefficient_c);
 
  if (!coefficient_a) {
  printf("Entered equation is not a quadratic equation, since coefficient_a is ZERO\n");
  return;
     }

  delta = (coefficient_b * coefficient_b) - (4 * coefficient_a * coefficient_c);
 
  if (delta > 0){
  /* Roots are Real numbers */
  printf("Roots are Real numbers\n");
  root1 = (-coefficient_b + sqrt(delta)) / (2 * coefficient_a);
  root2 = (-coefficient_b - sqrt(delta)) / (2 * coefficient_a);
 
  printf(" Roots are %f and %f\n", root1, root2);
  }
  else {
 /* Roots are imaginary numbers
  * roots = (-b +- i sqrt(4*a*c-b*b)) / (2*a)
  * temp = -b/2*a;
  * root1 = temp + i sqrt(4*a*c-b*b)/(2*a);
  * root2 = temp - i sqrt(4*a*c-b*b)/(2*a);
  */
 printf("Roots are imaginary numbers\n");
 temp = -coefficient_b / (2 * coefficient_a);
 root1 = sqrt(-delta) / (2 * coefficient_a);
   root2 = -sqrt(-delta) / (2 * coefficient_a);
   printf(" Roots are %f + i(%f) and %f + i(%f)\n", temp, root1, temp, root2);
  }
}

Output1:
---------
Enter values for coefficients a, b and c
1
5
6
Entered expression is
1x^2 + 5x + 6
Roots are Real numbers
 Roots are -2.000000 and -3.000000


Output2:
-----------
Enter values for coefficients a, b and c
1
2
8
Entered expression is
1x^2 + 2x + 8
Roots are imaginary numbers
 Roots are -1.000000 + i(2.645751) and -1.000000 + i(-2.645751)

[C Programming] Find Maximum and Minimum number in array of numbers

#include<stdio.h>
#define ARRAY_SIZE 100 /* Define Your ARRAY Size */

int find_max(int input[], int size)
{
  int max, index;
 
  max = input[0];
 
  for (index = 1; index < size; index++)
  {
if (max < input[index])
  max = input[index];
    }
    return max;
}

int find_min(int input[], int size)
{
  int min, index;
 
  min = input[0];
 
  for (index = 1; index < size; index++)
  {
if (min > input[index])
  min = input[index];
    }
    return min;
}

void main()
{
  /* We can dynamically allocate memory for an array using malloc() based on array_size requested from end user*/
  int set_of_numbers[ARRAY_SIZE];
  int size, index;
  int maximum, minimum;
 
  printf("Please enter number list size\n");
  scanf("%d", &size);
 
  if (size <= 0) {
  printf("Please enter valid size\n");
  return;
     }

  printf("Enter numbers\n");
  for (index = 0; index < size; index++)
  scanf("%d", &set_of_numbers[index]);

printf("List of numbers entered by user\n");
  for (index = 0; index < size; index++)
  printf("%d\n", set_of_numbers[index]);
 
  maximum = find_max(set_of_numbers, size);
  minimum = find_min(set_of_numbers, size);
 
  printf("Maximum number: %d\n", maximum);
  printf("Minimum number: %d\n", minimum);
}

Output:
-----------
Please enter number list size
5
Enter numbers
12
34
2
7
8
List of numbers entered by user
12
34
2
7
8
Maximum number: 34
Minimum number: 2

Monday, 15 July 2013

[C Programming #3] Print all combinations of given array with given size

Problem: Print all possible combinations of given array with given combination size limit.

Input:
array: 10 11 12 13 14
combination size: 3

Output:
10 11 12
10 12 13
10 13 14
11 12 13
11 13 14
12 13 14

Program:


  1. #include<stdio.h>

  2. void print_combinations(int a[], int n, int r) {
  3. int i,j,k;
  4. for (i = 0; i <= n-r; i++) {
  5. for (j = i+1; j+r-1 <= n; j++) {
  6. printf("%d\t", a[i]);
  7. for (k = 0; k < r-1; k++)
  8. printf("%d\t", a[j+k]);
  9. printf("\n");
  10. }
  11. printf("\n");
  12. }
  13. }

  14. int main()
  15. {
  16.   int a[] ={10,11,12,13,14};
  17.   int n=sizeof(a)/sizeof(a[0]);
  18.   print_combinations(a, n, 3);
  19.  
  20. return 0;
  21. }

Saturday, 13 July 2013

[C Programming #2] Calculate power of a number without using pow() pre-defined function

Calculate power of a number without using pow() pre-defined function
This question is also asked in interviews frequently.

Please check below program:

  1. #include<stdio.h>

  2. int main()
  3. {
  4.   int n, power;
  5.   int i, j;
  6.   int total = 0, sum;
  7.  
  8. printf("Please enter Value and its power\n");
  9. scanf("%d%d", &n, &power);
  10.  
  11. printf("Calculate: %d^%d:\n", n, power);
  12.  
  13. total = n;
  14. for (i=1; i<power; i++) {
  15. sum = total;
  16. for (j=1; j<n; j++) {
  17. total += sum;
  18. }
  19. }
  20. printf("Result: %d\n", total);


  21. return 0;
  22. }
Output:
Please enter Value and its power
10
3
Calculate: 10^3:
Result: 1000

[C Programming #1] Print 1 to 100 without using loops

How to print 1 to 100 numbers without using loops concepts (for/while/do-while) ?
This is a tricky question. Think Think Think.
..
..
..
..
Hint: We can use the concept of static variables to do this task.
Think
..
..
..
Here is the program, which solves the problem.

  1. #include<stdio.h>

  2. int main()
  3. {
  4.   static int i = 0;

  5. printf("%d\n", i++);
  6. if (i<=100)
  7.   main();


  8. return 0;
  9. }
Output: 1 to 100 numbers printed line by line.

Now, we can made a small change in the program to display numbers from x to y;
Here is the program to do this task.

  1. #include<stdio.h>

  2. int main()
  3. {
  4.   static int i = 0, x ,y;

  5. if (i == 0) {
  6.     printf("Please enter two numbers, x should be <= y\n");
  7.   printf("x = ");
  8.   scanf("%d", &x);
  9.   printf("y = ");
  10.     scanf("%d", &y);
  11.     if (x > y) {
  12.     printf("x should be less than or equal to y.\n");
  13.   return 0;
  14.     }
  15. }

  16. printf("%d\n", x+(i++));
  17. if ((x+i)<=y)
  18.   main();

  19. return 0;
  20. }
Output: Numbers are displayed from X to Y line bye line:
Example output:
Please enter two numbers, x should be <= y
x = 12
y = 17
12
13
14
15
16
17

Example output_2:
Please enter two numbers, x should be <= y
x = 12
y = 6
x should be less than or equal to y.

Sunday, 7 July 2013

[Problem Solving #1] Project Team (InMobi Challenge question)

A Professor of Physics gave projects to the students of his class. The students have to form a team of two for doing the project. The professor left the students to decide the teams. The number of students in a class will be even.
Each student has a knowledge level. It tells how much knowledge each student has. The knowledge level of a team is the sum of the knowledge levels of both the students.The students decide to form groups such that the difference between the team with highest knowledge and the one with lowest knowledge is minimum.InputFirst line of the input will contain number of test cases t; In the next t lines the first number is n the number of students in the class followed by n integers denoting the knowledge levels of the n studentsOutputYour output should be a single line containing the lowest possible difference between the team with highest knowledge and the one with lowest knowledge.
Sample Input
2
4 2 6 4 3
6 1 1 1 1 1 1
Sample Output
1
0
Explanation
Input Constraints are

1 <= t <= 100

1 <= n <= 100
1 <= knowledge level <= 10000

Program in C:


  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int comp(const void *a,const void *b) {
  4.        int *x = (int *) a;
  5.        int *y = (int *) b;
  6. return *x - *y;
  7. }

  8. int find_team_gk(int student_gk[], int count){
  9.   int start=0, end=count-1;
  10. int team_gk[(start+end+1)/2];
  11. int t;
  12. qsort(student_gk, count, sizeof(int), comp);
  13. for (t=0; start+1<=end;t++) {
  14. team_gk[t] = student_gk[start] + student_gk[end];
  15. start++;
  16. end--;
  17. }
  18. if (team_gk[0] > team_gk[1]) 
  19. return (team_gk[0] - team_gk[1]);
  20. else
  21. return (team_gk[1] - team_gk[0]);
  22. }

  23. int main()
  24. {
  25. int testcases, students;
  26.   int *student_gk, *output;
  27.   int i, s;

  28. printf("number of testcases:");
  29. scanf("%d", &testcases);
  30. output = (int *)malloc(testcases * sizeof(int));

  31.    for (i=0; i< testcases; i++) {
  32. printf("Enter number of students and their gk level in order:"):
  33.      scanf("%d", &students);
  34.      student_gk = (int *)malloc(students * sizeof(int));
  35.      for (s=0; s<students; s++) {
  36.      scanf("%d", &student_gk[s]);
  37.      }
  38.      }
  39.       for (i=0; i<testcases; i++)
  40. output[i] = find_team_gk(student_gk, students);

  41.  for (i=0; i<testcases; i++)
  42.      printf("%d\n", output[i]);
  43. return 0;
  44. }



You might also like

Related Posts Plugin for WordPress, Blogger...