1

My program is suppose to read the Numbers text file which is where I have listed my numbers and store them into an array then display in 5 columns. My problem is that it wont display the array at all. Im not sure why.

#include <stdio.h>
#include <stdlib.h>
#define ELEMENTS 100

void fillArray(FILE *, double [], double *);
void printArray(double [], double); 
double findMIN(double [], double);
double findMAX(double [], double);

int main()
{
    FILE *doublefp;
    double values[ELEMENTS];
    double elements;
    int status;

    status = fopen_s(&doublefp, "Numbers.txt", "r");
    if (status != 0)
    {
        printf("Unable to open the file Numbers.txt\n");
        system("PAUSE");
        exit(99);
    }

    fillArray(doublefp, values, &elements);
    printArray(values, elements);


    printf("The minimum value is %d\n", findMIN(values, elements));

    printf("The maximum value is %d\n", findMAX(values, elements));

    system ("PAUSE");
    return 0;

}

double findMIN(double nums[], double element)
{
    int i;
    double min = nums[0];

    for (i = 1; i < element; i++)
    if (min > nums[i])
      min = nums[i];

  return (min);
}

double findMAX(double nums[], double element)
{
    int i;
    double max = nums[0];

    for (i = 1; i < element; i++)
    if (max < nums[i])
      max = nums[i];

  return (max);

}

void fillArray (FILE *fp, double nums[], int *count)
{
    double number;

    printf("Enter up to %d integers press the F6 key to end input. \n", ELEMENTS);
    *count = 0;
    while (fscanf_s(fp, "%d", &nums[*count]) != EOF)
    {
        (*count)++;
    }

}

void printArray (double nums[], double elements)
{
    int count;

    printf("Values in array:\n");
    for (count = 0; count < elements; count++)
    {
        printf("%5d ",nums[count]);
        if ((count+1)% 10 == 0)
           printf("\n");
    }
    printf("\n");printf("\n");


}

My numbers are in a text file listed like this: 23.53 56.8 12.1 677.23 122.09 788.18 123.25 65.12 98.18 622.27 366.34 433.45 844.56 244.67 544.78 290.10 189.28 522.17 321.33 178.76

4
  • The %d conversion operates on integers. Try using %f for doubles instead. You'll also want the file to contain one value per line. Commented Oct 27, 2013 at 2:18
  • 1
    Turn on all the warnings in your complier, fix them and then ask again. Commented Oct 27, 2013 at 2:22
  • 1
    fillArray() isn't even defined in the same way that it's declared, this code shouldn't even compile, let alone compile without warnings. Commented Oct 27, 2013 at 2:23
  • I had to keep changing them around so my pointers would work and I guess my warnings wasn't on either. Thanks though! Commented Oct 27, 2013 at 3:05

2 Answers 2

2

Basically, double elements; needs to be int elements

Here &elements is a double *, the function fillArray expects an int *

fillArray(doublefp, values, &elements);

Did you turn the warnings on in your compiler?

Also, in printArray, elements is defined as a double when it should be an int.

Same with findMax and findMin.

And printf("%5d ",nums[count]); should be printf("%lf ",nums[count]);

Turn on all the warnings in your complier, fix them and then ask again if it doesn't work.

Sign up to request clarification or add additional context in comments.

1 Comment

I saw the warnings in the ouput when building. I fix them all and now it works. Thank you!
1

Two problems:

1) Your "printf()" format statement should use "%d" to print integers, and "%lf" to print doubles. You're not doing this consistently.

2) You're better off using an "int" for "counting numbers", instead of double. Unfortunately, you're not doing this consistently, either.

SUGGESTED CHANGES:

#include <stdio.h>
#include <stdlib.h>
#define ELEMENTS 100

void fillArray(FILE * fp, double values [], int * count);
void printArray(double values[] int count); 
double findMIN(double values[], int count);
double findMAX(double values[], int count);

int main()
{
    FILE *doublefp;
    double values[ELEMENTS];
    int elements;
    int status;

    status = fopen_s(&doublefp, "Numbers.txt", "r");
    if (status != 0)
    {
        printf("Unable to open the file Numbers.txt\n");
        system("PAUSE");
        exit(99);
    }

    fillArray(doublefp, values, &elements);
    printArray(values, elements);


    printf("The minimum value is %f\n", findMIN(values, elements));
    printf("The maximum value is %f\n", findMAX(values, elements));

    system ("PAUSE");
   ...   

double findMIN(double nums[], int count)
   ...

void fillArray (FILE *fp, double nums[], int *count)
{
    printf("Enter up to %d integers press the F6 key to end input. \n", ELEMENTS);
    *count = 0;
    while (fscanf_s(fp, "%f", &nums[*count]) != EOF)
    {
        (*count)++;
    }
    ...

1 Comment

1 more thing. Im trying to format the decimals so it wont have all the zeros after that. I tried %8.2f but it gives me an error this time. EDIT: Nevermind. I got it c:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.