Menu


NPTEL - Introduction to Programming in C - Week 7:Assignment 7 Answers- 2025




Introduction to Programming in C
NPTEL- Week 7: Assignment 7 Answers- 2025

Week 7 : Assignment 7 - Introduction to Programming In C

Week 7 : Assignments - NPTEL >> Introduction to Programming In C - 2025


Week 7 : Assignment 7 - Question 1

Write a C program that creates a database of students using a struct.

Solution :


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

struct Student {
    char name[21];          
    int physics;           
    int chemistry;         
    int maths; 
};

// comparator for qsort
int compare(const void *a, const void *b) {
    struct Student *s1 = (struct Student *)a;
    struct Student *s2 = (struct Student *)b;
 if (s1->physics != s2->physics) {
        return s2->physics - s1->physics;  // Descending order
    }
  if (s1->chemistry != s2->chemistry) {
        return s2->chemistry - s1->chemistry;  // Descending order
    }
   return s2->maths - s1->maths; 
    // Complete the code here.
    // Return a negative value if s1 comes before s2
    // Return a positive value if s2 comes before s1
}

int main() {
    int n;
    scanf("%d", &n);

    struct Student arr[100];

    for (int i = 0; i < n; i++) {
        scanf("%s %d %d %d", arr[i].name, &arr[i].physics,
              &arr[i].chemistry, &arr[i].maths);
    }

    qsort(arr, n, sizeof(struct Student), compare);

    for (int i = 0; i < n; i++) {
        printf("%s %d %d %d\n", arr[i].name,
               arr[i].physics, arr[i].chemistry, arr[i].maths);
    }

    return 0;
}


Week 7 : Assignment 7 - Question 2

complete the C code for performing the following operations on the linked list:

Create and return a node e with given id and value val

Add a node e to the beginning of the list. Return the new list head.

Search for a node e with id inside the list. Return a pointer to e if found, else return NULL.

Change the priority (value) of an element with given id (if found) to the new value val.

Note: The code for reading input and printing output is provided; you only need to write these functions.

Solution :


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

struct node {
    int id;
    int value;
    struct node *next;
};

struct node *create_node(int id, int val) {
    struct node *new_node = (struct node *)malloc(sizeof(struct node));
    new_node->id = id;
    new_node->value = val;
    new_node->next = NULL;
    //Complete the code here
    return new_node;
}

struct node *append(struct node *list, struct node *e) {
    if (list == NULL)
      e->next = NULL;
        //Complete the code here
    else {
      e->next = list;
        //Complete the code here
    }
    return e;
}

struct node *search(struct node *list, int id) {
    while (list != NULL) {
        //Complete the code here
      if (list->id == id) {
            return list;
        }
        list = list->next;
    }
    return NULL;
}

void change_value(struct node *list, int id, int val) {
    struct node *e = search(list, id);
  if (e != NULL) {
        e->value = val;
    }
    //Complete the code here
}

int find_value(struct node *list, int id) {
    struct node *e = search(list, id);
    if (e != NULL)
        return e->value;
    return -1;
}

int main() {
    char op;
    int id, val;
    struct node *list = NULL;

    int flag = 1;
    do {
        scanf(" %c", &op);
        switch (op) {
            case 'A':
                scanf("%d %d", &id, &val);
                list = append(list, create_node(id, val));
                break;
            case 'S':
                scanf("%d", &id);
                printf("%d %d\n", id, find_value(list, id));
                break;
            case 'C':
                scanf("%d %d", &id, &val);
                change_value(list, id, val);
                break;
            case 'E':
                flag = 0;
                break;
        }
    } while (flag == 1);

    return 0;
}




Relevant blogs :

NPTEL - Introduction to Programming in C - Week 7:Assignment 7 Answers- 2025

NPTEL - Introduction to Programming in C - Week 6:Assignment 6 Answers- 2025

NPTEL - Introduction to Programming in C - Week 5:Assignment 5 Answers- 2025

NPTEL - Introduction to Programming in C - Week 4:Assignment 4 Answers- 2025

NPTEL - Introduction to Programming in C - Week 3:Assignment 3 Answers- 2025

NPTEL - Introduction to Programming in C - Week 2:Assignment 2 Answers- 2025

NPTEL - Introduction to Programming in C - Week 1:Assignment 1 Answers- 2025



Other blogs :

NPTEL - Programming in Java - QUIZ : Week 12:Assignment 12 Answers- 2025

NPTEL - Programming in Java - Week 12 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 11:Assignment 11 Answers- 2025

NPTEL - Programming in Java - Week 11 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 10:Assignment 10 Answers- 2025

NPTEL - Programming in Java - Week 10 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 9:Assignment 9 Answers- 2025

NPTEL - Programming in Java - Week 09 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 8:Assignment 8 Answers- 2025

NPTEL - Programming in Java - Week 08 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 7:Assignment 7 Answers- 2025

NPTEL - Programming in Java - Week 07 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 6:Assignment 6 Answers- 2025

NPTEL - Programming in Java - Week 06 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 5:Assignment 5 Answers- 2025

NPTEL - Programming in Java - Week 05 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 4:Assignment 4 Answers- 2025

NPTEL - Programming in Java - Week 04 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 3:Assignment 3 Answers- 2025

NPTEL - Programming in Java - Week 03 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 2:Assignment 2 Answers- 2025

NPTEL - Programming in Java - Week 02 : Programming Assignments Answers- 2025

NPTEL - Programming in Java - QUIZ : Week 1:Assignment 1 Answers- 2025

NPTEL - Programming in Java - Week 01 : Programming Assignments Answers- 2025