Question

In: Computer Science

Write a C program that creates a database of plant height samples. In this scenario a...

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.

Solutions

Expert Solution

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);

}

                                                   


Related Solutions

Write a program (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
Write a C program that creates a toy scheduler for the child processes in part A....
Write a C program that creates a toy scheduler for the child processes in part A. This program takes as input the number of processes, and all of the PIDs that are being echoed. HINT: Look up redirecting echo output. The program will schedule the ”processes” (note that these are not true processes, this is a toy system. You are effectively only scheduling echo statements). The result of the scheduler will be to echo the PID and current system time...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
C++ : Write a program that creates a login name for a user, given the user's...
C++ : Write a program that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input. Output the login name, which is made up of the first five letters of the last name, followed by the first letter of the first name, and then the last two digits of the number (use the % operator). If the last name has less than five letters, then use all letters of the...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
1. a. In C++, Write a program that creates an array of 20 integers and initializes...
1. a. In C++, Write a program that creates an array of 20 integers and initializes it with the even values starting from 200. i.e. 200, 202, 204…. b. Write the elements of the array to the file even.txt, each element on a separate line. Try to use a single for loop for initializing the array and writing to file. You will need a separate counter for initializing the array. 2. a. Write another program that opens the file even.txt...
Write a c program that creates a struct to be able to read a .img file...
Write a c program that creates a struct to be able to read a .img file and a .pat file.
(C++) Write a program to read from a grade database (data.txt). The database (text file) has...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has students names, and grades for 10 quizzes.Use the given function prototypes to write the functions. Have main call your functions. The arrays should be declared in main() and passed to the functions as parameters. This is an exercise in parallel arrays, int and char 2 dim arrays. Function prototypes: int readData(ifstream &iFile, int scores[][10], char names[][30]); This functions takes the file stream parameter inFile...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must create a list. The list is composed of integer double pairs. There is one pair per line. The integers are in sequence (0, 1, 2, 3, ...) beginning with zero and ending with some random value between 512 and 1024. The doubles should be random values between 50.000 and 90.000. The doubles only have 3 decimal places. The file should look like this (of...
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT