# Solving a Programming Problem: Design & Build a Program

• Azrioch
In summary, the program aims to design and build a program that declares a 100-element array to hold float values, sets each element to a random float value from 1 to 10, finds the max and min value of the array, calculates the average of the max and min values, and determines the number of values above and below the average value. The main function outputs the range of values in the array, the mid point of the array, and the number of values above and below the mid point. However, there was an error when running the program and the user sought help in identifying the issue. After receiving guidance on pointers and defining memory for variables, the user was able to successfully run the program. They also learned about using
Azrioch
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;
}
}

}

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.

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.

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.

## 1. How do I approach solving a programming problem?

The first step in solving a programming problem is to clearly define the problem and understand the requirements. Then, break down the problem into smaller, manageable tasks. It is also helpful to research and gather any necessary information or resources for the problem.

## 2. What should I consider when designing a program?

When designing a program, it is important to consider factors such as the target audience, the purpose and functionality of the program, and any potential limitations or constraints. It is also helpful to plan and outline the program's structure and flow before starting to code.

## 3. What are some key elements to include in a program's code?

Some key elements to include in a program's code are comments, descriptive variable names, and proper indentation and formatting. This helps to make the code more readable and maintainable, as well as facilitate collaboration with others.

## 4. How do I test and debug my program?

Testing and debugging are crucial steps in the programming process. One approach is to use a combination of manual testing and automated testing tools. Additionally, utilizing debugging tools and carefully reviewing error messages can help identify and fix any issues in the code.

## 5. How can I continuously improve my programming skills?

Continuous learning and practice are key to improving programming skills. It is helpful to regularly seek out new challenges and projects, and to study and learn from experienced programmers. Additionally, staying up-to-date with industry trends and advancements can also aid in skill development.

• Programming and Computer Science
Replies
8
Views
1K
• Engineering and Comp Sci Homework Help
Replies
8
Views
945
• Programming and Computer Science
Replies
22
Views
3K
• Engineering and Comp Sci Homework Help
Replies
7
Views
1K
• Engineering and Comp Sci Homework Help
Replies
2
Views
2K
• Programming and Computer Science
Replies
66
Views
4K
• Programming and Computer Science
Replies
20
Views
1K
• Programming and Computer Science
Replies
10
Views
1K
• Programming and Computer Science
Replies
75
Views
4K
• Programming and Computer Science
Replies
5
Views
976