using structures loops etc

Write a program that performs the following:
It creates an array of a structure student (What the structure contains is your choice).
It executes a loop that displays a menu as follows:
1.Add new student
2.Modify a student information
3.Display Array
4.Exit
It allows the user to choose one of the above menu options.
If the user chooses option 1, the program allows the input of the student information, builds a structure, create a thread passing the created structure as argument. The thread simply adds the structure to the array.
If the user chooses option 2, the program allows the entry of a key (such as student id), creates a thread passing the key as argument. The thread requests for the field to be modified and the new value and makes the required modification.
If the user chooses option 3, the program creates a thread that displays all filled elements of the array.


In each of the above cases the thread exits after having performed its task.

————————————————————————————

# include<stdio.h>
//# include<pthread.h>

struct student{
char name[15];
int id;
}//end structure

void printMenu();
void * funct1(void* arg);
void * funct2(void * arg);
void * funct3(void* k1);

struct student s[10];
int num=0;
// main
int main(){
int x=0;

//——–menu
printMenu();
while(scanf(“%d”,x)){

//———————first choice
if(x==1){
struct student s1;
printf(“enter name:\n”);
scanf(“%s”,s1.name);
printf(“enter id:\n”);
scanf(“%d”,s1.id);
pthread_t p;
pthread_create(&p,NULL,funct1,(void*)&s1);
num++;
printMenu();
}//endif

//——– ———–second choise

else if(x==2){
int k=0;
printf(“enter the key\n”);
scanf(“%d”,k);
pthread_t p1;
pthread_create(&p1,NULL,funct2,(void*)&k);
printmenu();

}//end elseif

//——————–third choise

else if(x==3){
pthread_t p3;
pthread_create(&p3,NULL,funct3,(void*)&x);
printMenu();

}//ending else if
else
exit(0);
}//while
}//main

//——————————– function 1———————-
void * funct1(void* arg){

char *nm;
int *i1;
nm=(*char)arg.name;
i1=(*int)arg.id;
s[num-1].name=(*nm);
s[num-1].id=(*i1);

}//end function

//——————————–function 2———————–
void * funct2(void* k1){

printf(“which modification to make:/n1.id/n2.name/nchoice:”);
scanf(“%d”,x);

int *ids;
ids=(*int)k1;
int idd=ids;
int i=0;
int break_while=0;

if(x==1){
printf(“enter new id:”);
int idi;
scanf(“%d”,idi);

while(break_while==0){

if(s[i].id==idd){
s[i].id=idi;
break_while=1;}//if
else
i++;

}//while

}//if x=1

else{
printf(“enter new name:”);
char nm[15];
scanf(“%s”,nm);

while(break_while==0){

if(s[i].id==idd){
s[i].name=nm;
break_while=1;}//if
else
i++;

}//while

}//else;

}//end function 2
//—————————-function 3———————
void * funct3(void* k1){
int i=0;

for(i;i<num;i++){
printf(“\nstudent:”,i);
printf(“\nstudent id:”,s[i].id);
printf(“\nstudent name:”,s[i].name);

}//end i

}//end funct3

//—————————- function printMenu
void printMenu(){
printf(“1. Add new student\n”);
printf(“2.Modify a student Information\n”);
printf(“3.Display array\n”);
printf(“4.exit\n”);

}//

Leave a Reply