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;
}
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.
Get Answers For Free
Most questions answered within 1 hours.