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
Write a function to create a copy of a string with the prototype below. (C Language)...
Write a function to create a copy of a string with the prototype below. (C Language) char * strcpy (const char *); The function should dynamically allocate the necessary memory for the new string. You can do that with char * newstr = (char *) malloc(sizeof(char) * (strlen(str)+1)); assuming str is your input string. Think though how you would copy the characters of the string, and don't forget about the end of string character.
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...
For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what...
For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what the issue was in the `Error:` section of each problem. And fix the code to make it work. // P0 #include <stdio.h> #include <stdlib.h> /* Error: */ void fib(int* A, int n); int main(int argc, char *argv[]) { int buf[10]; unsigned int i; char *str; char *printThisOne; char *word; int *integers; int foo; int *bar; char *someText; // P1 for (i = 0; i...
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...
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
"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...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is always zero? Please fix and explain! #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int BinarySearch(const int A[],const int L,const int R,const int key) {              if (R >= 1){           int middle = L + (R-1)/2;                              if (A[middle] == key)        return A[middle];               if (A[middle] > key){...