Advertisement
Dido09

Exam Prep

Sep 22nd, 2022
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct  Members{
  6.     char name[56];
  7.     char id[7];
  8.     float price;
  9.     int lockerCode;
  10. }member;
  11.  
  12. member *array;
  13. int arrSize = 0;
  14.  
  15. void addMember(member *array, int arrSize, FILE *fp){
  16.     array = (member*)realloc(array, sizeof(member) * (arrSize + 1));
  17.     arrSize++;
  18.     if(array == NULL){
  19.         return 1;
  20.     }
  21.  
  22.     member *member1 = malloc(sizeof(member));
  23.  
  24.     array[arrSize - 1] = member1;
  25.  
  26.     printf("Enter the name: ");
  27.     fflush(stdin);
  28.     scanf("%[^\n]s", member1.name);
  29.  
  30.     printf("Enter the credentials of a new member: ");
  31.     fflush(stdin);
  32.     scanf("%s", member1.id);
  33.  
  34.     printf("Enter the credentials of a new member: ");
  35.     fflush(stdin);
  36.     scanf("%f", &member1.price);
  37.  
  38.     printf("Enter the credentials of a new member: ");
  39.     fflush(stdin);
  40.     scanf("%d", &member1.lockerCode);
  41.  
  42.     fprintf(fp, "%s;%s;%f;%d\n", member1.name, member1.id, member1.price, member1.lockerCode);
  43.  
  44.  
  45. }
  46.  
  47. void averageSubscribtion(member *array, int arrSize){
  48.     double averagePrice;
  49.     double price = 0;
  50.    
  51.     for(int i = 0; i < arrSize; i++){
  52.         price = price + array[i].price;
  53.     }
  54.    
  55.     averagePrice = price / arrSize;
  56.    
  57.     for(int n = 0; n < arrSize; n++){
  58.         if(array[n].price < averagePrice){
  59.         printf("%s - %s - f", array[n].name, array[n].id, array[n].price);
  60.         }
  61.     }
  62.    
  63.    
  64.    
  65. }
  66.  
  67.  
  68. int main(){
  69.  
  70. FILE *fp;
  71. fp = fopen("membersText.txt", "w");
  72. if(fp == NULL){
  73.     printf("Error creating file");
  74.     exit(1);
  75. }
  76.  
  77. addMember(array, arrSize, fp);
  78.  
  79.  
  80. return 0;
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement