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 this assignment. I can't get the program to run when I enter...
I am struggling with this assignment. I can't get the program to run when I enter a number with the $ symbol followed by a number below 10. any help would be greatly appreciated. Create a program named Auction that allows a user to enter an amount bid on an online auction item. Include three overloaded methods that accept an int, double, or string bid. Each method should display the bid and indicate whether it is over the minimum acceptable...
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 struggling with this java code Objectives: Your program will be graded according to the...
I am struggling with this java code Objectives: Your program will be graded according to the rubric below. Please review each objective before submitting your work so you don’t lose points. 1. Create a new project and class in Eclipse and add the main method. (10 points) 2. Construct a Scanner to read input from the keyboard. (10 points) 3. Use the Scanner to read the name, number of balls, and yards per ball of a yarn type specified by...
I am taking a Data Structures and Program Design class. BUT, I am having trouble getting...
I am taking a Data Structures and Program Design class. BUT, I am having trouble getting access to the vectors so that I can compare the guesses with the random numbers. I’ve been working on this for several days and don't feel any closer to a solution and could really use some help as I really don't know how to get this to work. Thank you! The assignment is: Design an ADT for a one-person guessing game that chooses 4...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT