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

  • Thread starter freezer
  • Start date
  • Tags
    Program
In summary: The program as it is could just fill the array of 100 floats with values from the larger array of 1000, with no need for a second call to rand().As for the 1000 integers, I'm guessing the professor wants to show students how using a larger range of random numbers will yield a better average. If the random number generator always yields a value between 0 and RAND_MAX, then the average will be RAND_MAX/2. If the random number generator yields numbers in the range 0 to RAND_MAX/10, then the average will be RAND_MAX/20. This is just a guess. I think the problem statement is incomplete.If you're confused about the second array, you could ask the professor for clarification
  • #1
freezer
76
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
  • #2
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
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
 
  • #4
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.
 
  • #5
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.
 
  • #6
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.
 

What is a C program problem statement?

A C program problem statement is a concise and clear description of the problem that a C program is designed to solve. It outlines the objectives, constraints, and expected outcomes of the program.

Why is a problem statement important in a C program?

A problem statement helps the programmer to understand the purpose of the program and stay focused on solving the specific problem. It also helps in identifying potential issues and evaluating the success of the program.

How do you write a good problem statement for a C program?

To write a good problem statement for a C program, start by clearly defining the problem and its scope. Then, identify the key requirements and constraints of the problem. Make sure to use specific and measurable language in your statement. Finally, review and revise the statement to ensure it accurately reflects the problem and its objectives.

What is the difference between a problem statement and a project scope in a C program?

A problem statement focuses on the specific issue that the C program will solve, while a project scope outlines the overall goals, objectives, and deliverables of the project. The problem statement provides a detailed description of the problem, whereas the project scope provides a broader overview of the project.

Can a problem statement change during the development of a C program?

Yes, a problem statement can change during the development of a C program. As the programmer gains a deeper understanding of the problem and its complexities, the problem statement may need to be revised to reflect any changes in the objectives or constraints. It is important to regularly review and update the problem statement to ensure the program remains focused on solving the problem at hand.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
987
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
Back
Top