Introduction to Programming in C Week 7

Session: JAN-APR 2024

Course Name: Introduction to Programming in C

Course Link: Click Here

For answers or latest updates join our telegram channel: Click here to join

These are Introduction to Programming in C Assignment 7 Answers


Question 1

You are the new tech administrator of a college. The college offers four courses listed below.
Course name Course code Credits
Science 1001 10
Maths 1002 5
Literature 1003 5
Philosophy 1004 1
Each student has to take exactly three courses and she gets a grade from (0 – 10)
on the course, depending on her performance.

Input
The first line contains the number of students n.
The next n lines contains the information on the students in the following order:
Name  CourseCode1 Marks1 CourseCode2 Marks2 CourseCode3 Marks3
Output
The names and CGPAs (to a single decimal point) of students, line by line in the following format:
Name CGPA

Solution:

#include<stdio.h>

#include<stdlib.h>

struct course{
    int code;
    int credits;
};

typedef struct course course;

course science = {1001, 10};
course maths = {1002, 5};
course literature = {1003, 5};
course philosophy = {1004, 1};
  
struct student{
  char name[20];
  course courses[3];
  int grades[3];
};

typedef struct student student;

float calculate_gpa(student s){
  float total_credits = 0.0;
    float total_weighted_grades = 0.0;

    for (int i = 0; i < 3; i++) {
        total_credits += s.courses[i].credits;
        total_weighted_grades += s.courses[i].credits * s.grades[i];
    }

    return total_weighted_grades / total_credits;
}

void print(student * s, int n) {
  int i;
  for (i = 0; i < n; i++) {
    printf("%s %.1f\n", s[i].name, calculate_gpa(s[i]));
  }
}


int main() {
  int i,j, n;
  scanf("%d", & n);
  student *student_info = (student * ) malloc(sizeof(student) * n);
  for (i = 0; i < n; i++) {
    scanf("%s", student_info[i].name);
    for(j = 0 ; j < 3; j ++){
        int coursecode;
        scanf("%d", &coursecode);
        switch(coursecode){
            case 1001:
            student_info[i].courses[j] = science;
            break;
            case 1002:
            student_info[i].courses[j] = maths;
            break;
            case 1003:
            student_info[i].courses[j] = literature;
            break;
            case 1004:
            student_info[i].courses[j] = philosophy;
            break;
        }
        scanf("%d", &student_info[i].grades[j]);
    }
  }
  print(student_info, n);
  return 0;
}

For answers or latest updates join our telegram channel: Click here to join

These are Introduction to Programming in C Assignment 7 Answers


Question 2

Linked List Operations
In this question, a linked list is partially implemented where each element in the linked list
is a structure of the following format:
struct node{
int id;
int priority;
struct node *next;
};

Solution:

#include <stdio.h>

#include <stdlib.h>

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

/*
   create_node : Create a new struct node e, with given id and priority as val.
   Return a pointer to e.
*/
struct node * create_node(int id, int val) {
    struct node *new_node = (struct node *)malloc(sizeof(struct node));
    new_node->id = id;
    new_node->priority = val;
    new_node->next = NULL;
    return new_node;
}

/*
   append : Append the given node e, to the start of the list list.
   Return the head of the list, which is now e.
*/

struct node * append(struct node * list, struct node * e) {
    if (e == NULL)
        return list;
    e->next = list;
    return e;
}

/*
   search : Search for a node e in list, with given id.
   Return Return a pointer to e, if found. Return NULL otherwise.
*/
struct node * search(struct node * list, int id) {
    struct node *current = list;
    while (current != NULL) {
        if (current->id == id)
            return current;
        current = current->next;
    }
    return NULL;
}

/*
   change_priority : Change priority of element with given id (if found) to val.
*/

void change_priority(struct node * list, int id, int val) {
    struct node *e = search(list, id);
    if (e != NULL)
        e->priority = val;
}

/*
   find_priority : Searches for an element with given id.
   Returns its priority if found, -1 otherwise.
*/
int find_priority(struct node * list, int id) {
  struct node * e = search(list, id);
  if (e != NULL)
    return e -> priority;
  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_priority(list, id));
      break;
    case 'C':
      scanf("%d %d", & id, & val);
      change_priority(list, id, val);
      break;
    case 'E':
      flag = 0;
    }
  } while (flag == 1);
  return 0;
}

For answers or latest updates join our telegram channel: Click here to join

See also  Introduction to Programming in C Week 2

These are Introduction to Programming in C Assignment 7 Answers

