Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
CstructProduct
//gcc 7.4.0 //CstructProduct: get 3 product items from user, print them, sort them and print the sorted items //code is created by Rezaul Hoque on May 24,2021 //please contact at 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; //Instead of sorting algorithm, simple string comparison is used to sort the items; strcmp compares two strings and is 0 if they are equal or it is the difference between the first unmatched characters; if the difference is negative then the first string is above in order (more close to A)than the second string; #include <stdio.h> #include <string.h> struct store{ char name[20]; char number[20]; char price[20]; }; int main() { struct store product[3]; char temp[20]; int i=0, j=0; printf("Enter input:\n"); for (i=0;i<3;i++) scanf("%s %s %s",product[i].name, product[i].number, product[i].price); printf("\nInput:\n"); for (i=0;i<3;i++) { printf("%5s %5s %5s\n", product[i].name, product[i].number, product[i].price); } for(i=1;i<=3;i++) for(j=1;j<=3-i;j++) if(strcmp(product[j-1].name, product[j].name)>0)//as long as the first string has higher ASCII code (far away from A) , then first swap names, then numbers and finally prices {//swap names strcpy(temp,product[j-1].name); strcpy(product[j-1].name,product[j].name); strcpy(product[j].name, temp); //swap numbers strcpy(temp,product[j-1].number); strcpy(product[j-1].number,product[j].number); strcpy(product[j].number, temp); //swap prices strcpy(temp,product[j-1].price); strcpy(product[j-1].price,product[j].price); strcpy(product[j].price, temp); } printf("\nOutput:\n"); for (i=0;i<3;i++) { printf("%5s %5s %5s\n", product[i].name, product[i].number, product[i].price); } //to convert string numbers into integers use atoi(string number) int y[3]; int sum=0; for(i=0;i<3;i++){ y[i]= atoi(product[i].price);//converting string numbers into integers and storing them into y sum += y[i]; } printf("\n3 products cost Tk %d",sum); return 0; }
run
|
edit
|
history
|
help
0
sreesasu
upper case letter.c
C_141204_ConcatenarCadenas
detonadisimo
bitwise operations
T3.C
qsort
Project 3 Part 1 v1.5
poist
Star 1,3