Question

In: Computer Science

I am comfortable with structures that contains one item per variable. But i am struggling to...

I am comfortable with structures that contains one item per variable. But i am struggling to manage structure that contains multiple items in one variable (within a matrix). it's in C language.

for example this structure :

   struct resume{
char *name;
char *job;
char school*;
int counter_resume;

}resume;

i want to create functions that are able to dynamically allocate memory so it can add multiple persons resume without knowing how many they are going to be in the beginning. Lets say we have Bob . Bob worked as a fireman, postman, and truck driver. So there is 3 items in the Jobs variable for Bob. let say he went to school to Marveric and Standford. We should be able to add more persons in the same structure using the functions. for example the main look like this :

nb: feel free to give more knowledge, suggestions, or any useful advices or  information needed for me to master this particular subject.

int main()
{
struct resume *Bob = malloc(sizeof(struct resume));

add_name(Bob,"Bob");

add_job(Bob, "Postman");
add_job(Bob, "Truck driver");
add_job(Bob, "fireman");

add_school(Bob, "Standford University");
add_school(Bob, "Chicago business school");

print_single_resume(Bob); // print Bob resume
print_all_resume(); // print all of the resumes

free_memory(bob); // function that free the memory allocated

free_all(); // function that free the memory allocated for all the persons
}

Solutions

Expert Solution

We can use arrays to store more than one values for the same data type. Here we need to keep the count of elements in the array. The initial count is 0 and when we add elements to the array, increment the count by 1. The array index starts with 0. We can add each element at the specified index of the array. We need to specify the maximum size of the array at the time of declaration.

#include<stdio.h>
#include<stdlib.h>
struct resume{
    char *name;
    char *job[10];    // array of jobs(maximum 10 jobs
    int jobCount;     // count of jobs
    char *school[10]; // array of school(maximum 10 schools)
    int schoolCount; // count of schools
    int counter_resume;
}resume;

// array of resume and the count of resumes
struct resume* resumes[10];
int resumeCount = 0;

// function to add name to a resume
void add_name(struct resume *r, char* name){
    r->name = name;
}
// function to add job to a resume
void add_job(struct resume *r, char* job){

    r->job[r->jobCount++] = job;
}
// function to add school to a resume
void add_school(struct resume *r, char* school){
    r->school[r->schoolCount++] = school;
}
// function to print a resume
void print_single_resume(struct resume *r){
    printf("\nName: %s\n", r->name);
    printf("Jobs: ");
    int i;
    for(i=0; i<r->jobCount-1; i++){
        printf("%s,", r->job[i]);
    }
    printf("%s\n", r->job[i]);
    printf("Schools: ");
    for(i=0; i<r->schoolCount-1; i++){
        printf("%s,", r->school[i]);
    }
    printf("%s\n", r->school[i]);
}
// function to print all resumes
void print_all_resume(){
    int i;
    for(i=0; i<resumeCount; i++){
        print_single_resume(resumes[i]);
    }
}
// function to free memory of a resume
void free_memory(struct resume *r){
    free(r);
}
// function to free memory of all resumes
void free_all(){
    int i;
    for(i=0; i<resumeCount; i++){
        free_memory(resumes[i]);
    }
}
int main()
{
    struct resume *Bob = malloc(sizeof(struct resume));

    add_name(Bob,"Bob");

    // initialize counts to 0
    Bob->jobCount = 0;
    Bob->schoolCount = 0;

    add_job(Bob, "Postman");
    add_job(Bob, "Truck driver");
    add_job(Bob, "fireman");

    add_school(Bob, "Standford University");
    add_school(Bob, "Chicago business school");

    // add Bob to resumes
    resumes[resumeCount++] = Bob;

    struct resume *Jay = malloc(sizeof(struct resume));
    // initialize counts to 0
    Jay->jobCount = 0;
    Jay->schoolCount = 0;
    add_name(Jay,"Jay");
    add_job(Jay, "Postman");
    add_school(Jay, "Standford University");
    // add Jay to resumes
    resumes[resumeCount++] = Jay;

    printf("\nBob's resume:");
    print_single_resume(Bob); // print Bob resume

    printf("\n\nAll resumes:\n");
    print_all_resume(); // print all of the resumes

    free_memory(Bob); // function that free the memory allocated

    free_all(); // function that free the memory allocated for all the persons
}

Output


Related Solutions

