0% found this document useful (0 votes)
15 views4 pages

Student and Cricketer Data Management

The document contains three C programs that demonstrate the use of structures to store and display information about students and cricketers. The first program stores a student's name, roll number, and fee paid, while the second stores a cricketer's name, runs scored, and wickets taken. The third program collects data for multiple students, calculates their total marks in three subjects, and sorts them by total marks, along with a brief explanation of the difference between structures and unions.

Uploaded by

pixelpirate008
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)
15 views4 pages

Student and Cricketer Data Management

The document contains three C programs that demonstrate the use of structures to store and display information about students and cricketers. The first program stores a student's name, roll number, and fee paid, while the second stores a cricketer's name, runs scored, and wickets taken. The third program collects data for multiple students, calculates their total marks in three subjects, and sorts them by total marks, along with a brief explanation of the difference between structures and unions.

Uploaded by

pixelpirate008
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

WAP to store and display the name roll no and fee paid by the student.

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

struct student {
char name[20];
int roll_no;
float fee;
};

int main() {
struct student s1;

printf("Enter the student's name: ");


fgets([Link], sizeof([Link]), stdin);
[Link][strcspn([Link], "\n")] = 0;

printf("Enter the roll number and fee paid: ");


scanf("%d %f", &s1.roll_no, &[Link]);

printf("The student's details are as follows:\n");


printf("Name: %s\n", [Link]);
printf("Roll Number: %d\n", s1.roll_no);
printf("Fee Paid: %.2f\n", [Link]);

return 0;
}

Output;

Enter the student's name: XYZ


Enter the roll number and fee paid: 110 1000
The student's details are as follows:
Name: XYZ
Roll Number: 110
Fee Paid: 1000.00

