In: Computer Science
Write a C program that creates a database of plant height samples. In this scenario a researcher enters a name of a group/type of plants, and heights for each plant in the group/type Use input will be stored in a database file, which should be a file called database.csv and should be in the following formatPlant1, number_of_samples, sample1, sample2, sample3 ...Plant2, number_of_samples, sample1, sample2, sample3 ...Plant3, number_of_samples, sample1, sample2, sample3 ...Lines starts with the name of a plant type, then an integer indicating the number of following samples. Each sample represents the height of a plant.In your program you should declare a function “input_sample_set” that prompts the user to input plant name and sample count. In this function dynamically allocate memory an array of ints for each plant sample and read in each sample value. Append the data to the database file. Prompt the user “y” or “n” to continuously re-run “input_sample_set” if y is entered. Make sure you deallocate memory after each run of “input_sample_set” to avoid memory leaks.
C program to create a database of plant height samples.
#include<stdio.h>
#include<conio.h>
Struct plant_sample
{
char Group_name[15];
int n;
int* sample_height;
}
Main()
{
Struct plant_sample p;
do
{
Printf(“\n enter plant group name: \t”);
gets(p.Group_name);
Printf(“\n Enter howmany samples available: \t”);
Scanf(“%d”,&p.n);
Input_sample_set(p.n);
Fp= fopen(("database.csv", "w+");
// specify the path where you want to store the file
if (fp == NULL)
{
fprintf(stderr, "\nError open file\n");
exit (1);
}
fwrite
(&p,
sizeof(struct
person), 1,
fp);
printf(“\n do you want to continue (enter y or
n):”);
ch=getch();
}while(ch==’y’||
‘Y’);
}
Void Input_sample_set(int n)
{
ptr = (int*) malloc(n * sizeof(int));
// if memory cannot be allocated
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter sample height: ");
for(i = 0; i < n; ++i)
{
Printf(“sample %d”,i+1)
scanf("%d", ptr + i);
}
// deallocating the memory
free(ptr);
}