Assignment 4 Question 1

Self done solution

#include <stdio.h>
int read(int arr[]){
 int i,n;
 scanf("%d",&n);
 i=0;
 while(i<n){
  scanf("%d", &arr[i]);
  i++;
 }
 return n;
}
int lowest(int* ptr,int* ptr1, int n, int n3){
 int i=0;
 int j=0;
 int *ptr2;
 int low;
 ptr2=ptr1;

 while(i<n){
  if(*ptr==*ptr1){
   i++;
   ptr= ptr+1;
   ptr1=ptr2;
   j=0;
  }
  if (j==n3){
   break;
  }
  ptr1 = ptr1 + 1;
  j++;
 }
 low=*ptr;
 return low;
}
int main(){
 int a[20];
 int n1,low1;
 n1=read(a);
 int b[20];
 int n2;
 n2=read(b);
 low1=lowest(a,b,n1,n2);

 if (low1==0){
  printf("NO");
 }else{
  printf("%d", low1);
         }
 return 0;
}

previous year solution

 #include<stdio.h>
       #define MAX 20
       int read_array(int arr[])
       {
        int i, n;
        scanf("%d", &n);
        for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);
        return n;
       }

      int present(int arr[], int n, int elt) {
      int i;
      for (i = 0; i < n; i++) {
         if (arr[i] == elt) {
            return 1;
          }
      }
      return 0;
   }
    int main() {
    int arr1[MAX], n1;
    int arr2[MAX], n2;
    n1 = read_array(arr1);
    n2 = read_array(arr2);
    int i, small_np = 0, flag = 0;
    for (i = 0; i < n1; i++) {
        if (!present(arr2, n2, arr1[i])) {
            if (!flag || (small_np > arr1[i]) ) {
                flag = 1;
                small_np = arr1[i];
            }
          }
       }
      if (flag) {
        printf("%d", small_np);
      } else {
        printf("NO");
     }
    return 0;
  }

Comments

Popular posts from this blog

Assignment 6 Question 3

Assignment 8 Question 1

Assignment 7 Question 2