#I'm trying to get this program to fill two 1d arrays of a defined size and then calculate some stats

17 messages · Page 1 of 1 (latest)

lusty sentinel
#

Here are the assignment instructions. I have the c program attached.

Write a program and functions that will get 2 sets of random numbers from a random number
generator, average the 2 sets, and calculate the difference in the two averages. More specifically,
the program should do the following:

  1. Include these files: stdio.h, stdlib.h, and time.h
  2. Main function
    a. Define manifest constant NSIZE1 and NSIZE2 for the number of values in data
    sets 1 and 2, respectively. Set their values to 4 and 8, respectively.
    b. Declare arrays to hold the sets of random numbers. The arrays should be of type
    double.
    c. Include the statement:
    srand((unsigned int) time(0));
    before calling rand(). The call to srand() will initialize the random number
    generator; it should only be called one time. srand() is defined in stdlib.h and
    time() is defined in time.h.
    d. Fill the arrays with data by calling FillArray() which is a function that you write.
    e. Compute the average for each set of values by calling a dAvg() function which is
    another function that you create. This function should return the average as a
    double value and should accept a 1-dimensional array of values to be averaged.
    f. Determine the largest value in each set of data by calling a dMax() function that
    you write. dMax() should accepted a 1-dimensional array and return the
    maximum value as a double value.
    g. In your main function, print out the results in the following format:
    i. The number of data sets and the number of items in a data set.
    ii. For the first set of data, print its average, a colon, and then the values of its
    items on one line.
    iii. On the next line, print the second set’s average, a colon, and then the
    values of its items.
    iv. On the next line print the differences (set 1 – set 2) in the 2 sets’ average
    and maximum values. Be sure to clearly label your results.
  3. void FillArray(double vs[], int nVals); // Prototype
    a. Uses the following expression to fill the array with random data.
    rand()*10.0/RAND_MAX
    b. rand() is defined in stdlib.h and returns an integer value in the range of 0 to
    RAND_MAX.
    c. Does not return a value.(
  4. double dAvg(double vs[], int nVals); // Prototype
    a. Returns a double value containing the average value for an array of doubles that is
    passed to it.
  5. double dMax(double vs[], int nVals); // Prototype
    a. Returns a double value containing the maximum value in the array that is passed
    to it.

OUTPUT


PLACEHOLDER NAME

Analysis of Data. . .
Number of datasets: 2
Number of items in set 1: 4
Number of items in set 2: 8


(No return value, program will not close. Maybe has to do with one of the loops I have?)

robust shadowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

long ice
#

First of all, by convention variables and functions should be camelCase, not PascalCase (i.e. first character shouldn't be capitalized).

sizeof(array) / 8 == NSIZE1
This check in your fillArray method already doesn't work. array is a pointer, so assuming you're on a 64-bit system it'll most likely return 8 for whatever it points to. This means sizeof(array) / 8 will probably always be 1.
The general approach to fix this is to also pass the length of the array, which also would drastically reduce your code's complexity.

#

On a second check this is even shown in your assignment:
3. void FillArray(double vs[], int nVals); // Prototype

lusty sentinel
#

This is very helpful actually, so because array is a pointer it has the standard value of a double, but if I passed an array with something like:

#

Wait nevermind, are these checks unnecessary?

#

@long ice Could I have an example?

long ice
long ice
lusty sentinel
#

Of a check being passed. I'm kind of digesting right now and trying to rewrite some code.

long ice
#

check that needs to be passed?

#

In your assignment all the functions' signatures tell you, they expect a second parameter of type int, which probably describes the size of the array you're passing

lusty sentinel
#

Okay, rewrote the code and it works

#

I made it much more complicated than it needed to be. If anyone is wondering, here is the functioning code:

#

!solved

robust shadowBOT
#

Thank you and let us know if you have any more questions!