WAP to store and display the name of the cricketer, runs scored and wickets takenof a cricketer
player.

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct cricketer
{
char name[20];
int runs,wickets;
};
void main()
{
struct cricketer c1;
clrscr();
printf("Enter the name of the criketre,runs scored and wicketes taken");
gets([Link]);
scanf("%d%d",&[Link],&[Link]);
printf("The details are:\nName:%s\nRuns scored:%d\nWickets
taken:%d\n",[Link],[Link],[Link]);
getch();
}

WAP to store the name roll no and marks of physics , chem, and maths of n students and calculate
the total of three marks.

#include<stdio.h>

struct student {
char name[20];
int roll_no;
int physics, chem, maths, total;
};

int main() {
struct student s[100], temp;
int n, i, j;

printf("Enter the number of students: ");


scanf("%d", &n);

for (i = 0; i < n; i++) {


printf("Enter the student's name and marks of three subjects (physics, chemistry, maths): ");
scanf("%19s %d %d %d %d", s[i].name, &s[i].roll_no, &s[i].physics, &s[i].chem, &s[i].maths);
s[i].total = s[i].physics + s[i].chem + s[i].maths;
}

// Sorting students by total marks in descending order


for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (s[j].total < s[j + 1].total) {
temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}

// Displaying the sorted list


printf("\nName\tRoll No\tPhysics\tChem\tMaths\tTotal\n");
printf("-------------------------------------------------\n");
for (i = 0; i < n; i++) {
printf("%s\t%d\t%d\t%d\t%d\t%d\n", s[i].name, s[i].roll_no, s[i].physics, s[i].chem, s[i].maths,
s[i].total);
}

return 0;
}

Output
Enter the number of students: 4
Enter the student's name and marks of three subjects (physics, chemistry, maths): AA1
2
4
5

5
Enter the student's name and marks of three subjects (physics, chemistry, maths):
BB
34
5
6
8
Enter the student's name and marks of three subjects (physics, chemistry, maths): CC
&8
Enter the student's name and marks of three subjects (physics, chemistry, maths): 95
6
7
8

Name Roll No Physics Chem Maths Total


-------------------------------------------------
&8 95 6 7 8 21
BB 34 5 6 8 19
AA1 2 4 5 5 14
CC 0 0 0 0 0
Write the difference between structure and union

Common questions

Powered by AI

The source code uses the Bubble Sort algorithm to sort students based on their total marks in descending order. This method involves comparing adjacent elements and swapping them if they are in the wrong order, iterating through the list multiple times until it is sorted . Bubble sort is chosen here due to its simplicity and straightforward implementation, making it easy to understand and effectively demonstrating sorting principles in educational contexts, despite its O(n^2) time complexity which is inefficient for large datasets .

The C programming language uses structures to group different types of related data together under a single name, which facilitates management and display. For instance, in the source, a structure named 'student' is defined to hold a student's name (as a char array), roll number (as an integer), and fee paid (as a float). This allows related data to be managed easily without creating separate variables for each attribute . Furthermore, the program reads the student's data via the 'fscanf' function and dynamically displays the formatted result using 'printf', ensuring organized output .

The source code considers efficient data handling by using an array of structures for storing multiple students' records, each containing fields for the student's name, roll number, and scores in physics, chemistry, and maths . This design facilitates iterating over each student record to calculate totals and apply sorting algorithms . Additionally, the array's fixed size allows for easy indexing and passing of student data between functions, ensuring scalable operations when more students are added in the same format.

In C programming, 'scanf' is used for reading formatted input, ideal for directly reading primitive data types, such as integers and floats. For example, it reads roll number and fees in the student details program . However, 'scanf' stops reading input at whitespace, which makes it less suitable for strings with spaces like names. 'fgets', on the other hand, is used for reading a line of text including spaces, making it more appropriate for reading names as it reads until a newline is encountered . 'fgets' ensures that buffer overflow is avoided by limiting input to the size provided, while 'scanf' requires size management to prevent similar issues.

The student marks program may fail due to improper input formatting or buffer overflows in 'scanf' for reading names and numeric validations . For example, if special characters or extra whitespace are introduced, or if the input exceeds expected sizes, 'scanf' might skip entries or misread inputs. Effective strategies include validating user input through character checks, utilizing safer functions like 'fgets', providing buffer overflows prevention, implementing retry mechanisms for incorrect inputs, and setting default values for recovery from parsing errors . Furthermore, using error messages and exceptions to guide users toward proper input enhances resilience.

If a program to handle cricketer statistics was designed with functions for reading and displaying data using structures, outputs would mirror those for student data in format and structure. For instance, after input, the function calls would provide outputs such as: ``` The cricketer's details are as follows: Name: [Cricketer Name] Runs scored: [Runs] Wickets taken: [Wickets] ``` These outputs would organize data read and processed within function scopes, facilitating code reuse and modular design . Using functions would separate the data handling logic and improve the program structure, making it simpler to scale and maintain.

The source code demonstrates memory management through buffer handling and input size constraints to prevent overflow. For example, the 'fgets' function limits the input to the size of the buffer allocated for the name, avoiding overflow . Similarly, 'scanf' reads specific data types without exceeding buffer limits, and careful type-specific format specifiers ensure the correct handling of memory . Using a fixed array size for storing student records ensures that allocated memory is predictable and reduces the risk of runtime errors due to unmanaged allocations.

Structures and unions in C both allow grouping of different types of data, but their key difference lies in memory allocation and data handling. Structures allocate separate memory space for each member, allowing all to store values independently. Unions, however, allocate shared memory for all members, so any data written to one member will overwrite previous data . This difference impacts their usage: use structures when you need to maintain multiple pieces of data simultaneously, and unions when you need efficient memory usage but work with only one piece of data at a time .

The source programs use console I/O functions like 'clrscr', which may not work uniformly across different platforms, causing compatibility issues . Such functions are often specific to the development environment (like Turbo C) and aren't standard library features. Solutions include using conditional preprocessor directives to check for platform type and include compatible header files or use standard functions (such as 'system("clear")' for UNIX-like systems and 'system("cls")' for Windows) that perform similar tasks but are supported across environments . Avoiding outdated and non-standard functions by relying on C Standard Library features enhances portability.

User input in the sample code for storing cricketer details employs 'gets' for reading the name, which is not safe as it does not prevent buffer overflow. 'scanf' is used for reading integers directly, capturing runs and wickets . Improvements could include replacing 'gets' with 'fgets' to limit input size and avoid overflow, while validating numeric input using loops that ensure valid number entry before accepting it as data . Additionally, using additional libraries or functions to verify and sanitize string inputs would improve robustness.

You might also like