Solving a Programming Problem: Design & Build a Program

Click For Summary

Discussion Overview

The discussion revolves around a programming problem involving the design and implementation of a program that manipulates an array of float values. Participants explore issues related to pointer usage, variable initialization, and the correct implementation of functions to achieve the desired functionality.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant presents a code snippet and describes the intended functionality, including generating random float values, finding max/min values, and calculating averages.
  • Another participant suggests that the error may stem from using pointers incorrectly and emphasizes the need to dereference pointers to access their contents.
  • A participant points out that only an array of floats is created in the main function, while other variables are uninitialized pointers.
  • There is a question about whether pointers need to be initialized before being used in subfunctions.
  • Participants discuss how to retain values in the main function after they are set in subfunctions, highlighting the need for proper variable declaration.
  • One participant explains that pointers must point to allocated memory and suggests using `new` to allocate memory for pointer variables.
  • Another participant proposes an alternative approach of using regular float variables instead of pointers, passing their addresses to functions.
  • A participant expresses a newfound understanding of pointers and mentions the need for a new seed to generate random numbers consistently.
  • Finally, a participant suggests including `` and using `srand((unsigned)time(NULL));` to achieve this.

Areas of Agreement / Disagreement

Participants generally agree on the need to properly initialize pointers and the importance of understanding pointer concepts. However, there are multiple competing views on whether to use pointers or regular variables, and the discussion remains unresolved regarding the best approach.

Contextual Notes

Limitations include the potential for uninitialized pointers leading to runtime errors, and the scope of the discussion is limited to the specific programming problem presented without broader context on pointer usage in C++.

Who May Find This Useful

Individuals learning C++ programming, particularly those struggling with pointers and memory management, may find this discussion beneficial.

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/count 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/count 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 the address 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:
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
22
Views
5K
Replies
5
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
11K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
6K
  • · Replies 5 ·
Replies
5
Views
4K