0% found this document useful (0 votes)
32 views

Subject: PRF192-PFC Workshop 07: Objectives: Managing Strings Managing Parallel Arrays

This document discusses managing parallel arrays in C programming. It includes functions for adding, removing, finding, and sorting employees using parallel arrays of employee codes, names, salaries, and allowances. The main function initializes the arrays and uses a menu-driven system to call these functions and manage a list of employees stored in the parallel arrays.

Uploaded by

Lâm Huỳnh
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)
32 views

Subject: PRF192-PFC Workshop 07: Objectives: Managing Strings Managing Parallel Arrays

This document discusses managing parallel arrays in C programming. It includes functions for adding, removing, finding, and sorting employees using parallel arrays of employee codes, names, salaries, and allowances. The main function initializes the arrays and uses a menu-driven system to call these functions and manage a list of employees stored in the parallel arrays.

Uploaded by

Lâm Huỳnh
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/ 24

Subject: PRF192- PFC

Workshop 07

Objectives:
Managing strings
Managing parallel arrays

Problem 1: (3 marks) Managing a list of student names

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
const int MAX = 100;
void showMenu(void);
void addNewStudent(char id[][MAX], char name[][MAX],
int yob[], double point[], int *size);
int findIndexOfStudent(char id[][MAX], int size, char key[]); void
removeStudentByPos(char id[][MAX], char name[][MAX],
int yob[], double point[], int *size);

