Home
  • C
  • C++
  • Java
  • Python
  • All Courses
  • About us
  • Home
  • C
  • C++
  • Java
  • Python
  • All Courses
  • About us
  • Contact
  • Privacy Policy

Swayam Solver

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

NPTEL Problem Solving Through Programming In C - Programming Assignment | July-2023 Week 9 to 12

NPTEL Problem Solving Through Programming In C - Programming Assignment | July-2023 Week 9 to 12

Posted on September 16, 2023

 

Problem Solving Through Programming In C


Subscribe to our YouTube Channel :  Swayam Solver

  Please scroll down for latest Programs. ðŸ‘‡ 


Week 9 : Programming Assignment 1

Due on 2023-09-28, 23:59 IST

Write a program to print all the locations at which a particular element (taken as input) is found in a list and also print the total number of times it occurs in the list. The location starts from 1.

For example if there are 4 elements in the array

5

6

5

7

If the element to search is 5 then the output will be

5 is present at location 1

5 is present at location 3

5 is present 2 times in the array.

Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
   int array[100], search, n, count = 0;
5
   //"search" is the key element to search and 'n' is the total number of element of the array
6
   // "count" is to store total number of elements
7
​
8
 scanf("%d", &n); //Number of elements is taken from test case
9
​
10
 int c;
11
   for (c = 0; c < n; c++)
12
      scanf("%d", &array[c]);
13
​
14
   scanf("%d", &search); // The element to search is taken from test case
15
​
16
/* Use the printf statements as below:
17
"%d is present at location %d.\n"  for each locations
18
"%d is not present in the array.\n" if the element is not found in the list
19
"%d is present %d times in the array.\n"
20
*/
21
for (c = 0; c < n; c++)
22
    {
23
      if (array[c] == search)
24
      {
25
         printf("%d is present at location %d.\n", search, c+1);
26
         count++;
27
      }
28
    }
29
   if (count == 0)
30
      printf("%d is not present in the array.", search);
31
   else
32
      printf("%d is present %d times in the array.", search, count);
33
​
34
   return 0;
35
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
4
5
6
5
7
5
5 is present at location 1.\n
5 is present at location 3.\n
5 is present 2 times in the array.
5 is present at location 1.\n
5 is present at location 3.\n
5 is present 2 times in the array.
Passed
Test Case 2
5
67
80
45
97
100
50
50 is not present in the array.
50 is not present in the array.
Passed


Week 9 : Programming Assignment 2

Due on 2023-09-28, 23:59 IST

Write a C program to search a given element from a 1D array and display the position at which it is found by using linear search function. The index location starts from 1.

Select the Language for this assignment. 
1
#include <stdio.h>
2
int linear_search(int[], int, int);
3
int main()
4
{
5
   int array[100], search, c, n, position;
6
   /* search - element to search, c - counter, n - number of elements in array,
7
   position - The position in which the element is first found in the list. */
8
​
9
    scanf("%d", &n); // Number of elements in the array is read from the test case data
10
​
11
    for (c = 0; c < n; c++)
12
    scanf("%d", &array[c]); //Elements of array is read from the test data
13
​
14
    scanf("%d", &search);  //Element to search is read from the test case data
15
​
16
   /* Use the following in the printf statement to print the output
17
   printf("%d is not present in the array.", search);
18
   printf("%d is present at location %d.", search, position+1); //As array[0] has the position 1
19
   */
20
position = linear_search(array, n, search);
21
​
22
   if (position == -1)
23
      printf("%d is not present in the array.", search);
24
   else
25
      printf("%d is present at location %d.", search, position+1);
26
   return 0;
27
}
28
​
29
int linear_search(int a[], int n, int find) {
30
   int c;
31
   for (c = 0 ;c < n ; c++ )
32
    {
33
      if (a[c] == find)
34
         return c;
35
    }
36
   return -1;
37
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
78
90
34
54
98
90
90 is present at location 2.
90 is present at location 2.
Passed
Test Case 2
6
30
40
50
20
90
60
90
90 is present at location 5.
90 is present at location 5.
Passed



Week 9 : Programming Assignment 3

Due on 2023-09-28, 23:59 IST
Write a C program to search a given number from a sorted 1D array and display the position at which it is found using binary search algorithm. The index location starts from 1.
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
 int c, n, search,
5
 array[100];
6
 scanf("%d",&n); //number of elements in the array
7
​
8
 for (c = 0; c < n; c++)
9
 scanf("%d",&array[c]);
10
​
11
 scanf("%d", &search); //The element to search is read from test case.
12
​
13
/* Use the printf statements as below:
14
 printf("%d found at location %d.", search, variable_name);
15
 printf("Not found! %d isn't present in the list.", search);
16
*/
17
int first, last, middle;
18
   first = 0;
19
   last = n - 1;
20
   middle = (first+last)/2;
21
​
22
   while (first <= last) {
23
      if (array[middle] < search)
24
         first = middle + 1;
25
      else if (array[middle] == search) {
26
         printf("%d found at location %d.", search, middle+1);
27
         break;
28
      }
29
      else
30
         last = middle - 1;
31
​
32
      middle = (first + last)/2;
33
      }
34
   if (first > last)
35
      printf("Not found! %d isn't present in the list.", search);
36
​
37
     return 0;
38
      }
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
10
20
30
40
50
40
40 found at location 4.
40 found at location 4.
Passed
Test Case 2
4
44
55
66
77
10
Not found! 10 isn't present in the list.
Not found! 10 isn't present in the list.
Passed


