Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
cstructCricPoint
//gcc 7.4.0 //define a structure cricket with following members: name,team and batting average; now define an array of 5 cricketers and write a program to read information about 5 cricketers and print a team-wise list containing names of cricketers ,their batting average and modify the data(use pointer); //this code is created by Rezaul Hoque on August 24,2021;contact: jewelmrh@yahoo.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; #include <stdio.h> #include <stdlib.h> #include <string.h> const int n=5; struct cricket{ char name[30]; char team[30]; float avg; }; void disp(struct cricket *,int); void mod(struct cricket*); void teamwise(struct cricket *); void mod(struct cricket* ptr) { int l; printf("Enter position to modify value:\n"); scanf("%d",&l); printf("Enter new name value(skip for no change):\n"); scanf("%5s",(ptr+l-1)->name); printf("Enter new team value(skip for no change):\n"); scanf("%5s",(ptr+l-1)->team); printf("Enter new avg value(skip for no change):\n"); scanf("%f",&(ptr+l-1)->avg); } void disp(struct cricket*cric,int n) { struct cricket * ptr; printf("Name\tTeam\tBatting Average: \n"); for(ptr=cric; ptr<cric+n;ptr++) printf("%5s\t%5s\t%2.2f\n",ptr->name,ptr->team,ptr->avg); } void teamwise(struct cricket *cric){ struct cricket* ptr; char temp[30]; char temp2[30]; for(int l=1;l<n;l++) { for(int j=1;j<=n-l;j++) { if(strcmp((cric+j-1)->team,(cric+j)->team)>0) { strcpy(temp,(cric+j-1)->team); strcpy((cric+j-1)->team, (cric+j)->team); strcpy((cric+j)->team,temp); strcpy(temp2, (cric+j-1)->name); strcpy((cric+j-1)->name, (cric+j)->name); strcpy((cric+j)->name,temp2); float temp3 = (cric+j-1)->avg; (cric+j-1)->avg = (cric+j)->avg; (cric+j)->avg =temp3; } } } printf("Name\tTeam\tBatting Average: \n"); for(ptr=cric; ptr<cric+n;ptr++) printf("%5s\t%5s\t%2.2f\n",ptr->name,ptr->team,ptr->avg); } int main(){ struct cricket cric [5]; struct cricket * cptr; printf("Enter team,player name & batting average (5times):\n"); for(cptr=cric; cptr<cric+n;cptr++) scanf("%5s\t%5s\t%f\n",cptr->name,cptr->team,&cptr->avg); printf("Original list:\n"); disp(cric,n); printf("Teamwise list:\n"); teamwise(cric); printf("Modified list:\n"); mod(cric); disp(cric,n); return 0; }
run
|
edit
|
history
|
help
0
A_141124_arrayMaxMin01
Spring 2017 Lab 4 v1.1
CPTTRN_2
Lior Yehieli Lesson
2.1.3 Sum of squares of first hundred natural numbers with different cores
Project 3 Part 1 v1.2
Max3
19_1
more_than_one_break_in_loop
first C code