Solving a Programming Problem: Design & Build a Program

AI Thread Summary
The program aims to create a 100-element array of random float values between 1 and 10, calculate the maximum and minimum values, and determine the average. It includes subprograms for generating random values, locating the max and min, and calculating how many values are above and below the average. However, the code has several issues, primarily related to pointer usage and uninitialized variables. The pointers for max, min, avg, and counters are not properly initialized in the main function, leading to errors when dereferencing them. To resolve this, the pointers should either be allocated memory using `new` or replaced with regular float variables passed by reference. Additionally, to ensure varied random numbers, the `srand` function should be used with a time seed. Proper debugging practices, such as using `cout` for output, can help identify issues during development.
Azrioch
Messages
30
Reaction score
0
The problem is as follows:

Design and build a program that does the following:
a. Declare a 100-element array that will hold float values.
b. In a subprogram set each element of the array to a random float value from 1 to 10;
i. Use the rand () function and the stdlib.h include file;
c. Declare two float variables to hold the max and min value of the array
d. Make a sub program that will accept the array and the two variable as arguments
i. The subprogram will search the array and find the max value and place in the max variable.
ii. The subprogram will find the min value and place it the min variable.
e. Calculate the average of the max. and min. then make sub program to use the value as follows:
i. The subprogram will use the random array and the average value to determine the number of values of above the average value and the number of values below the average value.
ii. The subprogram shall be designed in such a way as not to be able to change the value of the average value.
f. In the main function output the statistics:
i. The range of values in the array
ii. The mid point of the array
iii. The number of values above and below the mid point


This compiles but has an error when I run it. I'm still new at programming and, well, I've tried to tinker around with it but to no avail. Any insight as to what might be wrong?

This is my code:
Code:
#include <StdAfx.h>
#include <iostream>
#include <math.h>

#include <iomanip>
#include <cstdlib>


using namespace std;

void randomizer(float*);
void locate(float*,float*,float*);
void calculate(float*,float*,float*,float*,int*,int*);

int main()
{
float random[100], *max, *min, *avg;
int *abovecounter, *belowcounter;
randomizer(random);
locate(max,min,random);
calculate(max,min,random,avg,abovecounter,belowcounter);
cout     << "The range of values is: "<< max<< " - "<<min<<" = "<<max-min<<endl;
cout     << "The mid point of the values is: " << avg << endl;
cout     << "The number of values above the mid point is: "<<abovecounter<<endl;
cout     << "The number of values below the mid point is: "<<belowcounter<<endl;
return 0;
}

void randomizer(float *random)
{    
for(int index =0; index<=100; index++)
	{
 random[index] = ((rand()/RAND_MAX)*9+1);
	}

}

void locate(float *max,float *min, float* random)
{
  *max=random[0];
  *min=random[0];
 for(int index = 0; index<=100; index++)
	{
  if(random[index]>*max)
  {
	  *max=random[index];
  }
  if(random[index]<*min)
  {
	  *min=random[index];
  }
	}
}

void calculate(float*max, float*min, float*random, float*avg, int*abovecounter, int*belowcounter)
{

*avg = ((*max+*min)/2);
for(int index=0; index<=100; index++)
	{
		if(random[index]>*avg)
        {
			belowcounter = belowcounter +1;
		}
        if(random[index]<*avg)
        {
			abovecounter = abovecounter +1;
		}
	}
                             
}
 
Computer science news on Phys.org
it'd help if you post the error. but the mostlikely error is that you declared all your individual value variables as pointers and are displaying the pointer not the contents...remember you have to dereference to use the contents of the pointer...its funny that you did when calculating max/min but not when you displayed or calculates above/below.

and if your knew to programming...printf/cout are your friend...
if your unsure of some function or someline...learn to use them to feed out the data so you know portions are working correctly. One you get used to the debugger then you can move onto it...but for now printf/cout are your friends.
 
Last edited:
The problem is that the only objects you've created in main is an array of 100 floats, and a bunch of uninitialized pointers.
 
So do I need to then initialize them?

I thought that I could pass them uninitialized and have them become initialized in the subfunctions.

:frown:
 
How do I get those values to stay in the main function after I set them down in the subfunctions?

When I compile it tells me that it used all the variables with them still being uninitialized.
 
How do I get those values to stay in the main function after I set them down in the subfunctions?

The only values you've created are an array of floats, and some memory addresses. You have not created any integer values in main.
 
Last edited:
A pointer variable is a variable that hold the addres of some memory location. In your program the pointer variable max hold an address, and *max refers to the memory at that address.

You get an error because you use *max while you never declared the memory to which max should point. In main you have to declare some memory and assign the address of that memory to max. Like this:

max = new float;

and the same for all the other pointer variables

(when you do not use the declared memory anymore you should free it again, like this: delete max; but since that is at the end of your program it is not very important in your case)

----------------------------
Alternatively, you would not declare max as a pointe but just a float and pass teh adress of max to the other functions, like this:
Code:
int main()
{
   float random[100], max, min, avg;
   ...
   locate(&max, &min, random);
   ...
}


void locate(float *max, float *min, float* random)
{
  *max=random[0];
   ...
}
 
Awesome, I got it.

I guess that I just wasn't understanding the whole pointer concept fully.

It seems that I need a new seed though in order to constantly get random numbers.
 
Got it.

Just include <time.h>

and
srand((unsigned)time( NULL ));


Thanks for the help with the program. :approve:
 
Back
Top