-4

I need homework help! This isn't an assignment that is due so please no comments saying that I need to figure this out on my own. Just doing some extra practice to prepare for my exam.

I need to 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.

Use a for loop.

The array fArray[] is defined in a file called arraypractice.h . Add it to your project directory and refer to it in your code as follows:

int main(void)
{
#include "arraypractice.h“
.

I have only gotten as far as determining the length of the array and I don't know where to go from here:

int main(void)
   {
#include "arraypractice.h"

   int n;
   n = sizeof(fArray);
   int size;

   printf("Size of the given array is %d\n", n / sizeof(double));
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • 6
    Ever heard of `for` loops? – Chisko Apr 05 '18 at 17:21
  • 2
    If you are doing this to prepare for an exam then in some sense it is even *more* imperative for you to figure it out on your own since you won't have access to Stack Overflow on the exam (given reasonable proctoring). – John Coleman Apr 05 '18 at 17:22
  • Possible duplicate of [C Array loop through](https://stackoverflow.com/questions/18669574/c-array-loop-through) – Chisko Apr 05 '18 at 17:24
  • 3
    The instructions tell you to put `#include "arraypractice.h"` inside your main? – Tormund Giantsbane Apr 05 '18 at 17:29
  • Don't quite know why this is getting so many downvotes -- it clearly explains that it is for homework and shows OP's efforts. It doesn't deserve any upvote, but is better than the typical copy-paste of a homework problem with no effort shown. – John Coleman Apr 05 '18 at 17:32
  • I don't think explaining a `for` is convenient for SO @JohnColeman – Chisko Apr 05 '18 at 17:33
  • @Chisko I don't disagree, but just wanted to give OP some credit for at least trying (albeit not enough credit to actually upvote it) – John Coleman Apr 05 '18 at 17:35
  • 2
    I find it a little unsettling an assignment would ask students to check equality on a floating-point type. Even if 6.0 is representable by double-precision floating point representation, it sets a bad precedence. – Christian Gibbons Apr 05 '18 at 18:27

1 Answers1

1

You need to look up for loops in a tutorial, among other things :p

Your question regarding the average with 7 decimal places is under the topic of precision. What I used was %12.7f, f being used for the double and 12.7 meaning 12 digits before the decimal point and 7 digits after.

#include <stdio.h>

int main(void) {
  double fArray[10] = {1.2, 12.6, 6.0, 5.3, 3.2, 6.0, 5.0, 60.3, 6.0, 1.1};
  int count = 0;
  int sixes = 0;
  double sum = 0;

  for (int i = 0; i < (sizeof(fArray) / sizeof((fArray)[0])); i++)
  {
      count +=1;
      sum += fArray[i];
      if(fArray[i] == 6.0){
          sixes ++;
      }
  }
  printf("Total elements: %d \n Sixes found: %d \n Average is: %12.7f", count, sixes, sum/count);
  return 0;
}

Edit

The code above will run fine, but as for reading in doubles from a file. You would have to specify more what your file looks like, otherwise that probably deserves another question SEARCH on Stack Overflow.

...But this (untested) code sample should get you started :)

FILE *f=fopen("file.txt","r");

if(f==NULL)
    return 1;

double values[10];
unsigned int i;         

for(i = 0; i < 10; ++i) {
    fscanf(f, "%lf",&values[i]);
    printf("%lf\n",values[i]);
}
close(f);
Camilo
  • 35
  • 11