Week 9 : Programming Assignment 4

Due on 2023-09-28, 23:59 IST

Write a C program to reverse an array by swapping the elements and without using any new array.

Select the Language for this assignment. 
1
#include <stdio.h>
2
int main() {
3
  int array[100], n, c;
4
  scanf("%d", &n); // n is number of elements in the array.
5
  for (c = 0; c < n; c++) {
6
    scanf("%d", &array[c]);
7
  }
8
int temp, end;
9
   end = n - 1;
10
   for (c = 0; c < n/2; c++) {
11
    temp = array[c];
12
    array[c] = array[end];
13
    array[end] = temp;
14
    end--;
15
  }
0
printf("Reversed array elements are:\n");
1
​
2
  for (c = 0; c < n; c++) {
3
    printf("%d\n", array[c]);
4
  }
5
  return 0;
6
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
4
10
20
30
40
Reversed array elements are:\n
40\n
30\n
20\n
10
Reversed array elements are:\n
40\n
30\n
20\n
10\n
Passed after ignoring Presentation Error
Test Case 2
5
50
60
40
30
20
Reversed array elements are:\n
20\n
30\n
40\n
60\n
50
Reversed array elements are:\n
20\n
30\n
40\n
60\n
50\n
Passed after ignoring Presentation Error




Week 10 : Programming Assignment 01

Due on 2023-10-05, 23:59 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
float fun (float x); //Function fun returns the function value of f(x)
3
void bisection (float *x, float a, float b, int *itr); // This function computes the root of f(x) using bisection method
4
​
5
int main ()
6
{
7
    int itr = 0, maxmitr=10;
8
    float x, a=1.0, b=2.0, allerr, x1; // x is the value of root in each iteration, x1 is the final value of the root 
9
   // a and b are the initial range for calculating the root using bisection method
10
      
11
scanf("%f", &allerr);  // allerr is the allowable error taken from test case data 
12
    bisection (&x, a, b, &itr);
13
​
14
/* Use the printf statement as given below to print the root
15
printf("Root = %1.4f\n", x1); */
16
do
17
    {
18
        if (fun(a)*fun(x) < 0)
19
            b=x;
20
        else
21
            a=x;
22
        bisection (&x1, a, b, &itr);
23
        if (((x1-x)<0 && -(x1-x)< allerr) || ((x1-x)>0 && (x1-x)< allerr))
24
        {
25
            printf("Root = %1.4f", x1);
26
            return 0;
27
        }
28
        x=x1;
29
    }
30
    while (itr < maxmitr);
31
    return 1;
32
}
33
float fun (float x)
34
{
35
    return (2*x*x*x - 3*x - 5);
36
}
37
void bisection (float *x, float a, float b, int *itr)
38
/* this function performs and prints the result of one iteration */
39
{
40
    *x=(a+b)/2;
41
    ++(*itr);
42
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
0.001
Root = 1.7197
Root = 1.7197
Passed
Test Case 2
0.1
Root = 1.6875
Root = 1.6875
Passed


Week 10 : Programming Assignment 02

Due on 2023-10-05, 23:59 IST

Write a C code to check if a 3 x 3 matrix is invertible. A matrix is not invertible if its determinant is 0.

Select the Language for this assignment. 
1
#include<stdio.h>
2
 int main()
3
{
4
  int a[3][3], i, j;
5
  long determinant;
6
 //  9 elements of matrix is taken as input from test data
7
  for(i = 0 ; i < 3; i++)
8
      for(j = 0; j < 3; j++)
9
           scanf("%d", &a[i][j]);
10
​
11
/*Use the printf statements as:
12
printf("The given matrix is not invertible");
13
printf("The given matrix is invertible"); 
14
*/
15
determinant = a[0][0] * ((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1] * (a[1][0]
16
   * a[2][2] - a[2][0] * a[1][2]) + a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);
17
   if ( determinant == 0)
18
       printf("The given matrix is not invertible");
19
   else
20
       printf("The given matrix is invertible");
21
   return 0;
22
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
1
2
3
4
5
6
7
8
9
The given matrix is not invertible
The given matrix is not invertible
Passed
Test Case 2
1
0
5
2
1
6
3
4
0
The given matrix is invertible
The given matrix is invertible
Passed


Week 10 : Programming Assignment 03

Due on 2023-10-05, 23:59 IST

Write a C program to sort a given 1D array using pointer in ascending order.

Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
    int a[100],i, n;
5
    scanf("%d",&n);  //Number of elements of the array is taken from the test case data 
6
   
7
   for (i=0; i<n; i++)
8
    {
9
        scanf("%d",a+i); // Input the array elements
10
    }
11
int j,t;
12
for (i=0; i<(n-1); i++) 
13
    {
14
        for (j=i+1; j<n; j++)
15
        {
16
            if (*(a+i)>*(a+j))
17
            {
18
                t=*(a+i);
19
                *(a+i)=*(a+j);
20
                *(a+j)=t;
21
            }
22
        }
23
    }
0
//   Printing sorted array in ascending order 
1
    for (i=0; i<n; i++)
2
    {
3
        printf("%d\n",*(a+i));
4
    }
5
    return 0;
6
   }
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
20
40
50
30
10
10\n
20\n
30\n
40\n
50
10\n
20\n
30\n
40\n
50\n
Passed after ignoring Presentation Error



Week 10 : Programming Assignment 04

Due on 2023-10-05, 23:59 IST

Write a C program to sort a 1D array using pointer by applying Bubble sort technique.

Select the Language for this assignment. 
1
#include<stdio.h>
2
void sort(int *a, int n);
3
int main()
4
{
5
    int a[20];
6
    int n,i; 
7
    scanf("%d",&n); // Enter number of elements to sort is taken from test case data
8
   
9
    for(i=0;i<n;i++)
10
    {
11
        scanf("%d",&a[i]); // The elements of the array is taken from the test data
12
    }
13
​
14
sort(a, n); // Calling the sorting function
15
​
16
    //Printing the sorted array 
17
    for(i=0;i<n;i++)
18
    {
19
        printf("%d\n",a[i]);
20
    }
21
   return 0;
22
}
23
void sort(int *a, int n)
24
{
25
    int i,temp,j;
26
    for(i=1;i<n;i++)
27
    {
28
        for(j=0;j<n-i;j++)
29
        {
30
           if(*(a+j)>=*(a+j+1))
31
        {
32
            temp = *(a+j);
33
            *(a+j)= *(a+j+1);
34
            *(a+j+1)= temp;
35
        }
36
        }
37
    }
38
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
6
-10
90
30
20
-100
50
-100\n
-10\n
20\n
30\n
50\n
90
-100\n
-10\n
20\n
30\n
50\n
90\n
Passed after ignoring Presentation Error



Week 11 : Programming Assignment 1

Due on 2023-10-12, 23:59 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
int main()
3
{
4
  float t[100]={10,15,18,22,30}, v[100]={22,26,35,48,68};
5
  float a; //Value of the t to find the respective value of v(t)
6
  scanf("%f", &a);  // This will be taken from test cases
7
int i,j;
8
float b, c, k =0;
9
for(i=0; i<5; i++)
10
   {
11
      b=1;
12
      c=1;
13
      for(j=0; j<5; j++)
14
        {
15
           if(j!=i)
16
             {
17
               b=b*(a-t[j]);
18
               c=c*(t[i]-t[j]);
19
              }
20
            }
21
        k=k+((b/c)*v[i]);
22
        }
0
printf("The respective value of the variable v is: %.2f", k);
1
  return 0;
2
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
25
The respective value of the variable v is: 56.42
The respective value of the variable v is: 56.42
Passed
Test Case 2
16
The respective value of the variable v is: 28.74
The respective value of the variable v is: 28.74
Passed


Week 11 : Programming Assignment 2

Due on 2023-10-12, 23:59 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
float func(float x);
3
int main()
4
{
5
  int n=10; //Taking n=10 sub intervals
6
  float a,b,integral; //integral is the integration result
7
  scanf("%f",&a); // initial limit taken from test case 
8
  scanf("%f",&b); // Final limit taken from test case
9
​
10
//Use the printf statement as printf("The integral is: %0.6f\n",integral);
11
int i;
12
float h,x, sum=0;  
13
if(b>a)
14
  h=(b-a)/n;
15
  else
16
  h=-(b-a)/n;
17
  for(i=1;i<n;i++){
18
    x=a+i*h;
19
    sum=sum+func(x);
20
  }
21
  integral=(h/2)*(func(a)+func(b)+2*sum);
22
  printf("The integral is: %0.6f",integral);
23
  return 0;
24
}
25
float func(float x)
26
{
27
  return x*x;
28
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
0
1
The integral is: 0.335000
The integral is: 0.335000
Passed
Test Case 2
1
3
The integral is: 8.680000
The integral is: 8.680000
Passed


Week 11 : Programming Assignment 3

Due on 2023-10-12, 23:59 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
float func(float x,float y);
3
int main()
4
{
5
    float m1,m2,m3,m4,m,h=0.3;
6
    float x0 = 0.3, y0 = 5, xn;
7
    scanf("%f",&xn); //xn will be taken from test cases
8
​
9
​
10
//Use the printf statement as: printf("y=%f",y);
11
while(x0<xn)
12
    {
13
        m1=func(x0,y0);
14
        m2=func((x0+h/2.0),(y0+m1*h/2));
15
        m3=func((x0+h/2.0),(y0+m2*h/2));
16
        m4=func((x0+h),(y0+m3*h));
17
        m=((m1+2*m2+2*m3+m4)/6);
18
        y0=y0+m*h;
19
        x0=x0+h;
20
    }
21
    printf("y=%f",y0);  // Final output
22
    return 0;
23
}
24
float func(float x,float y)
25
{
26
    float m;
27
    m=(x*(x+1)-3*y*y*y)/10;
28
    return m;
29
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
0.9
y=1.777165
y=1.777165
Passed
Test Case 2
1.2
y=1.468128
y=1.468128
Passed


Week 11 : Programming Assignment 4

Due on 2023-10-12, 23:59 IST
Write a C program to check whether the given input number is Prime number or not using recursion. So, the input is an integer and output should print whether the integer is prime or not.

Note that you have to use recursion.
Select the Language for this assignment. 
1
#include <stdio.h>
2
int checkPrime(int, int); //Function to check prime or not 
3
​
4
int main()
5
{
6
    int num, check;
7
    scanf("%d", &num); //The number is taken from test case data
8
    check = checkPrime(num, num/2);
9
    if (check == 1)
10
    {
11
        printf("%d is a prime number\n", num);
12
    }
13
    else
14
    {
15
        printf("%d is not a prime number\n", num);
16
    }
17
    return 0;
18
}
19
int checkPrime(int num, int i)
20
{
21
    if (i == 1)
22
    {
23
        return 1;
24
    }
25
    else
26
    {
27
       if (num % i == 0)
28
       {
29
         return 0;
30
       }
31
       else
32
       {
33
         return checkPrime(num, i - 1);
34
       }
35
    }
36
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
13
13 is a prime number
13 is a prime number\n
Passed after ignoring Presentation Error
Test Case 2
40
40 is not a prime number
40 is not a prime number\n
Passed after ignoring Presentation Error





Week 12 : Programming Assignment 1

Due on 2023-10-19, 23:59 IST

Write a program in C to find the factorial of a given number using pointers.

Your last recorded submission was on 2023-10-09, 13:25 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
void findFact(int, long int*);
3
int main()
4
{
5
        long int fact; //factorial of the number
6
        int num1; 
7
        scanf("%d",&num1); //The number is taken from test data
8
​
9
         findFact(num1, &fact);
10
         printf("The Factorial of %d is : %ld\n",num1, fact);
11
         return 0;
12
        }
13
void findFact(int n, long int *f)
14
        {
15
        int i;
16
​
17
       *f =1;
18
       for(i=1;i<=n;i++)
19
       *f=*f*i;
20
       }
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   



Private Test cases used for EvaluationStatus
Test Case 1
Passed





Week 12 : Programming Assignment 2

Due on 2023-10-19, 23:59 IST
Write a C program to print the Record of the Student Merit wise. Here a structure variable is defined which contains student rollno, name and score.
Select the Language for this assignment. 
1
#include<stdio.h>
2
struct student
3
{
4
int rollno;
5
char name[20];
6
int score;
7
};
8
void main()
9
{
10
struct student s[20];
11
int i, n;
12
​
13
scanf("%d" ,&n); //No. of Students taken from test data
14
// Roll no., Name and Score of n students are taken from test data
15
for(i=0;i<n;i++)
16
{
17
scanf("%d", &s[i].rollno);
18
scanf("%s", s[i].name);
19
scanf("%d", &s[i].score);
20
}
21
//Complete the program so that merit list is printed in descending order
22
struct student temp;
23
int j;
24
for(i=0;i<n-1;i++)
25
{
26
for(j=0;j<n-1;j++)
27
{
28
if(s[j].score<s[j+1].score)
29
{
30
temp=s[j];
31
s[j]=s[j+1];
32
s[j+1]=temp;
33
}
34
}
35
}
0
printf("The Merit List is :\n");
1
for(i=0;i<n;i++)
2
{
3
printf("%d", s[i].rollno);
4
printf("  %s", s[i].name);
5
printf("  %d\n", s[i].score);
6
}
7
​
8
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
3
1
Santanu
700
2
Aparna
550
3
Vivek
900
The Merit List is :\n
3  Vivek  900\n
1  Santanu  700\n
2  Aparna  550
The Merit List is :\n
3  Vivek  900\n
1  Santanu  700\n
2  Aparna  550\n
Passed after ignoring Presentation Error




Week 12 : Programming Assignment 3

Due on 2023-10-19, 23:59 IST
Write a C program to store n elements using Dynamic Memory Allocation - calloc() and find the Largest element
Select the Language for this assignment. 
1
#include <stdio.h>
2
#include <stdlib.h>
3
​
4
int main()
5
{
6
    int n; 
7
    float *element;
8
​
9
    scanf("%d", &n); //Total number of elements
10
​
11
    // Allocate the memory for 'n' number of elements. 
12
    //Then take the elements as input from test data
13
element = (float*) calloc(n, sizeof(float));
14
​
15
    if(element == NULL)
16
    {
17
        printf("Error!!! memory not allocated.");
18
        exit(0);
19
    }
20
​
21
    // Stores the number entered by the user.
22
    int i;
23
    for(i = 0; i < n; ++i)
24
    {
25
        scanf("%f", element + i);
26
    }
27
​
28
 // find the largest
29
    for(i = 1; i < n; ++i)
30
    {
31
       if(*element < *(element + i))
32
         *element = *(element + i);
33
    }
34
​
35
​
36
    printf("Largest element = %.2f", *element);
37
​
38
    return 0;
39
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
200
300
500
100
400
Largest element = 500.00
Largest element = 500.00
Passed



Week 12 : Programming Assignment 4

Due on 2023-10-19, 23:59 IST

Write a C program to find the sum of two 1D integer arrays ‘A’  and ‘B’ of same size and store the result in another array ‘C’, where the size of the array and the elements of the array are taken as input.
In the Test case the input is given as follows

5

10

20

30

40

50

1

2

3

4

5

So the output will be displayed as

Result is

11

22

33

44

55

Write the program accordingly. Use dynamic memory allocation. 

Select the Language for this assignment. 
1
#include<stdio.h>
2
#include<stdlib.h>
3
​
4
void main()
5
{
6
 int i,n;
7
 
8
//The number of elements in each array is taken from test case data 
9
​
10
 scanf("%d", &n);
11
int *a,*b,*c;
12
 a = (int *) malloc(n*sizeof(int));
13
 b = (int *) malloc(n*sizeof(int));
14
 c = (int *) malloc(n*sizeof(int));
15
​
16
// Input Elements of First List;
17
 for(i=0;i<n;i++)
18
 {
19
  scanf("%d",a+i);
20
 }
21
​
22
 //Input Elements of Second List;
23
 for(i=0;i<n;i++)
24
 {
25
  scanf("%d",b+i);
26
 }
27
​
28
 for(i=0;i<n;i++)
29
 {
30
  *(c+i) = *(a+i) + *(b+i);
31
 }
0
printf("Result is\n");
1
​
2
 for(i=0; i<n; i++)
3
 {
4
  printf("%d\n",*(c+i));
5
 }
6
​
7
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
10
20
30
40
50
1
2
3
4
5
Result is\n
11\n
22\n
33\n
44\n
55
Result is\n
11\n
22\n
33\n
44\n
55\n
Passed after ignoring Presentation Error
Test Case 2
4
100
200
300
400
400
300
200
100
Result is\n
500\n
500\n
500\n
500
Result is\n
500\n
500\n
500\n
500\n
Passed after ignoring Presentation Error




















































































No comments:

Post a Comment

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

Contact Form

Name

Email *

Message *

Total Page views

Quick Links

  • Home
  • About
  • Contact
  • Privacy Policy

Follow us on

YouTube WhatsApp Telegram

Designed by Swayam Solver © 2024