Question
C program

Main topics: Files Program Specification: For this assignment, you need only write a single-file C program. Your program will
<value><d><value><d> <eof> <d><value><eo1n> Note: Each <value> is a float number (as text) Each <d> is a delimiter character
Main topics: Files Program Specification: For this assignment, you need only write a single-file C program. Your program will: Define the following C structure sample typedef struct int sarray size; the number of elements in this sanple's array floatsarray the sample's array of values sample Each sample holds a variable number of values, all taken together constitute a Sample Point Write a separate function to Read a file of delimited Sample Points into an array of pointers to (sample structs). Note: you may use a statically sized array of size 1024, however, each element of the array must be a pointer to a sample struct (as defined above). Initially each element of the array must be set to NULL (to indicated that it is not used), and then later to a dynamically allocated sample struct if it is to be used. Note: after dynamically allocating memory for a sample struct, you have enough memory for each/every component of the structure. However, for each component of that structure that is a pointer, you will eventually need to allocated memory for the thing that it points to as well. You will also have to remember this when freeing memory via a pointer to a sample struct Write a separate function to: Create a new (buffered binary) file and write all the used elements of the array (of sample structs) to this file. Write a separate Free all of the used elements of the array (of sample structs) that means all the memory that was allocated for each element (the structure and its components ) ate function to: Write a separate function to Read a file of your sample structs, (as it was created by your second function) and display each in a reasonable report like format to the screen. Note: you will need to create a text file of of delimited Sample data records in an editor, and store it in the same project as your C-program file in order to test/run your program file.txt valued>.. value>ceoln>
Note: Each is a float number (as text) Each is a delimiter character that would not occur "naturally", like Each line of the file constitutes one Sample Point, and can be coverted and stored into one sample struct Each line of the file does not necessarily contain the same number of s
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
#define MAX 1024

typedef struct
{
int array_size; // the number of elements in the sample's array
float *sarray; // The sample's array of values
}sample;

// Function to read dates from file
void readFile(sample *mySample)
{
// Creates a file pointer to open the file SampleNumbers.txt in read mode
FILE *rFile = fopen("SampleNumbers.txt", "r");

// Loop variable
int pos = 0;
float data;

// Extracts data from file and count
while (!feof(rFile))
{
// Extracts float numbers separated by comma
fscanf(rFile, "%f,", &data);
// increase the counter by one
pos++;
}// End of while loop

// Assigns the counter value to array size
mySample->array_size = pos;

// Dynamically allocates memory to array with counter value
mySample->sarray = (float *) malloc(sizeof(float) * pos);

// Close the file
fclose(rFile);

// Re opens the file for reading data
rFile = fopen("SampleNumbers.txt", "r");

// Loops till number of elements
for(pos = 0; pos < mySample->array_size; pos++)
// Extracts float numbers separated by comma and stores it at pos index position
fscanf(rFile, "%f,", &mySample->sarray[pos]);

// Close the file
fclose(rFile);
}// End of function

// Function to display contents of sample
void show(sample mySample)
{
// Loop variable
int c;

// Loops till number of elements in array
for(c = 0; c < mySample.array_size; c++)
// Displays each element of the array
printf("\n %.2f", mySample.sarray[c]);
}// End of function

// Function to write data of sample to file
void writeFile(sample mySample)
{
// Creates a file pointer to open the file SampleNumbers1.txt in write binary mode
FILE *wFile = fopen("SampleNumbers1.txt", "wb");
// Loop variable
int c;

// Loops till number of elements in array
for(c = 0; c < mySample.array_size; c++)
// Writes each element of the array separated by comma
fprintf(wFile, "%.2f,", mySample.sarray[c]);
}// End of function

// Function to release the memory occupied by sample
void releaseMemory(sample *mySample)
{
// Loop variable
int c;
// Deletes the array
free(mySample->sarray);
// Resets the array size to 0
mySample->array_size = 0;
}// End of function

// main function definition
int main()
{
// Declares an object of sample
sample mySample;

// Calls the function to read file contents and stores it in mySample
readFile(&mySample);

// Calls the function to display sample contents
show(mySample);

// Calls the function to write the contents of mySample to file
writeFile(mySample);

// Displays the sample size before release memory
printf("\n Before release memory array size: %d", mySample.array_size);

// Calls the function to release memory
releaseMemory(&mySample);

// Displays the sample size after release memory
printf("\n After release memory array size: %d", mySample.array_size);
}// End of main function

