Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "StructAndSignatures.h"
- parents_t* createParentsArray(int *size) // size is a pointer that will count the size of the array
- {
- // define array to input name
- char lastName[MAX_NAME];
- // define choice
- char choice;
- int physSize=1, numKids;
- // define empty parents array
- parents_t *arr=NULL;
- *size=0;
- do
- {
- // input choice
- printf("Do you wish to enter parents? (y/n) : ");
- scanf("%c",&choice);
- printf("\n");
- flushall();
- if(choice=='n')
- {
- break;
- }
- else if(choice=='y')
- {
- // input last name
- printf("Enter parent's last name: ");
- gets(lastName);
- // input number of kids
- printf("Enter number of kids: ");
- scanf("%d",&numKids);
- // increase parents array by 1
- arr=(parents_t*)realloc(arr,physSize*sizeof(parents_t));
- // check if allocation succeeded
- if(!arr)
- {
- printf("Memory allocation didn't succeed! \n");
- exit(1);
- }
- // allocate memory and put last name in array
- arr[physSize-1].lastName = strdup(lastName);
- // check if allocation succeeded
- if(!arr[physSize-1].lastName)
- {
- printf("Memory allocation didn't succeed! \n");
- exit(1);
- }
- // put number of kids in the parents array
- arr[physSize-1].numberOfKids=numKids;
- // increase physical size by 1
- physSize++;
- // increase size by 1
- (*size)++;
- printf("\n");
- flushall();
- }
- else
- {
- printf("Not an option, please choose a different option \n\n");
- }
- }while(1);
- return arr;
- }
- // print the parents devided by catgorys
- void printParents(parents_t*** arr, int size)
- {
- int i, j;
- for(i=0;i<size;i++)
- {
- if(i==GROUP5)
- {
- printf("Parents with 5 kids or more: \n");
- }
- else if(i==GROUP4)
- {
- printf("Parents with 2 - 4 kids: \n");
- }
- else
- {
- printf("Parents with 2 kids or less: \n");
- }
- for(j=0;arr[i][j]!=NULL;j++)
- {
- printf("Last name: %s | Number Of kids: %d \n",arr[i][j]->lastName, arr[i][j]->numberOfKids);
- }
- printf("\n");
- }
- }
- parents_t*** sort(parents_t *arr, int size)
- {
- // define sizes to increase sizes of internal arrays
- int i, size5=1, size4=1, size2=1;
- // define constants
- const int KIDS5=5, KIDS4=4, KIDS2=2;
- // aloocate memeory that conatins pointer to parents
- parents_t ***groups=(parents_t***)calloc(PARENTS_GROUP,sizeof(parents_t**));
- // check if the allcoation suecceeded
- if(!groups)
- {
- printf("Memory allocation didn't succeed! \n");
- exit(1);
- }
- for(i=0;i<size;i++)
- {
- if(arr[i].numberOfKids>=KIDS5)
- {
- // increase parents array in group 5 by 1
- groups[GROUP5]=(parents_t**)realloc(groups[GROUP5],sizeof(parents_t*)*size5);
- // check if allocation succeeded
- if(!groups[GROUP5])
- {
- printf("Memory allocation didn't succeed! \n");
- exit(1);
- }
- // put the parrent pointer in group 5
- groups[GROUP5][size5-1]=&arr[i];
- size5++;
- }
- else if(arr[i].numberOfKids>KIDS2 && arr[i].numberOfKids<=KIDS4)
- {
- // increase parents array in group 4 by 1
- groups[GROUP4]=(parents_t**)realloc(groups[GROUP4],sizeof(parents_t*)*size4);
- // check if allocation succeeded
- if(!groups[GROUP4])
- {
- printf("Memory allocation didn't succeed! \n");
- exit(1);
- }
- // put the parrent pointer in group 4
- groups[GROUP4][size4-1]=&arr[i];
- size4++;
- }
- else
- {
- // increase parents array in group 2 by 1
- groups[GROUP2]=(parents_t**)realloc(groups[GROUP2],sizeof(parents_t*)*size2);
- // check if allocation succeeded
- if(!groups[GROUP2])
- {
- printf("Memory allocation didn't succeed! \n");
- exit(1);
- }
- // put the parrent pointer in group 2
- groups[GROUP2][size2-1]=&arr[i];
- size2++;
- }
- }
- // increase parents array in group 5 by 1
- groups[GROUP5]=(parents_t**)realloc(groups[GROUP5],sizeof(parents_t*)*size5);
- // check if allocation succeeded
- if(!groups[GROUP5])
- {
- printf("Memory allocation didn't succeed! \n");
- exit(1);
- }
- // increase parents array in group 4 by 1
- groups[GROUP4]=(parents_t**)realloc(groups[GROUP4],sizeof(parents_t*)*size4);
- // check if allocation succeeded
- if(!groups[GROUP4])
- {
- printf("Memory allocation didn't succeed! \n");
- exit(1);
- }
- // increase parents array in group 2 by 1
- groups[GROUP2]=(parents_t**)realloc(groups[2],sizeof(parents_t*)*size2);
- // check if allocation succeeded
- if(!groups[GROUP2])
- {
- printf("Memory allocation didn't succeed! \n");
- exit(1);
- }
- // put nulls at the end of every array group
- groups[GROUP5][size5-1]=NULL;
- groups[GROUP4][size4-1]=NULL;
- groups[GROUP2][size2-1]=NULL;
- return groups;
- }
- void updateLastName(parents_t*** arr,int size, int originalParentsArrSize) // originalParentsArrSize is the size of parrents array
- {
- // define indexes and flag to continue or stop inout
- int i, j, flag = 1;
- // define array to input name
- char name[MAX_NAME];
- // define array to input updated name
- char updatedName[MAX_NAME];
- // define array to input choice
- char choice[MAX_CHOICE];
- const int MIN_ARR_SIZE = 0;
- // if the parents array is empty exit the function
- if(originalParentsArrSize == MIN_ARR_SIZE)
- {
- printf("No families to display");
- return;
- }
- do
- {
- // input choice
- printf("Enter 'y' to update last name, enter 'end' to end the program: ");
- gets(choice);
- printf("\n");
- if(!strcmp(choice,"end"))
- {
- break;
- }
- else if(!strcmp(choice,"y"))
- {
- // input the name to chnage
- printf("Enter last name you want to update: ");
- gets(name);
- for(i=0;i<size;i++)
- {
- for(j=0;arr[i][j]!=NULL;j++)
- {
- if(!strcmp(arr[i][j]->lastName, name))
- {
- // input the updated name
- printf("Enter updated last name: ");
- gets(updatedName);
- // free the aloocation of the old name
- free(arr[i][j]->lastName);
- // callocate memory and copy the new name
- arr[i][j]->lastName = strdup(updatedName);
- printf("\n");
- // print the parents devided by categories
- printParents(arr,size);
- flag=0;
- break;
- }
- }
- }
- if(flag)
- {
- printf("\nLast name didn't found\n\n");
- }
- flag=1;
- }
- else
- {
- printf("Not an option, please choose a different option \n\n");
- }
- }
- while(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement