What is going on with my output? Explain whatever you can please. Here is my cod
ID: 3573971 • Letter: W
Question
What is going on with my output? Explain whatever you can please. Here is my code:
#include "numinfo.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NO_VALUE (-666666.0)
static int lineCount;
static int unqCount;
static double *doubles;
// This will be called first.
void initialize(){
}
// This will be called last.
void terminate(){
free(doubles);
}
/* Read a file of doubles into dynamically-allocated memory. If it
* succeeds, it returns zero. If it fails for any reason, including
* encountering non-numeric data, it returns non-zero, without producing
* any error message. If called more than once, the values accumulate.
*/
int readfile(const char *filename){
int i = 0;
double temp;
double swap = 0.0;
char line[] = {};
FILE *in = fopen(filename, "r");
if(in == NULL){
fprintf(stderr, "cannot open %s ", filename);
return 1;
}
while(fgets(line, sizeof(line), in) != NULL){
sscanf(line, "%lf", &temp);
if(line[i] == temp){
lineCount++;
}
i++;
}
doubles = malloc(lineCount * sizeof(double));
for(int j = 0; j < lineCount; j++){
sscanf(line, "%lf", &doubles[i]);
}
fclose(in);
//Sorting
for(int k = 0; k < lineCount-1; k++){
for(int l = k+1; l < lineCount; l++){
if(doubles[l] < doubles[k]){
swap = doubles[k];
doubles[k] = doubles[l];
doubles[l] = swap;
}
}
}
return 0; //Success
}
// Returns how many numbers have been read, from all files.
int count(){
return lineCount;
}
// Return how many unique numbers have been read, from all files.
int unique_count(){
for(int i = 0; i < lineCount-1; i++){
for(int j = i+1; j < lineCount; j++){
if(doubles[i] == doubles[j]){
unqCount++;
}
}
}
return unqCount;
}
/* Return a single value, assuming sorted order.
* If n 0, return the corresponding value, counting from the lowest:
* n = 0 retrieves the lowest value.
* If n < 0, count from the highest value:
* n = -1 retrieves the highest value.
* n = -2 retrieves the next highest value.
* For an invalid n, return NO_VALUE.
*/
double nth(int n){
for(int i = 0; i < lineCount; i++){
if(n >= 0){
return doubles[n];
}
if(n == 0){
return doubles[0];
}
if(n < 0){
return doubles[i-n];
}
if(n == -1){
return doubles[i-1];
}
if(n == -2){
return doubles[i-2];
}
}
return NO_VALUE;
}
/* Return the median (the middle value, as sorted).
* If count() is even, return the arithmetic mean of the two middle values (adding values and divide by 2).
* If count()==0, return NO_VALUE.
*/
double median(){
if(lineCount % 2 == 0){
return ((doubles[lineCount/2] + doubles[lineCount/2 - 1]) / 2.0);
}
if(lineCount == 0){
return NO_VALUE;
}
return 0;
}
HERE IS MY MAIN FUNCTION:
#include "numinfo.h"
#include <stdio.h>
int main() {
initialize();
readfile("hw4.data");
printf("There are %d values. ", count());
printf("There are %d unique values. ", unique_count());
printf("Smallest: %f ", nth(8));
printf("Largest: %f ", nth(-1));
terminate();
return 0;
}
THIS IS MY OUTPUT:
There are 15 values.
There are 28 unique values.
Smallest: 0.000000
Largest: 0.000000
THIS IS MY DATA FILE:
1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
1243.234 23.23 234.123 12.34 56.78 90.10
Explanation / Answer
Answer
I have addes comments for each line and its what type of operation performs.
What is going on with my output? Explain whatever you can please. Here is my code:
#include "numinfo.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NO_VALUE (-666666.0)
// here declared lineCount as the static varibale
//counts how many lines are there
static int lineCount;
// here declared unqCount as the static variable
//find the how many unique values are there
static int unqCount;
//doubles declaredc
static double *doubles;
// This will be called first.
void initialize(){
}
// This will be called last.
void terminate(){
free(doubles);
}
/* Read a file of doubles into dynamically-allocated memory. If it
* succeeds, it returns zero. If it fails for any reason, including
* encountering non-numeric data, it returns non-zero, without producing
* any error message. If called more than once, the values accumulate.
*/
int readfile(const char *filename){
int i = 0;
double temp;
double swap = 0.0;
char line[] = {};
//read the data from file in read mode
FILE *in = fopen(filename, "r");
//file pointer is null, unable to open file
if(in == NULL){
fprintf(stderr, "cannot open %s ", filename);
return 1;
}
while(fgets(line, sizeof(line), in) != NULL){
//read the each line from file
sscanf(line, "%lf", &temp);
if(line[i] == temp){
//increase the linecount varibale
lineCount++;
}
i++;
}
//allocates the memory for varible
doubles = malloc(lineCount * sizeof(double));
for(int j = 0; j < lineCount; j++){
//store the data
sscanf(line, "%lf", &doubles[i]);
}
//file is closed here
fclose(in);
//Sorting
for(int k = 0; k < lineCount-1; k++){
for(int l = k+1; l < lineCount; l++){
if(doubles[l] < doubles[k]){
//swap the elements and usnig sorting concept
swap = doubles[k];
doubles[k] = doubles[l];
doubles[l] = swap;
}
}
}
return 0; //Success
}
// Returns how many numbers have been read, from all files.
int count(){
return lineCount;
}
// Return how many unique numbers have been read, from all files.
int unique_count(){
for(int i = 0; i < lineCount-1; i++){
for(int j = i+1; j < lineCount; j++){
//unique values count here
if(doubles[i] == doubles[j]){
unqCount++;
}
}
}
return unqCount;
}
/* Return a single value, assuming sorted order.
* If n = 0, return the corresponding value, counting from the lowest:
* n = 0 retrieves the lowest value.
* If n < 0, count from the highest value:
* n = -1 retrieves the highest value.
* n = -2 retrieves the next highest value.
* For an invalid n, return NO_VALUE.
*/
//find nth value here
double nth(int n){
for(int i = 0; i < lineCount; i++){
if(n >= 0){
return doubles[n];
}
if(n == 0){
return doubles[0];
}
if(n < 0){
return doubles[i-n];
}
if(n == -1){
return doubles[i-1];
}
if(n == -2){
return doubles[i-2];
}
}
return NO_VALUE;
}
/* Return the median (the middle value, as sorted).
* If count() is even, return the arithmetic mean of the two middle values (adding values and divide by 2).
* If count()==0, return NO_VALUE.
*/
//calculate the median value using lineCount
double median(){
if(lineCount % 2 == 0){
return ((doubles[lineCount/2] + doubles[lineCount/2 - 1]) / 2.0);
}
if(lineCount == 0){
return NO_VALUE;
}
return 0;
}
HERE IS MY MAIN FUNCTION:
#include "numinfo.h"
#include <stdio.h>
int main() {
initialize();
//access the file and open in read mode
readfile("hw4.data");
//display the number of values in count format
printf("There are %d values. ", count());
//display the unique value
printf("There are %d unique values. ", unique_count());
//finds smalles and largest value from data of the file
printf("Smallest: %f ", nth(8));
printf("Largest: %f ", nth(-1));
terminate();
return 0;
}
THIS IS MY OUTPUT:
There are 15 values.
There are 28 unique values.
Smallest: 0.000000
Largest: 0.000000
THIS IS MY DATA FILE:
1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
1243.234 23.23 234.123 12.34 56.78 90.10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.