Hi, I am taking stats class and I have one question. I have been struggling with...
Hi, I am taking stats class and I have one question. I have been struggling with it for few hours. To investigate the fluid mechanics of swimming, twenty swimmers each swam a specified distance in a water-filled pool and in a pool where the water was thickened with food grade guar gum to create a syrup-like consistency. Velocity, in meters per second, was recorded and the results are given in a table below. The researchers concluded that swimming in guar...
Assembly Question: I am trying to annotate this code and am struggling to understand what it...
Assembly Question: I am trying to annotate this code and am struggling to understand what it is doing. Can someone please add comments? .data .star: .string "*" .line: .string "\n" .input: .string "%d" .count: .space 8 .text .global main printstars: push %rsi push %rdi _printstars: push %rdi mov $.star, %rdi xor %rax, %rax call printf pop %rdi dec %rdi cmp $0, %rdi jg _printstars mov $.line, %rdi xor %rax, %rax call printf pop %rdi pop %rsi ret printstarpyramid: mov %rdi,...
I am struggling with certain types of factoring. I recognize there is a special situation, but...
I am struggling with certain types of factoring. I recognize there is a special situation, but don't understand how to factor it. For example y2+24yw-36w What's the procedure on determining how to factor this?
Hi there I need to write an ethic issue in technology but I am struggling to...
Hi there I need to write an ethic issue in technology but I am struggling to choose a topic. I am asking about the possibility of "Ethical issues" topics that is tread common in technology. Would anyone can give me some options to choose from. Thank you.
I am struggling with the following accounting problem. I need to calculate the following breakeven point...
I am struggling with the following accounting problem. I need to calculate the following breakeven point in dollars. Sales 240 units or $1,200,000 ($5.00 per unit), cost of goods sold 784,000, gross profit 416,000, operating expense are 280,000 for selling and 156,000 for administrative expenses. The analysis indicates 75% of COGS are variable, 42% of selling are variable and 40% of administrative are variable. Those numbers are $588,000, 117,600 and 62,400. The fixed cost are 25% of COGS, 58% of...
I need to draw my first name using Turtle. I am struggling with "P" and "A"...
I need to draw my first name using Turtle. I am struggling with "P" and "A" this is what I have import turtle turtle.setup(800, 700) window = turtle.Screen() window.reset() window.bgcolor('cornsilk') t = turtle.Turtle() t.color('blue') t.pencolor('red') t.turtlesize(6) t.speed(2) t.up() t.setposition(-50, 0) t.pendown()#Drawing letter T t.forward(40) t.back(20) t.right(90) t.forward(50) t.left(90) t.penup() t.forward(70) t.left(90) t.forward(25) t.pendown() t.circle(25)# Drawing letter O t.penup() t.left(180) t.forward(25) t.left(90) t.forward(10) t.pendown()#Drawing letter N t.left(90) t.forward(50) t.right(150) t.forward(60) t.left(150) t.forward(53) t.back(53) t.right(90) turtle.done()
I am really struggling with this chemistry problem. I don`t even know how to start.. The...
I am really struggling with this chemistry problem. I don`t even know how to start.. The problem is about recrystallization: Two crystalline compounds x and y have the same solubility ratio in water( the solubility is: 1 g/100 mL at 20 degree celsius and 10 g/100 mL at 100 degree celsius) In this example we predict that if the x and y compunds is in the mix, they have the same solubility ratio. 1. From a mix of 10 g...
I am struggling with c++ and I could use some guidance getting through this project. Below...
I am struggling with c++ and I could use some guidance getting through this project. Below are my directions. The objective of this project is to be able to incorporate structures in a program. The program will have the user enter the information for two movies (details below) and then display a list of recommended movies that have 4 or more stars. 1. You will need to create a structure named MovieInfo that contains the following information about a movie:...
I am struggling to even get started on this assignment. its a teach yourself kind of...
I am struggling to even get started on this assignment. its a teach yourself kind of situation and I have and no issues up to this point but I just can't wrap my head around it. Implement the Account class. Add a public enum called ACCOUNT_TYPE with two values CHECKING, SAVING Add the following private instance variables to the Account class: An instance variable called aType of type Enum ACCOUNT_TYPE. An instance variable called accountOwner of type String. An instance...
I am struggling with figureing this out as a practicing question! Please show me the work...
I am struggling with figureing this out as a practicing question! Please show me the work for how to get the correct answers! ABC Company produces two separate products, product A and product B.  Information on product A and B is reported below: Product A                                Product B Sales Price:                  $1,000                                     $1,800 Direct Materials          $100                                        $500 Direct Labor                $250                                        $600 Variable Overhead      $50                                          $400 Commissions:             $25                                          $40 Company information: Fixed Manufacturing overhead:          $400,000 per year Fixed Administrative Expenses:          $375,000 per year Tax rate:                                              25% ABC Company has to decide which of the following sales mixes will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT