0% found this document useful (0 votes)
8 views8 pages

Student All Funcions

The document contains a C program that manages student and player details using structures. It includes functions for reading, displaying, searching, sorting, and finding maximum marks for students, as well as selecting players based on specific criteria for a cricket team. Additionally, it outlines a problem-solving approach for managing transaction details in various applications, including money transactions and blockchain technology.

Uploaded by

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

Student All Funcions

The document contains a C program that manages student and player details using structures. It includes functions for reading, displaying, searching, sorting, and finding maximum marks for students, as well as selecting players based on specific criteria for a cricket team. Additionally, it outlines a problem-solving approach for managing transaction details in various applications, including money transactions and blockchain technology.

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>
#include <string.h> // Added to use strcmp

typedef struct student {


char name[30];
int rollno;
float marks;
} st;

void read(st *s, int n) {


printf("Enter student details\n");
printf("Name Roll No Marks\n");
for (int i = 0; i < n; i++) {
scanf("%29s%d%f", (s + i)->name, &(s + i)->rollno, &(s + i)->marks); // Pointer arithmetic
}
}

void display(st *s, int n) {


printf("*********Student details are ************\n");
printf("Name Roll No Marks\n");
for (int i = 0; i < n; i++) {
printf("%s %d %f\n", (s + i)->name, (s + i)->rollno, (s + i)->marks); // Pointer arithmetic
}
}

void search(st *s, int n, int rn) {


int flag = 0;
printf("Student details of roll number %d is: \n", rn);
for (int i = 0; i < n; i++) {
if ((s + i)->rollno == rn) { // Pointer arithmetic
flag = 1;
printf("%s %d %f\n", (s + i)->name, (s + i)->rollno, (s + i)->marks);
}
}
if (flag == 0) {
printf("Student not found\n");
}
}

void search_by_name(st *s, int n, char name[30]) {


int flag = 0;
printf("Student details of %s is: \n", name);
for (int i = 0; i < n; i++) {
if (strcmp((s + i)->name, name) == 0) { // Pointer arithmetic
flag = 1;
printf("%s %d %f\n", (s + i)->name, (s + i)->rollno, (s + i)->marks);
}
}
if (flag == 0) {
printf("Student not found\n");
}
}

void sort(st *s, int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if ((s + j)->marks > (s + j + 1)->marks) { // Pointer arithmetic
st temp = *(s + j); // Pointer arithmetic
*(s + j) = *(s + j + 1); // Pointer arithmetic
*(s + j + 1) = temp; // Pointer arithmetic
}
}
}
}

void sort_by_name(st *s, int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (strcmp((s + j)->name, (s + j + 1)->name) > 0) { // Pointer arithmetic
st temp = *(s + j); // Pointer arithmetic
*(s + j) = *(s + j + 1); // Pointer arithmetic
*(s + j + 1) = temp; // Pointer arithmetic
}
}
}
}

void max_marks(st *s, int n) {


float max = 0;
int index = 0;
for (int i = 0; i < n; i++) {
if ((s + i)->marks > max) { // Pointer arithmetic
max = (s + i)->marks; // Pointer arithmetic
index = i;
}
}
printf("Maximum marks obtained is %f\n", max);
printf("Student details:\n");
printf("%s %d %f\n", (s + index)->name, (s + index)->rollno, (s + index)->marks); // Pointer
arithmetic
}

int main() {
int n, rn;
char name[30];
printf("Enter the number of students\n");
scanf("%d", &n);

// Dynamically allocate memory


st *s = (st *)malloc(n * sizeof(st));
if (s == NULL) {
printf("Memory allocation failed!\n");
return 1; // Exit if memory allocation fails
}

read(s, n);
display(s, n);

printf("Enter the roll no to be searched\n");


scanf("%d", &rn);
search(s, n, rn);

printf("Enter student name to be searched\n");


scanf("%29s", name); // Added size limit to avoid overflow
search_by_name(s, n, name);

printf("Student details after sorting based on marks:\n");


sort(s, n);
display(s, n);

printf("Student details after sorting based on name:\n");


sort_by_name(s, n);
display(s, n);

printf("Student who has got maximum marks:\n");


max_marks(s, n);

free(s); // Free the allocated memory


return 0;
}

VarunFinchi is a manger of BCR franchise team, where he has to select 5 players to make
squad full for playing in PPL League. He contacts Lord Hassan is an analyst of BCR team,
where he has all the player details. Varun Finchi selects 3 players who have highest runs in
their career, one who has least runs in his career and one more player who has Name
“Rajat”. Please help Varun Finchi to select players for BCR team.

I. Understanding the Problem:


1. Known’s: Players details.
2. Unknowns: Select total 5 Players, first 3 Players with highest runs, One Player with
least runs and one more Player with name “Rajat”.
3. Extra Information: BCR team, PPL league, Manager Varun Finchi.
4. Assumptions: NIL
5. Constraints: Consider number of Player details to store is more than 5.

II. Devise a Plan:


1. Problem Representation: Numerical
2. Problem Solving Strategy: Mathematical Reasoning
3. Identification of Structure name and its members:
a. Structure Name: players
b. Structure Members:player_name, team_name, odi_rank, runs, centuries.
4. Identification of Operations:read_player_details(), display_palyer_details(),
sort_player_details()

III. Carry Out a Plan:


1. Write Structure as per syntax:
typedef struct players
{
char player_name[20],team_name[10];
int oid_rank, runs, centuries;
}TEAM;

