Question

IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels...

IN C PROGRAMMING

A Tv_show structure keeps track of a tv show’s name and the channels (integer values) that broadcast the
show. For this problem you can ONLY use the following string library functions: strcpy, strlen, strcmp. You
MAY not use memcpy, memset, memmove. You can assume memory allocations are successful (you do not
need to check values returned by malloc nor calloc).

typedef struct tv_show {
char *name;
int num_channels, *channels;
} Tv_show;

a. Implement the init_tv_show function that has the prototype below. The function will:
       i. Dynamically allocate memory for a Tv_show structure.
       ii. Dynamically allocate memory to store a copy (deep copy) in the structure of the name parameter.
       iii. Dynamically allocate an array of integers in the structure (channels field) that will allow us to store the
       values provided in the channels array parameter.
       iv. Make sure you initialize the num_channels field accordingly.
       v. If the show, name, or channels parameter are NULL, no computation will take place and the function
       will return 0; otherwise the function will return 1.
   int init_tv_show(Tv_show **show, const char *name, int channels[], int num_channels);

I HAVE WRITTEN THE CODE BUT IT DOES NOT SEEM TO BE WORKING AND WANT SOME TO LOOK AT IT

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct tv_show {
char *name;
int num_channels, *channels;
} Tv_show;

int init_tv_show(Tv_show **show, const char *name,
       int channels[], int num_channels){
int i = 0;
  
if(show && name && channels){
return 0;
}
(*show) = malloc(sizeof(Tv_show));
(*show)->name = malloc((strlen(name)+1) * sizeof(char));
(*show)->channels = malloc((num_channels+1) * sizeof(int));
strcpy((*show)->name, name);
(*show)->num_channels = num_channels;
for (i = 0; i < num_channels; i++){
(*show)->channels[i] = channels[i];
  
}
return 1;
}

void print_tv_show(Tv_show *s){

int i =0;
  
printf("Show name: %s\n", s->name);
printf("Number Channels: %d\n", s->num_channels);
printf("Channels: /n");
for(i = 0; i < s->num_channels; i++){
printf("%d",s->channels[i]);
s->channels++;
}
printf("/n");
}

void free_tv_show(Tv_show **show){
int i;
free((*show)->name);
free((*show)->channels);

free(*show);
*show == NULL;
}

int main() {
Tv_show *show;
int channels[3] = {7, 51, 4};
init_tv_show(&show, "The Debugger", channels, 3);
print_tv_show(show);
free_tv_show(&show);
printf("%s\n", show == NULL ? "Cleared" : "not cleared");
return 0;
}

  

Homework Answers

Answer #1

Modified Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct tv_show {
char *name;
int num_channels, *channels;
}Tv_show;

//function to initialize tv_show
int init_tv_show(Tv_show **show, const char *name,
int channels[], int num_channels){

if(show==NULL || name==NULL || channels==NULL){
return 0;
}

int i = 0;

(*show) = malloc(sizeof(Tv_show));
(*show)->name = malloc((strlen(name)+1) * sizeof(char));
(*show)->channels = malloc((num_channels) * sizeof(int));

strcpy((*show)->name, name);
(*show)->num_channels = num_channels;

for (i = 0; i < num_channels; i++){
(*show)->channels[i] = channels[i];
}

return 1;
}

//function to print tv_show
void print_tv_show(Tv_show *s){

int i =0;

printf("Show name: %s\n", s->name);
printf("Number Channels: %d\n", s->num_channels);
printf("Channels: \n");

for(i = 0; i < s->num_channels; i++){
printf("%d\t",s->channels[i]);
}
printf("\n");
}

//function to deallocate memory of tv_show
void free_tv_show(Tv_show **show){
free((*show)->name);
free((*show)->channels);
free(*show);
*show = NULL;
}

//main function
int main() {
Tv_show *show;
int channels[3] = {7, 51, 4};

//initialize tv_show
init_tv_show(&show, "The Debugger", channels, 3);
//print tv_show
print_tv_show(show);
//deallocate memory of tv_show
free_tv_show(&show);

printf("%s\n", show == NULL ? "Cleared" : "not cleared");

return 0;
}

Output:

Show name: The Debugger
Number Channels: 3
Channels:
7 51 4
Cleared

Note: Whether you face any problem or need any modification then share with me in the comment section, I'll happy to help you.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
C Programming I have this function to i want to return the cipher text, but its...
C Programming I have this function to i want to return the cipher text, but its not working, can anyone try to see what i'm doing wrong. I need it to return the cipher text. char* vigenereCipher(char *plainText, char *k) { int i; char cipher; int cipherValue; int len = strlen(k); char *cipherText = (char *)malloc(sizeof(plainText) * sizeof(char)); //Loop through the length of the plain text string for (i = 0; i < strlen(plainText); i++) { //if the character is...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if you try to add to a position greater than the number of nodes you get an error warning. /INSERT.C /*THIS PROGRAM READS A BINARY FILE (MUST ALREADY EXIST AND BE FILLED) */ /*AND PUTS IT INTO A LINKED LIST AND PRINTS THE LIST TO THE SCREEN) */ #include #include #include #include typedef struct ENTRY { char name[81]; }ENTRY; typedef struct LISTREC /* LISTREC is...
Using the C programming language implement Heapsort in the manner described in class. Here is some...
Using the C programming language implement Heapsort in the manner described in class. Here is some example code to use as a guideline. Remember, you need only implement the sort algorithm, both the comparison and main functions have been provided. /* * * after splitting this file into the five source files: * * srt.h, main.c, srtbubb.c, srtinsr.c, srtmerg.c * * compile using the command: * * gcc -std=c99 -DRAND -DPRNT -DTYPE=(float | double) -D(BUBB | HEAP | INSR |...
Design flow chart of this code and make sure it is not handwritten it must on...
Design flow chart of this code and make sure it is not handwritten it must on some software. struct user {    int id;    int age;    bool annualClaim;    int plan;    char name[30];    char contactNum[15];    char address[50]; }; #define MAX_LENGTH 500 struct user users[100]; int userCount = 0; struct claim {    int id;    int claimedYear;    int amountClaimed;    int remaininigAmount; }; struct claim claims[100]; void loadData() {    char line[MAX_LENGTH];    const...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
This is C programming assignment. The objective of this homework is to give you practice using...
This is C programming assignment. The objective of this homework is to give you practice using make files to compose an executable file from a set of source files and adding additional functions to an existing set of code. This assignment will give you an appreciation for the ease with which well designed software can be extended. For this assignment, you will use both the static and dynamic assignment versions of the matrix software. Using each version, do the following:...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
C LANGUAGE IMPLEMENTATION - Writing source files implement several functions, declared in student.h, transcript.h and internal.h...
C LANGUAGE IMPLEMENTATION - Writing source files implement several functions, declared in student.h, transcript.h and internal.h (with support from common.h). There are 3 rules: 1. There are three types of students: undergraduate, MEng and PhD. 2. Undergraduates must complete 40 courses, MEng students 5 and PhD students 2. 3. Undergraduate students require a mark of 50 to pass; graduate students need a 65. ----------------Common.h-------------------------------------------------------------------------------------------------------------------------------------------------------------- #ifndef COMMON_H #define COMMON_H /*Portable macros for declaring C functions visible to C++.*/ #ifdef __cplusplus #define...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT