How Can You Convert 1000 Random Integers to 100 Floats in C?

  • Thread starter Thread starter freezer
  • Start date Start date
  • Tags Tags
    Program
Click For Summary

Discussion Overview

The discussion revolves around a programming assignment that requires participants to fill an array with 1000 random integers and then use those integers to fill another array with 100 float random numbers between 0 and 1. Participants explore how to correctly implement the conversion from integers to floats and the relationship between the two arrays.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests using the formula x = rand()/(RAND_MAX + 1) to generate float values, but expresses confusion about how to apply this to the second array.
  • Another participant proposes that the second array could be filled by selecting integers from the first array, converting them to floats, and dividing by the maximum value found in the first array to ensure values are between 0 and 1.
  • A different participant questions whether the professor intended for the "x" equation to be a model or if it should be directly utilized in the solution.
  • One participant critiques the approach of randomly selecting indices from the first array, suggesting instead to simply take the first 100 values for simplicity.
  • Another participant raises a concern about the necessity of having an array of 1000 integers if only the first 100 are used, questioning the interpretation of the assignment.
  • A later reply suggests averaging groups of integers from the first array to fill the float array, indicating that the current implementation does not adhere to the assignment's requirements.

Areas of Agreement / Disagreement

Participants express differing views on how to utilize the first array to fill the second array, with no consensus on the best approach. Some advocate for using the first 100 integers directly, while others suggest more complex methods involving averages or maximum values.

Contextual Notes

The discussion highlights various interpretations of the assignment statement and the lack of clarity regarding how to connect the two arrays. There are also mentions of coding practices and naming conventions that could improve code readability.

freezer
Messages
75
Reaction score
0

Homework Statement



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.


Homework Equations





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:
#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
 
Physics news on Phys.org
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.
 
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:
#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
 
freezer said:
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?
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:
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.
 
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 you on point 2.
 
freezer said:
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.
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.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K