NPTEL Introduction to programming in c Assignment 4 solutions

Question 1:

Programming Assignment_4 Question 1

Due on 2018-09-27, 23:59 IST
Given two arrays of integers output the smallest number in the
first array not present in the second one.

Input Specification: 
The first line contains the size N1 of the first array.
Next line give the contents of the first array.
Next line contains the size N2 of the second array.
Next line give the contents of the second array.

Output Format:
Output must be a single number which is the smallest number occurring
in the first array that does not occur in the second. In case there is
no such number, output NO.

Variable Constraints:
The sizes of the arrays are smaller than 20.
Each array entry is an integer which fits an int data type.

Example:
Input:
3
2 3 4
4
1 3 5 7

Output: 2

Input
1
1
2
1 2
Output: NO
Select the Language for this assignment. 
1
#include <stdio.h>
2
3
int main(){
4
  
5
  int n1,n2;
6
  int arr1[21],arr2[21];
7
  int uniq[21],uc = 0;
8
  
9
  scanf("%d",&n1);
10
  for(int i=0; i<n1 ; i++){
11
    scanf("%d",&arr1[i]);
12
  }
13
  
14
  scanf("%d",&n2);
15
  for(int i=0; i<n2 ; i++){
16
    scanf("%d",&arr2[i]);
17
  }
18
  
19
  for(int i =0 ; i <n1; i++){
20
    int unique = 1;
21
    for(int j = 0 ; j < n2 ; j++){
22
        if(arr2[j] == arr1[i])
23
            unique = 0;
24
    }
25
    if(unique){
26
        uniq[uc] = arr1[i];
27
        uc++;
28
    }
29
  }
30

  if(uc == 0){
31



    printf("NO");
32
  }else{

33

    
int min = uniq[0];  
for(int i = 0; i < uc ; i++){
34
     if(uniq[i] < min)
          min = uniq[i];
    }
    printf("%d",min);
  }
}

Programming Assignment 4 Question 2

Due on 2018-09-27, 11:59 IST
Find the number of distinct numbers in a given sequence. The sequence need not be sorted.
Input The input consists of two lines. The first line consists of a positive number N. N is at most 1000. The second line consists of N numbers separated by spaces. OutputThe output should be the number of distinct elements in the sequence. Sample Input4  1 2 3 1 
Sample Output 3

InputOutput
Test Case 1
4
1 2 3 1
3
Test Case 2
5
-1 -2 -3 -1 -2
3
Test Case 3
1
1
1
Test Case 4
100
1 2 3 4 5 6 7 8 9 10 19 18 17 16 15 14 13 12 11 1 2 3 4 5 6 7 8 9 10 19 18 17 16 15 14 13 12 11 1 2 3 4 5 6 7 8 9 10 19 18 17 16 15 14 13 12 11 1 2 3 4 5 6 7 8 9 10 19 18 17 16 15 14 13 12 11 1 2 3 4 5 6 7 8 9 10 19 18 17 16 15 14 13 12 11 1 2 3 4 5  
19
Due Date Exceeded.
As per our records you have not submitted this assignment.
Sample solutions (Provided by instructor)  Select the Language . 
1
#include <stdio.h>
2
3
#define SIZE 1000
4
5
int main()
6
{
7
    int arr[SIZE];
8
    int i;
9
    int j;
10
    int count=0;
11
    int n;
12
13
14
15
    scanf("%d",&n);
16
17
    for(i=0;i<n;i++){
18
        scanf("%d",&arr[i]);
19
    }
20
21
    /* O(n^2) solution */
22
23
    for(i=0;i<n;i++){
24
        int found=0;
25
        
26
        /* see if arr[i] occurs previously in the array */
27
        for(j=i-1;j>=0;j--){
28
            if(arr[j] == arr[i]){
29
                found=1;
30
                break;
31
            }
32
        }
33
        if ( found == 0 ){ /* arr[i] is a new element */
34
            count = count + 1;
35
        }
36
    }
37
38
    printf("%d\n",count);
39
    return 0;
40
}
41



Programming Assignment_4 Question 3

Due on 2018-09-27, 23:59 IST
Write a program that replaces the occurence of a given character (say c)
in a primary string (say PS) with another string (say s).

Input:
The first line contains the primary string (PS)
The next line contains a character (c)
The next line contains a string (s)

Output:
Print the string PS with every occurence of c replaced by s.

NOTE:
- There are no whitespaces in PS or s. 
- Maximum length of PS is 100.
- Maximum length of s is 10.
Select the Language for this assignment. 
1
#include<string.h>
2
#include<stdio.h>
3
void clean_stdin(void)
4
{
5
        char c = getchar();
6
}
7
8
int main(){
9
    
10
    char ps[101];
11
    char c;
12
    char ns[11],news[120];
13
    
14
    
15
    fgets(ps,101,stdin);
16
    
17
    scanf("%c",&c);
18
19
    clean_stdin();
20
    fgets(ns,11,stdin);
21
  
22
    
23
     ns[strlen(ps) - 1] ='\0';
24
     ns[strlen(ps) - 1] ='\0';
25
   
26
    //printf("ps = %s ns =%s",ps,ns);
27
    for(int i = 0 ; i < strlen(ps) ; i++){
28
      if(ps[i] == c){
29
        strcat(news,ns);
30
      }else{
31
        news[strlen(news)] = ps[i];
32
      }
33
   }
34
  for(int i = 0 ; i < strlen(news) ; i++){
35
    printf("%c",news[i]);
36
  }
37
}   

Comments

  1. Looking for a quick and reliable way to handle your academic workload? Our Do My Assignment service is here to help! With expert assistance across various subjects, we provide high-quality, personalized solutions to ensure you meet deadlines and achieve top grades. Let us simplify your assignments and make academic success more attainable today!

    ReplyDelete

Post a Comment

Popular posts from this blog

Code to Implement Dynamic Queue in C++ using arrays

C PROGRAM TO ECHO "*" - C PROGRAM TO GET PASSWORD FROM USER