2. Algorithm:
Algorithm: To read Player Details
Step 1: Start
Step 2: Repeat for n number of players’ details
Read player_name, team_name, odi_rank, runs and centuries.
Step 3: Stop

Algorithm: To Display Players Details


Step 1: Start
Step 2: Repeat for n number of players’ details
Display player_name, team_name, odi_rank, runs and centuries.
Step 3: Stop

Algorithm: To Sort and display the selected players


Step 1: Start
Step 2: Sort the Players details in descending order
Repeat for i=0 to n-1
Repeat for j=0 to n-i-1
Is players[j].runs less than players[j+1].runs?
[then, swap entire players details]
temp = players[j]
players[j] = players[j+1]
players[j+1] = temp
Step 3: Display 3 players with highest runs scorers. [Print index 0, 1 and 2 players
details after descending order sort]
Repeat for i=0 to less than 3
Display player_name, team_name, odi_rank, runs and centuries.
Step 4: Display 1 player with least runs scorer [Print last index (i.e. n-1 index)
player details after descending order sort]
Display player_name, team_name, odi_rank, runs and centuries.
Step 5: Search for Player name “Rajat” and display.
Repeat for i=0 to less than n
Is strcmp(player_name , “Rajat”) is ture?
Display player_name, team_name, odi_rank, runs and
centuries.
Step 6: Stop

IV. Assess the Result:


Enter number of player details to read
7
Enter Player name, team name, odi rank, centuries, runs scored
viratindia 1 40 25000
dhoniindia 2 50 32000
rohitindia 3 30 29000
ashwinindia 12 10 12000
ishantindia 34 2 2554
rainaindia 20 34 12455
Rajatindia 10 12 4832

BCR Team Details


Name Team ODi Rank Centuries Runs
virat india 1 40 25000
dhoni india 2 50 32000
rohit india 3 30 29000
ashwin india 12 10 12000
ishant india 34 2 2554
raina india 20 34 12455
Rajat india 10 12 4832

Selected Team Players are


Name Team ODi Rank Centuries Runs
dhoni india 2 50 32000
rohit india 3 30 29000
virat india 1 40 25000
ishant india 34 2 2554
Rajat india 10 12 4832
V. Summary:
The BCR team manager’s problem is solved using the descending order sort. Through
descending order sort, we can obtain the players with highest runs scorers to lowest runs
scorers. Therefore we select top 3 players that is indices 0, 1 and 2. Then we select last
index play with least runs scorer that is n-1, and we perform linear search for player name
with Rajat. Therefore we select and display the 5 players as per the manager’s needs.

Program:
*/

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

typedef struct {
char p_name[20], t_name[10];
int runs;
} TEAM;

void read(TEAM *bcr, int n) {


printf("Enter Player name, team name, runs scored:\n");
for (int i = 0; i < n; i++) {
scanf("%s %s %d", (bcr + i)->p_name, (bcr + i)->t_name, &(bcr + i)->runs);
}
}

void display(TEAM *bcr, int n) {


printf("\nTeam Details\n");
printf("Name\tTeam\tRuns\n");
for (int i = 0; i < n; i++) {
printf("%s\t%s\t%d\n", (bcr + i)->p_name, (bcr + i)->t_name, (bcr + i)->runs);
}
}

void sort_desc(TEAM *bcr, int n) {


TEAM temp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if ((bcr + j)->runs < (bcr + j + 1)->runs) {
temp = *(bcr + j);
*(bcr + j) = *(bcr + j + 1);
*(bcr + j + 1) = temp;
}
}
}
printf("\nSelected Team Players:\n");
printf("Name\tTeam\tRuns\n");

// Top 3 players with highest runs


for (int i = 0; i < 3; i++) {
printf("%s\t%s\t%d\n", (bcr + i)->p_name, (bcr + i)->t_name, (bcr + i)->runs);
}

// Player with the least runs


printf("%s\t%s\t%d\n", (bcr + n - 1)->p_name, (bcr + n - 1)->t_name, (bcr + n - 1)->runs);

// Player with name "Rajat"


for (int i = 0; i < n; i++) {
if (strcmp((bcr + i)->p_name, "Rajat") == 0) {
printf("%s\t%s\t%d\n", (bcr + i)->p_name, (bcr + i)->t_name, (bcr + i)->runs);
break;
}
}
}

int main() {
int n;
printf("Enter number of players: ");
scanf("%d", &n);

TEAM *bcr = (TEAM *)malloc(n * sizeof(TEAM)); // Allocate memory dynamically


if (bcr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}

read(bcr, n);
display(bcr, n);
sort_desc(bcr, n);

free(bcr); // Free the dynamically allocated memory


return 0;
}

Home task
1 PhonePay is an application for money transition online. The application stores ‘n’
transaction details of users. As every user is very much interested to know his transaction
details, write a program to display transactions performed by a user and transaction details
of a particular transaction. Given: Transaction_ID, User_ID, Amount, Beneficiary_ID
2 Blockchain is the new technology in computer science, where it is used to store the ‘n’
transaction details of users. Shivam is very much interested in implementing blockchain
kind of technology using C programming. Transaction details contains: Transaction_ID,
sender name, receiver name, amount, time. Please help Shivam to implement the above
problem and perform the below operations. i) Display the transaction details of particular
Transaction_ID. ii) Display the transaction details of particular user.
3 Vishal owns a cyber café, where he has ‘N’ number of computers. Since Vishal is lazy to
write the details of ‘N’ computers in paper, he stores the details of computers in a file.
Please help Vishal to store the details of computers and perform the below operations. i)
Display the details of all computers ii) Display only “HP” computers.

You might also like