More Solutions of Introduction to Programming in C: Click Here

More NPTEL Solutions: https://progiez.com/nptel-assignment-answers/


Course Name: Introduction to programming in C

Course Link: Click Here

These are Introduction to Programming in C Assignment 7 Answers


Question 1

Create a database of students using structures, where in each entry of the database will have the following fields:

  1. A name, which is a string with at most 128 characters
  2. Marks in physics which is an int between 0 and 100
  3. Marks in chemistry which is an int number between 0 and 100
  4. Marks in mathematics which is an int number between 0 and 100

You may assume that each student has a different mark in mathematics.

These are Introduction to Programming in C Assignment 7 Answers

Input Format :
First line contains the number of students n, where 1<=n<=100.
In following n lines each line contains(space separated) a name and their respective marks in physics, chemistry, maths, where 0<=marks<=100.
Output Format :
Sorted database of n lines.
Hint: You can use qsort function in C to sort the elements in the structure array.

These are Introduction to Programming in C Assignment 7 Answers

Solution

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
  char name[128];
  int phy;
  int chem;
  int maths;
};
struct student data[100];
int n;
int cmp(const void* a, const void* b)
{
  struct student *ia = (struct student *)a;
  struct student *ib = (struct student *)b;
  if (ia->phy == ib->phy)
  {
    if (ia->chem == ib->chem)
    {
      return (ib->maths - ia->maths);
    }
    else
    {
      return (ib->chem - ia->chem);
    }
  }
  else
  {
    return (ib->phy - ia->phy);
  }
}
int main()
{
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
  {
    scanf("%s %d %d %d", data[i].name, &data[i].phy, &data[i].chem, &data[i].maths);
  }
  qsort(data, n, sizeof(struct student), cmp);
  for (int i = 0; i < n; i++)
  {
    printf("%s %d %d %d\n", data[i].name, data[i].phy, data[i].chem, data[i].maths);
  }
  return 0;
}

These are Introduction to Programming in C Assignment 7 Answers

See also  Introduction to Programming in C Week 3

Question 2

You have to complete the code for performing the following operations in a priority queue linked list:

  1. Create and return a node e with given id and value val
    struct node * create_node(int id, int val)
  2. Add an node e to the beginning of the list. Return the new list.
    struct node * append(struct node * list, struct node * e);
  3. Search for a node e with id inside the list. Return a pointer to e  if found, else return NULL
    struct node * search(struct node * list, int id); 
  4. Change the value of an element e, (if found) in the list to the new value val.
    void change_priority((struct node *list, struct node* e, int val);

These are Introduction to Programming in C Assignment 7 Answers

Each element in the linked list is a structure of the following format:
struct node{
int id;
int priority;
struct node *next;}; 
The field priority denotes the priority of an element inside the list, the higher the value of the field, the more the priority.
Element with the highest priority is returned by extract_max.
Note: The code for manipulating the input as well as output is given to you. You only have to write code for the incomplete functions.
Input
——-
A set of lines, each lines containing a character representing the operation and its inputs.
The operation can be one of the following:

These are Introduction to Programming in C Assignment 7 Answers

  • A <id> <val>
    Add an node with id and val to the list, at the start of the list.
  • C <id> <val>
    Change the value of the element with id to val.
    If an element with this id is not found, do nothing.
  • S <id>
    If an element with the id is in the list print the id and the value and a newline. 
    Else, print the id and -1 and a newline.
  • E
    End of input, exit from the program
See also  Introduction to Programming in C Week 6

These are Introduction to Programming in C Assignment 7 Answers

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 *e = (struct node *) malloc(sizeof(struct node));
  e->id = id;
  e->value = val;
  e->next = NULL;
  return e;
}
struct node *append(struct node *list, struct node *e)
{
  if (list == NULL)
  {
    return e;
  }
  else if (e->value > list->value)
  {
    e->next = list;
    return e;
  }
  else
  {
    list->next = append(list->next, e);
    return list;
  }
}
struct node *search(struct node *list, int id)
{
  struct node *current = list;
  while (current != NULL)
  {
    if (current->id == id)
    {
      return current;
    }
    current = current->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;
  }
}
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;
    }
  }
  while (flag == 1);
  return 0;
}

These are Introduction to Programming in C Assignment 7 Answers

More Weeks of Introduction to Programming in C: Click Here

More Nptel courses: https://progiez.com/nptel


These are Introduction to Programming in C Assignment 7 Answers


These are Introduction to programming in C Assignment 7 Answers