0% found this document useful (0 votes)
21 views

Data sturcture and algorithm week 3

Uploaded by

hm896980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Data sturcture and algorithm week 3

Uploaded by

hm896980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ASSIGNMENT WEEK – 3

Name – HIMANSHU RAJ

Register No. - EA2331201010152

Program - B.C.A.-Data Science

Subject – Data Structures and Algorithms

Semester – II Semester

EA2331201010152
1. Write a C program that allocates sufficient memory for processing n
students data. The student details like Register number, name,
semester, 5 course codes along with their respective marks need to
be read as input. Generate a report with register number and grades
for all the courses, with appropriate headers. Note: Use meaningful
names for variables, Include proper documentation, Store the data
using linked representation of list.

Ans. C program that allocates memory for processing n students' data using a linked list
representation, reads their details, and generates a report:

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

// Structure to store student data


typedef struct Student {
int reg_number;
char name[50];
int semester;
char course_codes[5][10]; // Array to store 5 course codes (max 10 characters each)
int marks[5]; // Array to store marks for 5 courses
struct Student* next; // Pointer to the next student node in the list
} Student;

// Function prototypes
Student* create_student_node(int reg_number, char name[], int semester, char
course_codes[][10], int marks[]);
void add_student(Student** head_ptr, Student* new_student);
void print_report(Student* head);

int main() {
int n, i;
Student* head = NULL; // Head pointer of the student linked list

// Get the number of students


printf("Enter the number of students: ");
scanf("%d", &n);

// Read student data for each student


for (i = 0; i < n; i++) {
int reg_number, semester;
char name[50], course_codes[5][10];
int marks[5];

printf("\nEnter details for student %d:\n", i + 1);

// Get register number, name, and semester


printf("Register number: ");
scanf("%d", &reg_number);
printf("Name: ");
scanf(" %[^\n]", name); // Read name with spaces using scanf with space modifier
printf("Semester: ");
scanf("%d", &semester);

// Get course codes and marks


for (int j = 0; j < 5; j++) {
printf("Course code %d: ", j + 1);
scanf(" %s", course_codes[j]);
printf("Marks for course %d: ", j + 1);
scanf("%d", &marks[j]);

EA2331201010152
}

// Create a new student node and add it to the list


Student* new_student = create_student_node(reg_number, name, semester,
course_codes, marks);
add_student(&head, new_student);
}

// Print the report


printf("\n*** Student Report ***\n");
printf("Reg No.\t Name\t\t Semester\t");
for (i = 0; i < 5; i++) {
printf("Course %d Code\t Course %d Marks\t", i + 1, i + 1);
}
printf("\n---------------------------------------------------------------------------
----\n");
print_report(head);

// Free the allocated memory for the student list (implementation left as an
exercise)
// ... (code to free memory for each student node)

return 0;
}

// Function to create a new student node


Student* create_student_node(int reg_number, char name[], int semester, char
course_codes[][10], int marks[]) {
Student* new_student = (Student*)malloc(sizeof(Student));
if (new_student == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}

new_student->reg_number = reg_number;
strcpy(new_student->name, name);
new_student->semester = semester;
memcpy(new_student->course_codes, course_codes, sizeof(course_codes)); // Copy course
codes array
memcpy(new_student->marks, marks, sizeof(marks)); // Copy marks array
new_student->next = NULL;

return new_student;
}

// Function to add a student node to the linked list


void add_student(Student** head_ptr, Student* new_student) {
new_student->next = *head_ptr;
*head_ptr = new_student;
}

// Function to print the student report


void print_report(Student* head) {
Student* current = head;
int i;

while (current != NULL) {


printf("%d\t\t %s\t\t %d\t", current->reg_number, current->

EA2331201010152

You might also like