int main()
{
char id[MAX][9];
char name[MAX][40];
int yob[MAX];
double point[MAX];

int size = 0;
int choose;
char buffer;

do{
showMenu();
do{ //ep nguoi dung nhap so nguyen
printf("\n plz input your choose: ");
scanf("%d", &choose); scanf("%c",
&buffer);
fflush(stdin);
if(buffer != 10)
{
printf("\n do you know input integer number !!!");
}
}while(buffer != 10);
switch(choose){ // chon option

case 1:{
printf("\n ");
addNewStudent(id, name, yob, point, &size); printf("\
n");
break;
}
case 2:{
printf("\n ");
removeStudentByPos(id, name, yob, point, &size);
printf("\n");
break;
}
case 3:{
printf("\n "); showInforByPos(id,
name, yob, point, size); printf("\n");
break;
}
case 4:{
printf("\n "); sortStudent(id,
name, yob, point, size); printf("\n");
break;
}
case 5:{
printf("\n ");
printf("\nHave a nice day!");
return;
break;
}default:{
printf("\n Plz, input from 1 to 5 !!!");
}
}
}while(choose != 7);

void showMenu(void){ //show menu


printf("\n ~~~~~~~~Student Management~~~~~~~~~");
printf("\n1. Add a student");
printf("\n2. Remove a student");
printf("\n3. Search a student");
printf("\n4. Print the list in ascending order"); printf("\n5.
Quit");
}

void addNewStudent(char id[][MAX], char name[][MAX],


int yob[], double point[], int *size){
printf("\n Input infor new student: ");
//nhap id
while(1==1){
printf ("\n plz, id: ");
gets(id[*size]);
chuanhoa(id[*size]); int
isFind = 0;
for(int i = 0; i<= *size -1; i++){
if(strcmp(id[*size], id[i]) == 0){
isFind =1;
}
}
if(isFind == 1){
printf("\n ID is duplicated");
}else{
break;
}
}
printf("\n Name: ");
gets(name[*size]);
chuanhoa(name[*size]);
fflush(stdin);

printf("\n Yob: ");


scanf("%d", &yob[*size]);
fflush(stdin);

printf("\n Point: ");


scanf("%lf", &point[*size]);
fflush(stdin);

(*size)++;

void removeStudentByPos(char id[][MAX], char name[][MAX], int yob[], double point[],


int *size){
char key[MAX];
printf("\n plz, input id which you to remove: "); gets(key);
int pos = findIndexOfStudent(id, *size, key); if(pos
== -1){
printf("\n Your student not existed");
}else{
printf("\n Student before removing: ");
printf("\n|%-8s|%-20s|%4d|%5.2lf|",
id[pos], name[pos], yob[pos], point[pos]);
for(int i = pos; i <=*size -1; i++){
strcpy(id[i], id[i+1]);
strcpy(name[i], name[i+1]);
yob[i] = yob[i +1];
point[i] = point[i+1];
}
(*size)--;
printf("\n Removing is successfull");
}
}

void showInforByPos(char id[][MAX], char name[][MAX], int yob[], double point[], int
size){
char key[MAX];
printf("\n plz, input id you need: ");
gets(key);
int pos = findIndexOfStudent(id, size, key);
if(pos == -1){
printf("\n Your student not existed");
}else{
printf("\n|%-8s|%-20s|%4d|%5.2lf|",
id[pos], name[pos], yob[pos], point[pos]);

};
}

int findIndexOfStudent(char id[][MAX], int size, char key[]){


for(int i =0; i <= size -1; i++){
if(strcmp(id[i], key) == 0){
return i;
}
}
return -1;
}

void sortStudent(char id[][MAX], char name[][MAX], int yob[], double point[], int size){
for(int i = 0;i <= size-1; i++){
for(int j =size-1; j>i; j--){
if( point [i] >= point[j]){
double tmp = point[i];
point[i] = point[j];
point[j] = tmp;

char strTmp[MAX];
strcpy(strTmp, id[i]);
strcpy(id[i], id[j]);
strcpy(id[j], strTmp);

strcpy(strTmp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], strTmp);

int tmp1 = yob[i];


yob[i] = yob[j];
yob[j] = tmp1;

}
}
}
showStudentList(id, name, yob, point, size);
}

void showStudentList(char id[][MAX], char name[][MAX],


int yob[], double point[], int size){
if(size == 0){
printf("\n Nothing to printf");
return;
}
for(int i = 0; i<= size -1; i++){ printf("\n|
%-8s|%-20s|%4d|%5.2lf|",
id[i], name[i], yob[i], point[i]);
}
}

void viethoa(char name[][MAX]){


int len = strlen(name);
char tmp[MAX];
strcpy(tmp,name);

for(int i=0; i<=len-1;i++){


tmp[i]=toupper(tmp[i]);
}
strcpy(name, tmp);
}
void xoakytu(char name[][MAX], int vt)
{
char tmp[MAX];
strcpy(tmp,name);

int i, cd=strlen(tmp);
for(i=vt;i<cd;i++)
tmp[i]=tmp[i+1];

strcpy(name, tmp);
}
void chuanhoa(char name[][MAX])
{
char tmp[MAX];
strcpy(tmp,name);

//Xoa khoang trang dau chuoi


while(tmp[0]==32)
xoakytu(tmp,0);

//Xoa khoang trang o giua


int cd=strlen(tmp);
for(int i=0;i<cd;i++)
{
if (tmp[i]==32)
{
if (tmp[i+1]==32)
{
int k=i;
while (tmp[i]==32)
{
xoakytu(tmp,i);
i=k+1;
}
}
}
}

//Xoa khoang trang cuoi chuoi


int k=strlen(tmp)-1;
while(tmp[k]==32)
{
xoakytu(tmp,k);
k--;
}

strcpy(name, tmp);

viethoa(name);
}
Problem 2: (3 marks) Managing a parallel arrays

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
const int MAX = 100;
void showMenu(void);
void addNewAnEmployee(char code[][MAX], char name[][MAX],
double salary[], double allowance[], int *size);
int findIndexOfEmployee(char code[][MAX], int size, char key[]); void
removeEmployeeByPos(char code[][MAX], char name[][MAX],
double point[], double allowance[], int *size);
void showInforByPos(char code[][MAX], char name[][MAX], double salary[], double
allowance[], int size);
void sortEmployee(char code[][MAX], char name[][MAX], double salary[], double
allowance[], int size);
void showEmployeeList(char code[][MAX], char name[][MAX],
double salary[], double allowance[], int size);

int main()
{
char code[MAX][9];
char name[MAX][40];
double salary[MAX];
double allowance[MAX];

int size = 0;
int choose;
char buffer;

do{
showMenu();
do{ //ep nguoi dung nhap so nguyen
printf("\n plz input your choose: ");
scanf("%d", &choose); scanf("%c",
&buffer);
fflush(stdin);
if(buffer != 10)
{
printf("\n do you know input integer number !!!");
}
}while(buffer != 10);
switch(choose){ // chon option

case 1:{
printf("\n ");
addNewAnEmployee(code, name, salary, allowance, &size); printf("\n");
break;
}
case 2:{
printf("\n ");
showInforByPos(code, name, salary, allowance, size);
printf("\n");
break;
}
case 3:{
printf("\n ");
removeEmployeeByPos(code, name, salary, allowance, &size); printf("\n");
break;
}
case 4:{
printf("\n ");
sortEmployee(code, name, salary, allowance, size);
printf("\n");
break;
}
case 5:{
printf("\n ");
printf("\nHave a nice day!");
return;
break;
}default:{
printf("\n Plz, input from 1 to 5 !!!");
}
}
}while(choose != 7);

void showMenu(void){ //show menu


printf("\n ~~~~~~~~Managing A Parallel Arrays~~~~~~~~~");
printf("\n1. Adding a new employee"); printf("\
n2. Finding data about employee"); printf("\n3.
Remove an employee"); printf("\n4. Print the list
in descending order"); printf("\n5. Quit");
}

void addNewAnEmployee(char code[][MAX], char name[][MAX],


double salary[], double allowance[], int *size){
printf("\n Input infor new employee: ");
//nhap code
while(1==1){
printf ("\nPlz, code: ");
gets(code[*size]);
while(strlen(code[*size]) >8 || strlen(code[*size]) <1){
printf("\nNo more than 1 - 8 code!!!");
printf ("\nPlz, code: ");
gets(code[*size]);
}
int isFind = 0;
for(int i = 0; i<= *size -1; i++){
if(strcmp(code[*size], code[i]) == 0){
isFind =1;
}
}
if(isFind == 1){
printf("\n Code is duplicated");
}else{
break;
}
}
printf("\n Name: ");
gets(name[*size]);
fflush(stdin);

printf("\n Salary: ");


scanf("%lf", &salary[*size]);
fflush(stdin);

printf("\n Allowance: ");


scanf("%lf", &allowance[*size]);
fflush(stdin);
(*size)++;

void removeEmployeeByPos(char code[][MAX], char name[][MAX], double salary[],


double allowance[], int *size){
char key[MAX];
printf("\n plz, input code which you want to remove: ");
gets(key);
int pos = findIndexOfEmployee(code, *size, key); if(pos
== -1){
printf("\n An employee not existed");
}else{
printf("\n An employee before removing: ");
printf("\n|%-8s|%-20s|%5.2lf|%5.2lf|",
code[pos], name[pos], salary[pos], allowance[pos]);
for(int i = pos; i <=*size -1; i++){
strcpy(code[i], code[i+1]);
strcpy(name[i], name[i+1]);
salary[i] = salary[i +1];
allowance[i] = allowance[i+1];
}
(*size)--;
printf("\n Removing is successfull");
}
}

void showInforByPos(char code[][MAX], char name[][MAX], double salary[], double


allowance[], int size){
char key[MAX];
printf("\n plz, input code you need: ");
gets(key);
int pos = findIndexOfEmployee(code, size, key);
if(pos == -1){
printf("\n An employee not existed");
}else{
printf("\n|%-8s|%-20s|%5.2lf|%5.2lf|",
code[pos], name[pos], salary[pos], allowance[pos]);

};
}

int findIndexOfEmployee(char code[][MAX], int size, char key[]){


for(int i =0; i <= size -1; i++){
if(strcmp(code[i], key) == 0){
return i;
}
}
return -1;
}

void sortEmployee(char code[][MAX], char name[][MAX], double salary[], double


allowance[], int size){
for(int i = 0;i <= size-1; i++){
for(int j =size-1; j>i; j--){
if( salary[i] + allowance[i] <= salary[j] + allowance[j]){
double tmp = salary[i];
salary[i] = salary[j];
salary[j] = tmp;

char strTmp[MAX];
strcpy(strTmp, code[i]);
strcpy(code[i], code[j]);
strcpy(code[j], strTmp);

strcpy(strTmp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], strTmp);

int tmp1 = allowance[i];


allowance[i] = allowance[j];
allowance[j] = tmp1;

}
}
}
showEmployeeList(code, name, salary, allowance, size);
}

void showEmployeeList(char code[][MAX], char name[][MAX],


double salary[], double allowance[], int size){
if(size == 0){
printf("\n Nothing to printf");
return;
}
for(int i = 0; i<= size -1; i++){
printf("\n|%-8s|%-20s|%5.2lf|%5.2lf|", code[i],
name[i], salary[i], allowance[i]);
}
}}
Problem 3: (4 marks) Managing a parallel arrays

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define MAX 100
void add(char name[][20],char make[][20],int volume[],int price[],int duration[],int &n)
{
n++;
printf("please enter the %dth name: ",n);
fflush(stdin);
scanf("%[^\n]",name[n]);
printf("please enter the %dth make: ",n);
fflush(stdin);
scanf("%[^\n]",make[n]);
printf("please enter the %dth volumn: ",n);
fflush(stdin);
scanf("%d",&volume[n]);
printf("please enter the %dth price: ",n);
fflush(stdin);
scanf("%d",&price[n]);
printf("please enter the %dth duration: ",n); fflush(stdin);
scanf("%d",&duration[n]);

void print(char name[][20],char make[][20],int volume[],int price[],int duration[],int &n)


{
for(int i=0; i<=n; i++)
{
printf("The information of %dth: ",i);
printf("%s %s %d %d %d\n",name[i],make[i],volume[i],price[i],duration[i]);
}
}

void FindItem(char name[][20],char make[][20],int volume[],int price[],int duration[],int


&n)
{
printf("Please enter the make you want to see: "); char
str[MAX];
fflush(stdin);
scanf("%[^\n]",str);
bool pos=0;
for(int i=0; i<=n; i++)
{
if(strcmp(make[i],str)==0)
{
pos=1;
printf("The information of %dth: ",i);
printf("%s %s %d %d %d\n",name[i],make[i],volume[i],price[i],duration[i]);
}
}
if(pos==0)
{
printf("Can not find the information of make\n");
}
}

void printVolume(char name[][20],char make[][20],int volume[],int price[],int


duration[],int &n)
{
int p1,p2;
printf("Please enter the first volume: ");
fflush(stdin);
scanf("%d",&p1);
printf("Please enter the second volume: ");
fflush(stdin);
scanf("%d",&p2);
if(p1>p2){
int tmp = p1;
p1 = p2;
p2 = tmp;
}
bool ok=0;
for(int i=0; i<=n; i++)
{
if(volume[i]>=p1 && volume[i]<=p2)
{
printf("The information of %dth: ",i);
printf("%s %s %d %d %d\n",name[i],make[i],volume[i],price[i],duration[i]);
ok=1;
}
}
if(ok==0)
{
printf("Sorry we can find out the Volume\n");
}
}
void sort(char name[][20],char make[][20],int volume[],int price[],int duration[],int &n)
{
for(int i=0; i<=n; i++)
{
for(int j=i+1; j<=n; j++)
{
if(volume[i]>volume[j])
{
int t=price[i];
price[i]=price[j];
price[j]=t;

char b[MAX];
strcpy(b,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],b);

char a[MAX];
strcpy(a,make[i]);
strcpy(make[i],make[j]);
strcpy(make[j],a);

int tmp=duration[i];
duration[i]=duration[j];
duration[j]=duration[i];

int tmp1=volume[i];
volume[i]=volume[j];
volume[j]=tmp1;
}
}
}
for(int i=0; i<=n; i++)
{
for(int j=i+1; j<=n; j++)
{
if(volume[i]==volume[j])
{
if(price[i]>price[j])
{
int t=price[i];
price[i]=price[j];
price[j]=t;

char b[MAX];
strcpy(b,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],b);

char a[MAX];
strcpy(a,make[i]);
strcpy(make[i],make[j]);
strcpy(make[j],a);

int tmp=duration[i];
duration[i]=duration[j];
duration[j]=duration[i];

int tmp1=volume[i];
volume[i]=volume[j];
volume[j]=tmp1;
}
}
}
}
print(name,make,volume,price,duration,n);
}
int main()
{
int ch;
int n=-1;
do
{
printf("1-Adding a new soft drink\n");
printf("2-Printing out items which belong to a known make\n");
printf("3-Printing out items whose volumes are between v1 and v2
( integers)\n");
printf("4-Printing the list in ascending order based on volumes then prices.\n");
printf("Please enter your choice: ");
fflush(stdin);
scanf("%d",&ch);
char make[MAX][20],name[MAX][20];
int duration[MAX],volume[MAX],price[MAX];
switch(ch)
{
case 1:
{
add(name,make,volume,price,duration,n);
break;
}
case 2:
{
FindItem(name,make,volume,price,duration,n); break;
}
case 3:
{
printVolume(name,make,volume,price,duration,n); break;
}
case 4:
{
sort(name,make,volume,price,duration,n);
break;
}
}
}
while(ch>=1 && ch<=4);
return 0;
}

You might also like