# C program problem statement

1. Nov 2, 2013

### freezer

1. The problem statement, all variables and given/known data

A simple method to get a random uniform variable of data type float, between 0 (inclusive) and 1 (exclusive) is:

x = rand()/(RAND_MAX + 1);

where x is a float.

Write a program that fills an array with 1000 integers random numbers, then uses those to fill another array with 100 float random numbers between 0 and 1. Find and print out the average value of the integer array and float array.

2. Relevant equations

3. The attempt at a solution

I am confused about the second array and how i would use x variable and the first array to fill the second array.

Here what I have so far to fill the arrays:

Code (Text):

#include<stdio.h>
#include<cstdlib>
#include<time.h>

#define SIZE1 1000
#define SIZE2 100

void main(void)
{

float x;
int bob[SIZE1];
float sam[SIZE2];

srand( (unsigned)time( NULL ) );

for(int i = 0; i < SIZE1; i++)
{

bob[i] = rand();

}//End For Loop 1

for(int i=0; i<SIZE2; i++)
{

x = rand()/(RAND_MAX + 1.0);
sam[i] = x;

}// End for loop 2

}//End Main

2. Nov 2, 2013

### UltrafastPED

Your first array has a thousand numbers, scaled from 0-999 or whatever your spec says.

Knowing the maximum value stored in that array (or finding it if unknown), you can generate the second array by plucking out integers from the first array (perhaps the first 100 of them), convert to float, and then divide by the maximum value in the first array ... if you pick the maximum integer, this will generate a 1.0 ... if you want it literally between 0 and 1 - (0,1) instead of [0,1] - then divide by the max+1.

You have a similar issue at the zero end.

3. Nov 3, 2013

### freezer

So do you think the professor is showing you an example with the "x" equation and we are just to use that as a model or that we should utilize x to accomplish this problem?

Here is what i have:

Code (Text):

#include<stdio.h>
#include<cstdlib>
#include<time.h>

#define SIZE1 1000
#define SIZE2 100

void main(void)
{

float x;
int bob[SIZE1];
float sam[SIZE2];
int bob_max = 0;
float sum = 0.0;
float avg = 0.0;

srand( (unsigned)time( NULL ) );

for(int i = 0; i < SIZE1; i++)
{

bob[i] = rand();

if(bob[i] > bob_max)
bob_max = bob[i];

}//End For Loop 1

for(int i=0; i<SIZE2; i++)
{

x = float((rand()/(RAND_MAX + 1.0)));
sam[i] = float(bob[rand()%SIZE1]/(bob_max + 1.0));

sum += sam[i];

//printf_s("%lf \n", sam[i]);
}// End for loop 2

avg = sum/SIZE2;

printf_s("\n\nThe average is: %lf\n\n", avg);

}//End Main

4. Nov 3, 2013

### Staff: Mentor

The assignment statement in your original post (you shouldn't think of it as an equation) shows how to get values to store in the larger array. For the smaller array, I would take UltrafastPED's advice and just copy the first 100 values of the large array to the smaller array. What you did was find a random index value and then copy the value at that index in the large array into the small array. Since the values in the large array are random anyway, what difference does it make if you take the first 100 of them, or skip around picking them at random?

Some nits:
1. Naming your arrays bob and sam is silly. The compiler certainly doesn't care what you call them, but humans who read your code have no idea that these identifiers have anything to do with an array. It's a good idea to use names that suggest to the human reader of your code the purpose of that variable.

2.
Code (Text):
if(bob[i] > bob_max)
bob_max = bob[i];

You don't need braces when the body of your if, for, while, etc. statement has only a single statement, but it's a good idea to include them anyway. Thousands of programmers have gotten bit when they added a second line to the body and couldn't figure out why the second statement wasn't executing like they thought it should. Coding standards at many companies require braces for all if, while, for, etc. blocks.

5. Nov 3, 2013

### freezer

Okay, guess the question now is why have an array of 1000 if we only need the first 100 anyway? What good are the remaining 900?

So even if i just take the first 100 values and divide it by bob_max + 1 to fill the second array. I am not sure I interpreter the original problem statement correctly.

Got ya on point 2.

6. Nov 3, 2013

### rcgldr

The problem statement doesn't explain how you are to use 1000 integers with range 0 to RAND_MAX to fill an array of 100 floats. The program could take the average of 10 integers at a time, then use each average divided by (RAND_MAX + 1.0) to store into one element of the array of 100 floats. The current program just calls rand() again for the array of 100 floats, but the array of 100 floats is supposed to be based on the array of 1000 integers.