Sample Output:

12.20
27.30
18.60
55.30
44.70
2.30
6.70
7.40
1.20
2.90
1.47
9.10
11.90
78.10
8.11
3.77
4.60
Before release memory array size: 17
After release memory array size: 0

SampleNumbers.txt file contents
12.2,27.3,18.6,55.3,44.7,2.3,6.7,7.4,1.2,2.9,1.47,9.1,11.9,78.1,8.11,3.77,4.6

SampleNumbers1.txt file contents

12.20,27.30,18.60,55.30,44.70,2.30,6.70,7.40,1.20,2.90,1.47,9.10,11.90,78.10,8.11,3.77,4.60,

Add a comment
Know the answer?
Add Answer to:
Main topics: Files Program Specification: For this assignment, you need only write a single-file ...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This code should be in C please!! Your file should meet the following criteria: • Function...

    This code should be in C please!! Your file should meet the following criteria: • Function prototype and implementation for a calculateDivisors function that takes in an integer parameter, calculates the sum of its divisors, and returns that sum (as an integer value) • A structure that represents what is represented on each line of output, e.g., o Line number o Sum of the divisors for that line number o Character array containing “Perfect”, “Deficient”, or “Abundant” • Pointer declared...

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • CS 241 Program 03 Due: Thursday, October 18th Main topics: Arrays& Pointers Memory allocation ram Specification:...

    CS 241 Program 03 Due: Thursday, October 18th Main topics: Arrays& Pointers Memory allocation ram Specification: A stack is a container that can be defined in terms of an array where all adds are preformed at the end of the sequence of existing values, and all removes are also preformed at end of the sequence of existing values. An empty stack is one that has no existing values in the array at all. We use the notion of top of...

  • 3460:209 Assignment 9-B Assignment9-B: The Element Shifter The purpose of this assignment is to help gauge...

    3460:209 Assignment 9-B Assignment9-B: The Element Shifter The purpose of this assignment is to help gauge your skills in writing small programs that involve pointers. The program also contains functions and may perform input, output, files and file processing, use arrays and vectors and/or c-string/string arrays, flow of control, and/or calculations. PROGRAM SPECIFICATION For this program, we are going to expand a standard array by dynamically allocating a new one with a larger footprint. The program will use a function...

  • Can anyone help me with my C hw? Exercise 3 You will write a new program...

    Can anyone help me with my C hw? Exercise 3 You will write a new program that combines dynamically allocating an array and saving that array to a file. These are the tasks your program must perform Open an output file named "data.txt" and prepare it for writing in text mode o If the file handle is NULL, quit the program o By default, it is created and stored in the same directory as your source code file Prompt the...

  • C programming. 1.Create a program that does the following - Creates three pointers, a character pointer...

    C programming. 1.Create a program that does the following - Creates three pointers, a character pointer professor, and two integer pointers student_ids, grades - Using dynamic memory, use calloc to allocate 256 characters for the professor pointer - Prompts the professor for their name, and the number of students to mark. - Stores the professor’s name using the professor pointer and in an integer the number of students to mark. - Using dynamic memory, use malloc to allocate memory for...

  • C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You...

    C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You will be implementing the following structure and functions: struct LinkedList { int value; LinkedList *next; }; Note that with a singly linked list, you need to maintain a head pointer (pointer to the beginning of the list). Typically a tail pointer (pointer to the end of the list) is not maintained in a singly linked list (because you can only iterate...

  • Pointer

    Please answer in C++Design and implement a program that stores the following numbers in an array named kilometers: 21, 23, 27, 18, 16, 22, and 19.The program should use a loop that displays the contents of the array.Next, create a pointer variable called kiloPointer and assign it the value of the address of the first element in the array. Use a loop that displays the array using pointer notation. The loop should display two columns of numbers, the value of the pointer and the...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • A. Write an Array Program Write a main program that counts the number of occurrences of...

    A. Write an Array Program Write a main program that counts the number of occurrences of the number 6.0 in a large array of doubles and then prints out the number of elements in the array and the number of values that are 6.0. Also, compute and print the average value to 7 decimal places of all the elements in the array. 1. Use a for loop. The array fArray [ is defined in a file called Lab8Adatasetx.h that will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT